Xslt 使用<;从结果树片段构建节点集变量;xsl:choose>;
是否可以使用Xslt 使用<;从结果树片段构建节点集变量;xsl:choose>;,xslt,msxml,node-set,Xslt,Msxml,Node Set,是否可以使用xsl:choose(用于MSXML引擎)从rtf创建节点集变量 我有以下结构: <xsl:choose> <xsl:when test="function-available('msxsl:node-set')"> <xsl:variable name="colorList" select="msxsl:node-set($std:colorList)"/> <xsl:for-each select=
xsl:choose
(用于MSXML引擎)从rtf创建节点集变量
我有以下结构:
<xsl:choose>
<xsl:when test="function-available('msxsl:node-set')">
<xsl:variable name="colorList" select="msxsl:node-set($std:colorList)"/>
<xsl:for-each select="$colorList/color">
tr.testid<xsl:value-of select="@testid"/> {
color:<xsl:value-of select="."/>;
}
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="colorList" select="$std:colorList"/>
<xsl:for-each select="$colorList/color">
tr.testid<xsl:value-of select="@testid"/> {
color:<xsl:value-of select="."/>;
}
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
但这并不能正常工作:MSXML抱怨colorList
不是节点集,因此不能在xsl:for each
中使用它
XSL transformation failed due to following error:
Expression must evaluate to a node-set.
-->$colorList<--/color
不幸的是,在XSLT1.0中,当xsl:variable包含指令而不是select属性时,结果总是RTF。因此,您将RTF转换为节点集的谨慎尝试将一无所获,因为它又被直接转换回来了 恐怕没有干净的解决方法(当然除了使用XSLT2.0)。我建议如下构造代码:
<xsl:choose>
<xsl:when test="function-available('msxsl:node-set')">
<xsl:apply-templates select="msxsl:node-set($std:colorList)/color" mode="z"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="$std:colorList/color" mode="z"/>
</xsl:otherwise>
</xsl:choose>
<xsl:template match="color" mode="z">
tr.testid<xsl:value-of select="@testid"/> {
color:<xsl:value-of select="."/>;
}
</xsl:template>
特斯蒂德{
颜色:;
}
为了记录在案,我在下面添加了最终解决方案。这与Michael的建议略有不同,即在应用模板之前向变量添加RTF副本。这是因为在xsl解析过程中,MSXML仍然会出错(显然,它会检查apply templates select值,并得出结论,当它是RTF而不是节点集时,它是不正确的。而且,正如Michael所说,xsl:variable select属性只会执行以下操作:将RTF转换为节点集
<xsl:choose>
<xsl:when test="function-available('msxsl:node-set')">
<xsl:apply-templates select="msxsl:node-set($std:colorList)/color" mode="addTRclassToCSS"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="colorList" select="$std:colorList"/>
<xsl:apply-templates select="$colorList" mode="addTRclassToCSS"/>
</xsl:otherwise>
</xsl:choose>
<xsl:template match="color" mode="addTRclassToCSS">
tr.testid<xsl:value-of select="@testid"/> {
color:<xsl:value-of select="."/>;
}
</xsl:template>
特斯蒂德{
颜色:;
}
对我来说已经足够好了!或者更确切地说,我将把这一点纳入我对坚持使用XSLT 1.0:-)的用户的回答中。Hm遗憾-给出了与以前相同的错误-显然,馈送到模板的选择值也必须是一个节点集…因此我做了与以前相同的技巧:首先将$std:colorList复制到一个新变量,然后将其输入模板。
<xsl:choose>
<xsl:when test="function-available('msxsl:node-set')">
<xsl:apply-templates select="msxsl:node-set($std:colorList)/color" mode="z"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="$std:colorList/color" mode="z"/>
</xsl:otherwise>
</xsl:choose>
<xsl:template match="color" mode="z">
tr.testid<xsl:value-of select="@testid"/> {
color:<xsl:value-of select="."/>;
}
</xsl:template>
<xsl:choose>
<xsl:when test="function-available('msxsl:node-set')">
<xsl:apply-templates select="msxsl:node-set($std:colorList)/color" mode="addTRclassToCSS"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="colorList" select="$std:colorList"/>
<xsl:apply-templates select="$colorList" mode="addTRclassToCSS"/>
</xsl:otherwise>
</xsl:choose>
<xsl:template match="color" mode="addTRclassToCSS">
tr.testid<xsl:value-of select="@testid"/> {
color:<xsl:value-of select="."/>;
}
</xsl:template>