Assigning a string to a variable depending on condition in xslt

33,323

Solution 1

You can put any xslt code within an xsl:variable and the result will be assigned to the variable. In this case you could make use of an xsl:if to check your condition

<xsl:variable name="person"> 
    <xsl:if test="pr:all[@pr:name=current()/@cx:name]/pr:properties[@ls:middlename='cengie']">
       <xsl:text>young</xsl:text>
    </xsl:if>
</xsl:variable> 

If you wanted an 'else' case here, you would use xsl:choose instead.

Solution 2

You can use use-when, which applies the template conditionally.

However, it is evaluated at "compile time" of the template.

Check this: https://github.com/wildfly/wildfly/blob/master/testsuite/integration/src/test/xslt/enableTrace.xsl

<xsl:template match="//l:subsystem/l:periodic-rotating-file-handler" use-when="$trace">
    <xsl:choose>
        <xsl:when test="$trace='none'">
            ...
        </xsl:when>
        <xsl:otherwise>
            ...
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Apply that to your code...

Solution 3

<xsl:variable   name='person' 
    select='pr:all/[@pr:name=current()/@cx:name]/pr:properties/(@ls:middlename)'>

</xsl:variable>

This is syntactically illegal XPath -- both 1.0 and 2.0. A location step cannot start with a predicate. The offending substring is: /[.

Another syntax error (this time XML-well-formedness one) is that the <xsl:variable> element quoted above is not closed.

You need to correct this.

Besides this, here is an XSLT 2.0 solution (with the syntax of XPath expression quoted above and of the <xsl:variable> corrected:

In XSLT 2.0:

<xsl:variable name="person" as="xs:string?" select=
  "'young'[current()/pr:all[@pr:name=current()/@cx:name]
                                            /pr:properties
                                               [@ls:middlename='cengie']
          ]"/>
Share:
33,323
harsh
Author by

harsh

Updated on February 09, 2021

Comments

  • harsh
    harsh about 3 years

    I want to assign a value to a variable if a particular attribute returns a particular value. In here I want to assign the value "young" to vaiable "person" if pr:all/[@pr:name=current()/@cx:name]/pr:properties/(@ls:middlename) is "cengie". Is that possible?

    <xsl:variable
      name='person' select='pr:all/[@pr:name=current()/@cx:name]/pr:properties/(@ls:middlename)'>
    </xsl:variable>
    
    • febot
      febot almost 12 years
      XSLT doesn't "assign" and "return", it matches and transforms. Maybe a wording nuance, maybe a paradigm misunderstanding.
    • Michael Kay
      Michael Kay almost 12 years
      XSLT talks of "binding" a variable to a value. The difference between "bind" and "assign" is that a variable is bound to a value as soon as it is declared and remains bound to the same value for as long as it is in scope. Of course, the value that you bind it to can be determined by a conditional expression evaluated at run time.
    • harsh
      harsh almost 12 years
      Thanks for the explanation Michael!
  • febot
    febot almost 12 years
    Well, put it into variable, and that's it - no?
  • Jim Garrison
    Jim Garrison almost 12 years
    @TimC and Ondra answered your question as it is written and generally interpreted. If this is not the answer you need, you are going to have to explain better what you want.
  • harsh
    harsh almost 12 years
    thanks for the answer. can you please tell me what is the value of the variable if the "xsl:if test" is false.
  • Tim C
    Tim C almost 12 years
    If the answer is false, the variable will be empty. Effectively it will be an empty string.
  • Dimitre Novatchev
    Dimitre Novatchev almost 12 years
    @TimC: Have you noticed that the XPath expression in the test attribute is syntactically invalid?
  • harsh
    harsh almost 12 years
    thanks a lot for the suggestion. I changed the expression according to your suggestion. Thanks for pointing that out.