Dmitry Ilukhin
Dmitry Ilukhin

Reputation: 498

Conditional expression in XSLT

I need to pass a value to an attribute that based on a condition whether some value is empty or not. If not, an attribute should get this value, if yes, it gets another one.

Currently, I pass these value just in line assuming I get only one of them. For example,

<xsl:template match="/test">
    <Settings Checksum="{//test/metadata[@name='parChecksum']/@data}
        {//test/metadata[@name='root']/metadata[@name='parChecksum']/@data}" />
</xsl:template>

Input XML could be like this:

<test>
    <metadata name="root">
        <metadata name="parChecksum" data="90eee2"/>
    </metadata>
</test>

or this:

<test>
    <metadata name="parChecksum" data="e6f963"/>
</test>

I want to have an expression that selects only one of them, because in case of having both values an attribute will get an erroneus value.

Upvotes: 2

Views: 220

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167676

XPath 2.0 and later have an if (conditionExpression) then expression1 else expression2 expression. It is also possible to create a sequence (expression1, expression2) but only to select the first item (expression1, expression2)[1], in that case if expression1 evaluates to a non-empty item, it is selected, if it evaluates to an empty sequence, the result of expression2 is used.

Upvotes: 1

Related Questions