Xml 使用xsl:choose测试xsl:for-each的结果时XSLT失败
我有一个Xml 使用xsl:choose测试xsl:for-each的结果时XSLT失败,xml,xslt,xpath,Xml,Xslt,Xpath,我有一个xsl:for each,它将许多不同的元素组合在一起,然后有条件地处理每个元素。例如: <xsl:for-each select=".//del | .//sup | .//unc | .//gap"> <xsl:choose> <xsl:when test="del"><xsl:text>Output foo del</xsl:text></xsl:when> <xsl:whe
xsl:for each
,它将许多不同的元素组合在一起,然后有条件地处理每个元素。例如:
<xsl:for-each select=".//del | .//sup | .//unc | .//gap">
<xsl:choose>
<xsl:when test="del"><xsl:text>Output foo del</xsl:text></xsl:when>
<xsl:when test="sup"><xsl:text>Output foo sup</xsl:text></xsl:when>
<xsl:when test="unc"><xsl:text>Output foo unc</xsl:text></xsl:when>
<xsl:when test="gap"><xsl:text>Output foo gap</xsl:text></xsl:when>
<xsl:otherwise><xsl:text>For each works, but the tests do not!</xsl:text></xsl:otherwise>
</xsl:choose>
<xsl:for-each>
输出foodel
输出foo sup
输出foo unc
产出差距
对于每一个工程,但测试不!
工作正常,因为它为每个工作输出了大量的,但是测试没有代码>不知何故,我误解了如何编写@test
来捕获每个元素?我认为这与当前的环境有关
非常感谢。我不确定你想要实现什么,但它不应该是这样读的吗
<xsl:for-each select=".//del | .//sup | .//unc | .//gap">
<xsl:choose>
<xsl:when test="self::del"><xsl:text>Output foo del</xsl:text></xsl:when>
<xsl:when test="self::sup"><xsl:text>Output foo sup</xsl:text></xsl:when>
<xsl:when test="self::unc"><xsl:text>Output foo unc</xsl:text></xsl:when>
<xsl:when test="self::gap"><xsl:text>Output foo gap</xsl:text></xsl:when>
<xsl:otherwise><xsl:text>For each works, but the tests do not!</xsl:text></xsl:otherwise>
</xsl:choose>
<xsl:for-each>
输出foodel
输出foo sup
输出foo unc
产出差距
对于每一个工程,但测试不!
或者,您必须声明xmlns=”http://www.w3.org/1999/XSL/Transform“
例如,在中为每个选择甚至,更改为test=“self::del”
,正如另一个答案中所建议的那样,可以解决问题,但XSLT中实现这一点的惯用方法是使用模板规则:
<xsl:apply-templates select=".//*" mode="m"/>
然后
输出foo del
输出foo sup
输出foo unc
产出差距
否则
对不起,这只是我的输入错误-我的实际代码在所有元素中都有xsl:
。我已经更正了上面的代码。你知道为什么我放在@test
中的XPATH没有捕捉到相应的元素吗?试试self::del
等等。在我的答案中插入这个。对于进一步的问题:看到更多示例xml代码会很有帮助,当前结果和预期结果。是否有任何理由需要对嵌套的xsl:choose/xsl:when
节点测试使用xsl:for each
,而不是将节点推送到匹配模板,例如
,然后对其他元素类型使用输出foo del
等等?是,这些都集中在一个物理部分,计算并分配一个脚注编号。
<xsl:template match="del" mode="m">Output foo del</xsl:template>
<xsl:template match="sup" mode="m">Output foo sup</xsl:template>
<xsl:template match="unc" mode="m">Output foo unc</xsl:template>
<xsl:template match="gap" mode="m">Output foo gap</xsl:template>
<xsl:template match="*" mode="m">Otherwise</xsl:template>