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>
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With