XSLT将同名兄弟节点的值合并/连接成单个节点

时间:2012-09-25 15:15:29

标签: xslt merge siblings

输入xml

<catalog>
    <product id="1">
        <name>abc</name>
        <category>aaa</category>
        <category>bbb</category>
        <category>ccc</category>
    </product>
    <product id="2">
        <name>cde</name>
        <category>aaa</category>
        <category>bbb</category>
    </product>
</catalog>

预期输出xml

<products>
    <product>
        <id>1</id>
        <name>abc</name>
        <category>aaa,bbb,ccc</category>
    </product>
    <product>
        <id>2</id>
        <name>cde</name>
        <category>aaa,bbb</category>
    </product>
</products>

转换的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/catalog">
        <products>
            <xsl:for-each select="product">
                <product>
                    <id><xsl:value-of select="@id"/></id>
                    <name><xsl:value-of select="name"/></name>
                    <category><xsl:value-of select="category" /></category>
                </product>
            </xsl:for-each>
        </products>
    </xsl:template>
</xsl:stylesheet>

实际输出xml :(

<products>
    <product>
        <id>1</id>
        <name>abc</name>
        <category>aaa</category>
    </product>
    <product>
        <id>2</id>
        <name>cde</name>
        <category>aaa</category>
    </product>
</products>

在每个'产品'下通过名称'category'循环遍历所有兄弟节点并将其合并/连接到由逗号分隔的单个节点中所需的代码。每个产品的“类别”数量各不相同,因此计数未知。

3 个答案:

答案 0 :(得分:8)

使用这个方便的连接调用模板here,这就变得如此简单:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/catalog">
        <products>
            <xsl:for-each select="product">
                <product>
                    <id>
                        <xsl:value-of select="@id"/>
                    </id>
                    <name>
                        <xsl:value-of select="name"/>
                    </name>
                    <category>
                        <xsl:call-template name="join">
                            <xsl:with-param name="list" select="category" />
                            <xsl:with-param name="separator" select="','" />
                        </xsl:call-template>
                    </category>
                </product>
            </xsl:for-each>
        </products>
    </xsl:template>

    <xsl:template name="join">
        <xsl:param name="list" />
        <xsl:param name="separator"/>

        <xsl:for-each select="$list">
            <xsl:value-of select="." />
            <xsl:if test="position() != last()">
                <xsl:value-of select="$separator" />
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

输出:

<products>
  <product>
    <id>1</id>
    <name>abc</name>
    <category>aaa,bbb,ccc</category>
  </product>
  <product>
    <id>2</id>
    <name>cde</name>
    <category>aaa,bbb</category>
  </product>
</products>

答案 1 :(得分:6)

在XSLT 2.0中,您只需对代码进行一次小的更改:

<category><xsl:value-of select="category" separator=","/></category>

请注意,如果您需要XSLT 1.0解决方案,最好这样说。某些环境中的某些人被卡在1.0上,但很多人都没有。

答案 2 :(得分:3)

这是另一个XSLT 1.0解决方案。

当这个XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="product">
    <xsl:copy>
      <xsl:apply-templates select="*[not(self::category)]" />
      <category>
        <xsl:apply-templates select="category/text()" />
      </category>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="category/text()">
    <xsl:if test="position() &gt; 1">,</xsl:if>
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

...适用于OP的原始XML:

<catalog>
  <product id="1">
    <name>abc</name>
    <category>aaa</category>
    <category>bbb</category>
    <category>ccc</category>
  </product>
  <product id="2">
    <name>cde</name>
    <category>aaa</category>
    <category>bbb</category>
  </product>
</catalog>

...产生了所需的结果:

<?xml version="1.0"?>
<catalog>
  <product>
    <name>abc</name>
    <category>aaa,bbb,ccc</category>
  </product>
  <product>
    <name>cde</name>
    <category>aaa,bbb</category>
  </product>
</catalog>

<强>解释

  • 第一个模板 - Identity Template - 匹配所有节点和属性,并按原样将它们复制到结果文档中。
  • 第二个模板通过创建新的<category>元素并处理文档当前位置中每个<category>元素的文本子项来覆盖标识模板。
  • 最终模板根据需要输出文本值和逗号。