xslt - 无法使用 XSLT/XPath 2.0 正确获取调试输出

标签 xslt xslt-2.0 xpath-2.0

我有一个像这样的 XML:

<?xml version="1.0" encoding="UTF-8"?>

<Section>
    <Chapter>
        <Cell colname="1">
            <Value>A</Value>
        </Cell>
        <Cell colname="2">
            <MyValue>AAA</MyValue>
            <MyValue>BBB</MyValue>
        </Cell>
        <Cell colname="3">
            <MyCar>Honda</MyCar>
        </Cell>
    </Chapter>
    <Chapter>
        <Cell colname="1">
            <Value>C</Value>
        </Cell>
        <Cell colname="2">
            <MyValue>CCC</MyValue>
        </Cell>
        <Cell colname="3">
            <MyCar>Toyota</MyCar>
        </Cell>
    </Chapter>
</Section>

我有一个像这样的 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="Section/Chapter"/>
    </xsl:template>

    <xsl:template match="Chapter">
        <xsl:apply-templates select="./Cell/MyValue"/>
    </xsl:template>

    <xsl:template match="MyValue">
        <xsl:message>
            <xsl:value-of select="../../Cell/@colname"/>
            <xsl:value-of select="../../Cell[@colname='1']/Value"/>
            <xsl:value-of select="."/>
            <xsl:value-of select="../../Cell[@colname='3']/MyCar"/>
        </xsl:message>
    </xsl:template>

    <xsl:template match="text()" />

</xsl:stylesheet>

问题是调试输出显示为 1 2 3 A AAA Honda 1 2 3 A BBB Honda 1 2 3 C CCC Toyota。我想要 1 A 2 AAA 3 本田 1 A 2 BBB 3 本田 1 C 2 CCC 3 丰田。基本上正确获取属性 colname 的值。

几个问题:

  1. 我做错了什么?
  2. 有一个序列 1 2 3 生成,为什么?。

TIA,

约翰

最佳答案

So several questions:

  1. What did I do wrong ?.

您没有准确指定必要的表达式。

  1. There is a sequence 1 2 3 generated why ?.

因为 ../../Cell/@colname 选择每个 Cell 子项的 colname 属性当前节点的祖父Chapter

解决方案:

使用:

<xsl:template match="MyValue">
    <xsl:message>
        <xsl:value-of select="../../Cell[1]/@colname"/>
        <xsl:value-of select="../../Cell[@colname='1']/Value"/>
        <xsl:value-of select="../@colname"/>
        <xsl:value-of select="."/>
        <xsl:value-of select="../../Cell[3]/@colname"/>
        <xsl:value-of select="../../Cell[@colname='3']/MyCar"/>
    </xsl:message>
</xsl:template>

调试输出正是想要的:

1A2AAA3Honda
1A2BBB3Honda
1C2CCC3Toyota

您还可以对上一个问题的答案稍作修改:

<xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>

     <xsl:template match="Chapter">
      <xsl:apply-templates select="Cell[1]"/>
     </xsl:template>

     <xsl:template match="Cell">
      <xsl:param name="pText" as="xs:string*"/>

      <xsl:apply-templates select="*[1]">
       <xsl:with-param name="pText" select="$pText"/>
      </xsl:apply-templates>
     </xsl:template>

     <xsl:template match="Cell/*">
      <xsl:param name="pText" as="xs:string*"/>

      <xsl:variable name="vText" as="xs:string*"
       select="$pText, ../@colname, string(.)"/>

      <xsl:sequence select=
       "$vText
          [not(current()/../following-sibling::Cell)]"/>
      <xsl:apply-templates select="../following-sibling::Cell[1]">
        <xsl:with-param name="pText" select="$vText"/>
      </xsl:apply-templates>
      <xsl:apply-templates select="following-sibling::*">
        <xsl:with-param name="pText" select="$pText"/>
      </xsl:apply-templates>
     </xsl:template>
</xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<Section>
    <Chapter>
        <Cell colname="1">
            <Value>A</Value>
        </Cell>
        <Cell colname="2">
            <MyValue>AAA</MyValue>
            <MyValue>BBB</MyValue>
        </Cell>
        <Cell colname="3">
            <MyCar>Honda</MyCar>
        </Cell>
    </Chapter>
    <Chapter>
        <Cell colname="1">
            <Value>C</Value>
        </Cell>
        <Cell colname="2">
            <MyValue>CCC</MyValue>
        </Cell>
        <Cell colname="3">
            <MyCar>Toyota</MyCar>
        </Cell>
    </Chapter>
</Section>

产生了想要的正确结果:

1 A 2 AAA 3 Honda 1 A 2 BBB 3 Honda 1 C 2 CCC 3 Toyota

关于xslt - 无法使用 XSLT/XPath 2.0 正确获取调试输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9456741/

相关文章:

java - 我无法提取正确的 xpath ,可以帮助我

xml - XSLT 输出格式 : removing line breaks, 和删除元素的空白输出行,同时保持缩进

datetime - 将日期时间转换为 xslt 中的 unix 纪元

XSLT 查找和替换回车

xml - xslt中count属性的范围是多少?

xslt - 将每个组用于高性能XSLT

selenium - 单击基于表列值的复选框 - IE Webdriver Selenium

c# - 如何在 C# 中使用像 exists() 这样的 XPath 函数?

xslt - 如何识别 XSLT 输入或 XPath 中的行号?

XML 和 XSLT : need it to sort only certain child nodes