简体   繁体   中英

Merge specific elements but keep document order using XSLT 1.0

Given the following document:

<doc>
  <a>a</a>
  <b>1</b>
  <b>2</b>
  <b>3</b>
  <c>c</c>
</doc>

I want this to be turned into:

<doc>
  <a>a</a>
  <b>1,2,3</b>
  <c>c</c>
</doc>

What I have so far is (mostly taken from another post here at SO):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

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

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

</xsl:stylesheet>

This creates the following result:

<doc><a>a</a><c>c</c><b>1,2,3</b></doc>

But I'm struggling to find a solution that keeps the document order intact. Any run of <b> elements should be replaced by a single element containing the text of the original elements as a comma separated list. The other elements should not be reordered.

Why don't you do simply:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/doc">
    <xsl:copy>
        <xsl:copy-of select="a"/>
        <b>
            <xsl:for-each select="b">
                <xsl:value-of select="."/>
                <xsl:if test="position() !=last()">,</xsl:if>
            </xsl:for-each>
        </b>
        <xsl:copy-of select="c"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

This is assuming you know the structure of the incoming XML and can enumerate the elements before b and after it separately. Otherwise you'd have to do something like:

<xsl:template match="/doc">
    <xsl:copy>
        <xsl:apply-templates select="*[not(self::b or preceding-sibling::b)]"/>
        <b>
            <xsl:for-each select="b">
                <xsl:value-of select="."/>
                <xsl:if test="position() !=last()">,</xsl:if>
            </xsl:for-each>
        </b>
        <xsl:apply-templates select="*[not(self::b or following-sibling::b)]"/>
    </xsl:copy>
</xsl:template>

You can use sibling recursion in a different mode:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="b[not(preceding-sibling::*[1][self::b])]">
        <xsl:copy>
            <xsl:apply-templates select="." mode="merge"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="b[preceding-sibling::*[1][self::b]]"/>

    <xsl:template match="b" mode="merge">
        <xsl:value-of select="."/>
        <xsl:variable name="next" select="following-sibling::*[1][self::b]"/>
        <xsl:if test="$next">
            <xsl:text>,</xsl:text>
            <xsl:apply-templates select="$next" mode="merge"/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM