xslt keep multiple variables in scope depending on a single test

吃可爱长大的小学妹 提交于 2019-12-07 16:25:28

Well, I'd rather process that way :

<xsl:variable name="something">6</xsl:variable>

<xsl:variable name="var.set">
  <xsl:choose>
    <xsl:when test="$something = '6'">
        <foo>x</foo>
        <bar>xx</bar>
        <!-- 50 other variables, to be inserted as tag -->
    </xsl:when>
    <xsl:when test="$something = '7'">
        <foo>y</foo>
        <bar>yy</bar>
        <!-- 50 other variables, to be inserted as tag -->
    </xsl:when>
  </xsl:choose>
</xsl:variable>

The variable var.set will be a nodeset that you will be able to read thanks to the exsl:node-set() extension.

For example to retrieve the value for foo (stored as a node and not as a variable anymore), use something like: <xsl:value-of select="exsl:node-set($var.set)/foo" />. Like this you will handle a single variable, quite as if it were an array of values.

PS: on the root tag of your stylesheet, do not forget to add the exsl namespace declaration xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl"

If you know your mappings beforehand, you can store them in their own file.

So instead of having something like this

<xsl:variable name="var1">
  <xsl:if test="something = 5">x</xsl:if>
</xsl:variable>
<xsl:value-of select="$var1"/>

You could have this

<xsl:value-of select="document('other.xml')/root/scheme[@number = 5]/@value"/>

With this as other.xml

<root>
  <scheme number="5" value="x"/>
  <scheme number="6" value="y"/>
</root>

You'll probably want a more complex other.xml, with various groups of colours for each colour scheme, but the idea would be the same, and avoids tests and variables completely.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!