1

I have an XML file which has XPath string in its attribute. Something like this -


    <root>
     <element name="A" visible="True" text="Display Text " selected="True" />
     <element name="B" visible="True" text="DisplayText" visibilityCondition="//element[@name='A']/@selected" />
    </root>
    

Now using XSLT I want to show or hide content based on visibilityCondition of element named A. i.e.

    <xsl:for-each select="element">
     <xsl:variable name="visibleCondition" select="@visibilityCondition" />
     <xsl:choose>
      <xsl:when test="boolean($visibleCondition)">
       <xsl:when test="$visibleCondition">
        <xsl:if test="$visibleCondition='True'">
         ...
        </xsl:if>
        <xsl:otherwise>
         ...
        </xsl:otherwise>
       </xsl:when>
      </xsl:choose>
     </xsl:foreach>
    

The problem is the if condition always fails since, $visibleCondition has a value //element[@name='A']/@selected whereas I want to get the XPath parsed and match against the actual value in the selected attribute which is True. How do I go about achieving this?

5
  • 1
    Please be more careful with posting XML. The value of the name attribute of the B element has no opening quote, the attribute visibilityCondition is not a valid XPath expression. Commented Feb 26, 2014 at 8:59
  • Thank you Mathias. I have corrected the Xml now. Commented Feb 26, 2014 at 9:44
  • What you want to do it seems, is evaluate the string stored in the visibilityCondition attribute as xpath, which is not possible natively. Here is a similar question I found googling <SOLVED> XSLT: How to eval an XSL constructed XPath expression Commented Feb 26, 2014 at 9:53
  • @Tobias, thank you for sharing the post. I am finding it difficult to understand how the post you shared is related to my question. If you could explain little more it will me more helpful. Commented Feb 26, 2014 at 10:00
  • ...posted it as answer. Commented Feb 26, 2014 at 10:07

2 Answers 2

1

You cannot evaluate a string that you have stored in an attribute as XPath, which is the same as if you construct an xpath that you want to evaluate, without an extension.

In your example boolean($visibleCondition) will only check if the @visibilityCondtion attribute is present but will not evaluate the expression stored in the attribute.

You can check the saxon extension linked in the answer here or have a look at dyn:evaluate from exslt.

Sign up to request clarification or add additional context in comments.

3 Comments

Can I use this extension in .NET (C#)? I could not figure it out from the website.
This might be illuminating: stackoverflow.com/questions/2455910/…
@Tobias, thank you. I ended up writing an extension for XSLT. I was guided by msdn.microsoft.com/en-us/library/tf741884(v=vs.110).aspx.
1

this stylesheet uses EXSLT dynamic, however, this is not working using saxon processor.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dyn="http://exslt.org/dynamic"
    extension-element-prefixes="dyn"
    version="1.1">

    <xsl:template match="root">

    <xsl:for-each select="element">
        <xsl:variable name="visibleCondition" select="@visibilityCondition" />
        <xsl:if test="dyn:evaluate($visibleCondition)='True'">
                SuCcEsS!
        </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

I tried it with XALAN 2.7.1 in http://xsltransform.net/ and it works.

1 Comment

In Saxon you can use the official xsl:evaluate instruction which appears in the XSLT 3.0 draft specification. The saxon:evaluate() extension function also remains available for the time being.

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.