Passing an XPath Expression to an parameter

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing an XPath Expression to an parameter

PackerBronco
I'm trying to create a cross-browser solution for passing an XPath
expression to a XSLT global parameter. The XSLT global parameter is:

<xsl:param name="group" select="//person" />

What I would like to do is have users enter in a predicate expression on a
Web form such as:

name='Jones'

and then have JavaScript dynamically change the value of the group parameter
so that XSLT treats it as:

<xsl:param name="group" select="//person[name='Jones']" />

I can do this in IE using the following code:
=============
   fstring=document.webform.filter.value;
   if (fstring=="") filter="//person"
   else filter="//person["+fstring+"]";
   XMLProcessor.addParameter("group", XMLDoc.selectNodes(filter));
=============

so that the node-set is specified using the value of the filter variable and
then treated as a node-set by the group parameter.

My question is what is the equivalent approach for Mozilla?

I've tried the following approach using the XPathEvaluator class:
============
   fstring=document.webform.filter.value;
   if (fstring=="") filter="//person"
   else filter="//person["+fstring+"]";

   xEval = new XPathEvaluator();
   xNodes =
xEval.evaluate(filter,XMLDoc.documentElement,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);

   XMLProcessor.setParameter(null,"group",xNodes);
============

but that doesn't work I assume that the XPathResult object, xNodes, is not
being treated as a node-set in the setParameter() method. I've also tried to
send the filter variable to the setParameter() method, but that doesn't work
either (I assume that the group parameter treats the filter variable as a
text string rather than as an XPath expression.)

Any help would be GREATLY appreciated! Thanks.


_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Passing an XPath Expression to an parameter

Jonas Sicking
PackerBronco wrote:

> I've tried the following approach using the XPathEvaluator class:
> ============
>    fstring=document.webform.filter.value;
>    if (fstring=="") filter="//person"
>    else filter="//person["+fstring+"]";
>
>    xEval = new XPathEvaluator();
>    xNodes =
> xEval.evaluate(filter,XMLDoc.documentElement,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);
>
>    XMLProcessor.setParameter(null,"group",xNodes);
> ============
>
> but that doesn't work I assume that the XPathResult object, xNodes, is not
> being treated as a node-set in the setParameter() method.
That should work just fine. What version of Firefox are you using? Make
sure that you use the right name for the parameter and that your
expression evaluates correctly.

> I've also tried to
> send the filter variable to the setParameter() method, but that doesn't work
> either (I assume that the group parameter treats the filter variable as a
> text string rather than as an XPath expression.)

Yep, if you set the parameter to a string we'll use that as the value.
You can use numbers booleans and nodes too. I've attached a small
testcase that works fine.

/ Jonas

<script> function runTest() { parser = new DOMParser(); xslDoc = parser.parseFromString('\ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"\ xmlns="http://www.w3.org/1999/xhtml">\ <xsl:param name="myParam"/>\ <xsl:template match="/"><html><body><xsl:copy-of select="$myParam"/></body></html>\ </xsl:template>\ </xsl:stylesheet>', 'text/xml'); xmlDoc = parser.parseFromString('<testDoc><p>inDoc</p></testDoc>', 'text/xml'); proc = new XSLTProcessor(); proc.importStylesheet(xslDoc); param = xmlDoc.evaluate('//p', xmlDoc, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); proc.setParameter(null, 'myParam', param); resDoc = proc.transformToDocument(xmlDoc); alert(resDoc.documentElement.innerHTML); } </script>

myDoc


_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml
Reply | Threaded
Open this post in threaded view
|

Re: Passing an XPath Expression to an parameter

PackerBronco
Jonas,
 
First, thank you very much for taking the time to respond.
 
I tried your test document and that failed on my version of Firefox. The error message in the JavaScript console was:
 
Error: uncaught exception: [Exception... "Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIXSLTProcessor.transformToDocument]"  nsresult: "0x80070057 (NS_ERROR_ILLEGAL_VALUE)"  location: "JS frame :: etc.  :: runTest :: line 24"  data: no]
 
and there was no alert box because of the failure. However this was on Firefox 1.07. Taking your hint I tried it on Firefox 1.5 and it worked.
 
So you made my day. Thank you!!!
 
/ Patrick
"Jonas Sicking" <[hidden email]> wrote in message <A href="news:oZ6dneiSzqW5y77Z4p2dnA@mozilla.org">news:oZ6dneiSzqW5y77Z4p2dnA@......
PackerBronco wrote:

> I've tried the following approach using the XPathEvaluator class:
> ============
>    fstring=document.webform.filter.value;
>    if (fstring=="") filter="//person"
>    else filter="//person["+fstring+"]";
>
>    xEval = new XPathEvaluator();
>    xNodes =
> xEval.evaluate(filter,XMLDoc.documentElement,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);
>
>    XMLProcessor.setParameter(null,"group",xNodes);
> ============
>
> but that doesn't work I assume that the XPathResult object, xNodes, is not
> being treated as a node-set in the setParameter() method.

That should work just fine. What version of Firefox are you using? Make
sure that you use the right name for the parameter and that your
expression evaluates correctly.

> I've also tried to
> send the filter variable to the setParameter() method, but that doesn't work
> either (I assume that the group parameter treats the filter variable as a
> text string rather than as an XPath expression.)

Yep, if you set the parameter to a string we'll use that as the value.
You can use numbers booleans and nodes too. I've attached a small
testcase that works fine.

/ Jonas


<SCRIPT> function runTest() { parser = new DOMParser(); xslDoc = parser.parseFromString('\ <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"\ xmlns="http://www.w3.org/1999/xhtml">\ <xsl:param name="myParam"/>\ <xsl:template match="/"><html><body><xsl:copy-of select="$myParam"/></body></html>\ </xsl:template>\ </xsl:stylesheet>', 'text/xml'); xmlDoc = parser.parseFromString('<testDoc><p>inDoc</p></testDoc>', 'text/xml'); proc = new XSLTProcessor(); proc.importStylesheet(xslDoc); param = xmlDoc.evaluate('//p', xmlDoc, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); proc.setParameter(null, 'myParam', param); resDoc = proc.transformToDocument(xmlDoc); alert(resDoc.documentElement.innerHTML); } </SCRIPT>

myDoc


_______________________________________________
dev-tech-xml mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xml