2

我有一个这样的 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 Honda 1 A 2 BBB 3 Honda 1 C 2 CCC 3 Toyota。基本上正确获取属性 colname 的值。

所以几个问题:

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

TIA,

约翰

4

2 回答 2

2

所以几个问题:

  1. 我做错了什么 ?。

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

  1. 有一个序列 1 2 3 生成为什么?

因为../../Cell/@colname选择当前节点的每个子节点的colname属性grandparent 。 CellChapter

解决方案

使用

<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
于 2012-02-26T22:04:18.237 回答
0

以下产生所需的结果:

  1. 你得到 1 2 3 因为 ../../Cell/@colname 匹配当前章节中的所有三个单元格。这就是为什么我将其更改为 Cell[1]
  2. 我不确定 colnames 应该是如何“硬编码”的,例如使用 colname 3 来查找 colname 的值 3 似乎很奇怪。这就是为什么我只是将其输出为文本

?xml 版本=“1.0”编码=“UTF-8”?>

<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[1]/@colname"/>
        <xsl:value-of select="../../Cell[@colname='1']/Value"/>
        <xsl:value-of select="../@colname"/>
        <xsl:value-of select="."/>
        <xsl:text>3</xsl:text>
        <xsl:value-of select="../../Cell[@colname='3']/MyCar"/>
    </xsl:message>
</xsl:template>

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

</xsl:stylesheet>
于 2012-02-26T21:28:29.533 回答