<xsl:choose>
<xsl:when test="long convoluted expression">
<xsl:element name="Class">123</xsl:element>
<a lot more xsl:elements>
</xsl:when>
<xsl:when test="next very long expression">
<xml:element name="Class">124</xsl:element>
<a lot more xsl:elements>
</xsl:when>
<tens of more similar xsl:when>
</xsl:choose>
Is there a way to simplify the above code with conditionals? For every class value the objects are given, there follows tens of rows with extra attributes. These attributes form sets according to the value of the class. Class 0-99 has one set of extra tags, class 100-199 a second, forming a maintenance nightmare when one of these extra tag sets change.
I was considering a solution like this:
<xsl:choose>
<xsl:when test="long convoluted expression">
<xml:element name="Class">123</xsl:element>
<xsl:variable name="outputclass" select="123">
</xml:when>
<xsl:when test="next very long expression">
<xml:element name="Class">124</xsl:element>
<xsl:variable name="outputclass" select="124">
</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="$outputclass > 99">
<xml:elements for classes 100-199 here>
</xsl:when>
<xsl:choose>
But of course this fails, as the outputclass variable is not in the same scope. Any way to go around this?
The best solution to this problem is well-known:
Do note:
In order to give values to a variable (
$voutType), the<xsl:choose>instruction must be inside the body of the<xsl:variable>You only need to specify the
<Class>element once — outside of everything else.You don’t have to use
<xsl:element>if the element name is known.