0

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?

3
  • 1
    That XSLT file isn't valid. There are no templates. I'd suggest rewriting your XSLT. Commented Dec 9, 2013 at 4:48
  • Have you tried simply using the parameter as is, i.e. <xsl:for-each select="$queryFilter">? Commented Dec 9, 2013 at 5:29
  • I believe what you are trying to do is dynamic evaluation of xpath, which is not supported in XSLT 1.0 (or 2.0), I think. You can use an extension function to do this though. Check out exslt.org/dyn/index.html for example. Commented Dec 9, 2013 at 8:57

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.