Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - Recursively combine identical sibling elements in XSLT

How can I merge all sibling elements with the same name and the same attributes into a single element using XSLT? The transformation should also be applied recursively to children of elements that are being merged. This is the source document:

<?xml version="1.0"?>
<Root>
  <Element id="UniqueId1">
    <SubElement1/>
    <SubElement2>
      <LeafElement1/>
    </SubElement2>
  </Element>
  <Element id="UniqueId1">
    <SubElement2>
      <LeafElement1/>
      <LeafElement2/>
    </SubElement2>
    <SubElement3/>
  </Element>
  <Element id="UniqueId2">
    <SubElement1/>
    <SubElement4/>
  </Element>    
</Root>

It should be transformed to:

<?xml version="1.0"?>
<Root>
  <Element id="UniqueId1">
    <SubElement1/>
    <SubElement2>
      <LeafElement1/>
      <LeafElement2/>
    </SubElement2>
    <SubElement3/>
  </Element>
  <Element id="UniqueId2">
    <SubElement1/>
    <SubElement4/>
  </Element>    
</Root>

Any elements with the same name and attributes are combined into one element. Then, their children are inspected. If any of them have the same name and same attributes, they are combined. This transformation is applied recursively to all elements.

Edit: To clarify, all of these conditions must be true for two elements to be merged.

  • They have the same element name
  • They have the same attributes
  • The values of each corresponding attribute are the same
  • They are siblings (applied recursively, so any identical parent elements are merged and combined before their children are considered)

These elements are identical and should be merged:

  • <Item/> and <Item/> (same name, same attributes)
  • <Item Attr="foo"/> and <Item Attr="foo"/> (same name, same attributes)

These elements are not identical and shouldn't be merged:

  • <Item/> and <SubItem/> (different name)
  • <Item Attr="foo"/> and <Item/> (different attributes)
  • <Item Attr="foo"/> and <Item Attr="bar"/> (different attribute values)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The easiest way I can think of to do this is to parse all elements with the same ID when the first element with that ID is encountered. Which would look something like this:

<xsl:variable name="curID" select="@id"/>
<xsl:if test="count(preceding-sibling::*[@id=$curID])=0">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:for-each select="following-sibling::*[@id=$curID]">
      <xsl:apply-templates select="@*"/>
    </xsl:for-each>
    <xsl:apply-templates select="node()"/>
    <xsl:for-each select="following-sibling::*[@id=$curID]">
      <xsl:apply-templates select="node()"/>
    </xsl:for-each>
  </xsl:copy>
</xsl:if>

This is off the top of my head, so it may need a bit of tweaking.

To get this to work recursivly is a bit more of a problem. You will need to parse the SubElement2 in the second Element when you are processing the SubElement2 in the first Element tag. This will get rather convoluted to handle arbitrary depth.
I don't know your particular use case, but the simplist answer may be to run the above transform repeatedly, until the result is the same as the input.

Expanding the if statement to fire for elements with the same name as well should be easy.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...