Re: Passing an XPath Expression to an parameter

Jonas Sicking <[email protected]>
Newsgroups gmane.comp.mozilla.devel.xml
Message-ID <[email protected]>
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

_______________________________________________
dev-tech-xml mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xml
xsltParamTest.html (text/html, 876 B)
<html><head>
<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>
<body onload="runTest() } catch(e) { alert(e.message); }">
<p>myDoc</p>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.