Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a string to a variable depending on condition in xslt

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>
like image 731
harsh Avatar asked Nov 28 '22 09:11

harsh


2 Answers

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.

like image 120
Tim C Avatar answered Dec 09 '22 16:12

Tim C


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...

like image 39
Ondra Žižka Avatar answered Dec 09 '22 16:12

Ondra Žižka