tosi
tosi

Reputation: 1925

pass param as xpath expressin to XSLT- javascript

Looking for a solution, on the following: This is my javascript function which is setting a parameter to my xsl file and this parameter contains a string query which has a xpath syntx

queryFilter = "*/person[name='John']";

function getXSLDoc(xslDocument,xmlDocument,queryFilter) {
 ..
  var processor = new XSLTProcessor();
  processor.setParameter(null,"name",queryFilter);
....
}

Now on my xslt file I want to take queryFilter string and use it in a loop or create a variable:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/1999/xhtml">
     <xsl:output method="html" indent="yes"/>

     <xsl:param name="queryFilter"/> 

<!-- Lets say I want to create a local variable and use it to loop on a node -->

    <xsl:variable name="varFilter" select="$queryFilter"/>

    <xsl:for-each select="$**varFilter**">
        <tr>
            <xsl:apply-templates select="name"/>
            ..
        </tr> 
    </xsl:for-each>

 </xsl:stylesheet>

Now how do I get to use the param i set on my javascript function as an xpath expression?

Upvotes: 0

Views: 159

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167401

There is no support to evaluate a complete XPath expression dynamically at run-time. But for many cases it suffices to pass in a number or string value which you use in a comparison e.g.

name = "John";

and

processor.setParameter(null,"name",name);

and then in the XSLT you have

<xsl:param name="name"/> 

and

<xsl:for-each select="*/person[name = $name]">...</xsl:for-each>

Upvotes: 1

Related Questions