Assigning a string to a variable depending on condition in xslt
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']
]"/>
harsh
Updated on February 09, 2021Comments
-
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 almost 12 yearsXSLT doesn't "assign" and "return", it matches and transforms. Maybe a wording nuance, maybe a paradigm misunderstanding.
-
Michael Kay almost 12 yearsXSLT 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 almost 12 yearsThanks for the explanation Michael!
-
-
febot almost 12 yearsWell, put it into variable, and that's it - no?
-
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 almost 12 yearsthanks for the answer. can you please tell me what is the value of the variable if the "xsl:if test" is false.
-
Tim C almost 12 yearsIf the answer is false, the variable will be empty. Effectively it will be an empty string.
-
Dimitre Novatchev almost 12 years@TimC: Have you noticed that the XPath expression in the
test
attribute is syntactically invalid? -
harsh almost 12 yearsthanks a lot for the suggestion. I changed the expression according to your suggestion. Thanks for pointing that out.