xml - 使用xsl:choose从xsl:for-each测试结果时XSLT失败

标签 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: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>


<xsl:for-each>正常工作,因为它输出很多<otherwise> For each works, but the tests do not!我以某种方式误解了如何编写@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>


或者,您必须声明xmlns="http://www.w3.org/1999/XSL/Transform"例如在choose甚至for-each之内。

关于xml - 使用xsl:choose从xsl:for-each测试结果时XSLT失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47954303/

相关文章:

xml - 使用变量中的其他节点在 XSL apply-templates 节点中排序 (XSLT 1.0)

python - xml 文件上的 XPath 表达式

xml - HBase REST 过滤器 ( SingleColumnValueFilter )

xml - XSLT - 不能在复杂的图形表示中只复制一次元素

html - 将 html 有序列表转换为 xml

Java:遍历目录中的 HTML 文件

java - 使用 Java DOM Parser 解析具有复杂结构的 xml 文件中的子元素

java附加到文件

html - 使用 Perl XML::XSLT 生成 OUTLOOK 有问题的 HTML

.net - 获取每个 HTML 表格行的第一个和第二个单元格