Let's say I have two types of XML elements with text content that can be either 'true' or 'false':
<elementA>true</elementA>,
<elementB>false</elementB>
I would like to create a new element
<result>$content</result>
where variable $content can have 4 types of values based on the two input element values:
true, true -> A
true, false -> B
false, true -> C
false, false -> D
I was trying to solve this with xsl:function, but have no idea how to select the two elements at the same time (I have little experience with xslt). Is it possible to do this kind of transformation?
EDIT
I was experimenting with your second solution, however my main source of problem is that I don't know how to pass the variables to my function. Currently I have the following code:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:function name="transform">
<xsl:param name="p1"></xsl:param>
<xsl:param name="p2"></xsl:param>
<xsl:choose>
<xsl:when test="$p1='true' and $p2='true'">
<orderingType>RND</orderingType>
</xsl:when>
<xsl:when test="$p1='true' and $p2='false'">
<orderingType>FIX</orderingType>
</xsl:when>
<xsl:when test="$p1='false' and $p2='false'">
<orderingType>PREF</orderingType>
</xsl:when>
</xsl:choose>
</xsl:function>
<xsl:template match="//action/*">
<xsl:variable name="var1" select="shuffle"></xsl:variable>
<xsl:variable name="var2" select="limitOn"></xsl:variable>
</xsl:template>
How about:
<xsl:variable name="index" select="2*(elementA='false') + (elementB='false')" />
<result>
<xsl:value-of select="substring('ABCD', $index + 1, 1)" />
</result>
Alternatively, use xsl:choose
and specify each case and its result explicitly within a xsl:when
instruction:
<xsl:choose>
<xsl:when test="elementA='true' and elementB='true'">A</xsl:when>
<xsl:when test="elementA='true' and elementB='false'">B</xsl:when>
<xsl:when test="elementA='false' and elementB='true'">C</xsl:when>
<xsl:otherwise>D</xsl:otherwise>
</xsl:choose>
A template should do it;
<xsl:template match="xml-fragment">
<xsl:variable name="p1">
<xsl:value-of select="elementA"/>
</xsl:variable>
<xsl:variable name="p2">
<xsl:value-of select="elementB"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$p1='true' and $p2='true'">
<orderingType>RND</orderingType>
</xsl:when>
<xsl:when test="$p1='true' and $p2='false'">
<orderingType>FIX</orderingType>
</xsl:when>
<xsl:when test="$p1='false' and $p2='false'">
<orderingType>PREF</orderingType>
</xsl:when>
</xsl:choose>
</xsl:template>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.