简体   繁体   中英

XSLT: merge nodes with same name recursively

Although there a lot of questions on SO with similar titles, I couldn't find an answer for my specific question.

Suppose I have an xml tree:

<input>
    <a>
        <b>
            <p1/>
        </b>
    </a>
    <a>
        <b>
            <p2/>
        </b>
    </a>
</input>

I want to have this as

<input>
    <a>
        <b>
            <p1/>
            <p2/>
        </b>
    </a>
</input>

The idea behind this transformation is to transform a tree, where a node can have multiple children with the same name to a more 'well-formed' tree where each node can only have one child with the same name. (cf file systems).

I tried to use the grouping-feature of xslt-2 , but I couldn't get the recursion working.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <output>
            <xsl:apply-templates select="*"/>
        </output>
    </xsl:template>

  <!-- this merges the children -->
  <xsl:template name="merge" match="*">

      <xsl:for-each-group select="child::*" group-by="local-name()">
          <xsl:variable name="x" select="current-grouping-key()"/>
          <xsl:element name="{$x}">
              <xsl:copy-of select="current-group()/@*"/>
              <xsl:apply-templates select="current-group()"/>
              <xsl:copy-of select="text()"/>
          </xsl:element>
      </xsl:for-each-group>
  </xsl:template>
</xsl:stylesheet>

I see that the problem is that I'm applying the template separately for each node in current-group() , but I don't see how I can first "join" this set and apply the template en bloc.

I think you can set up a function with the grouping, see http://xsltransform.net/bdxtqM/1 , which does

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
  xmlns:mf="http://example.com/mf"
  exclude-result-prefixes="mf">

  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:function name="mf:merge" as="node()*">
    <xsl:param name="elements" as="element()*"/>
    <xsl:for-each-group select="$elements" group-by="node-name(.)">
        <xsl:copy>
            <xsl:copy-of select="current-group()/@*"/>
            <xsl:sequence select="mf:merge(current-group()/*)"/>
        </xsl:copy>
    </xsl:for-each-group>
  </xsl:function>

  <xsl:template match="/*">
    <xsl:copy>
      <xsl:sequence select="mf:merge(*)"/>
    </xsl:copy>
  </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