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 the开发者_运维技巧se 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)
This should do the work:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:key name="atts-by-name" match="@*" use="name()"/>
<xsl:template match="Root">
<xsl:copy>
<xsl:call-template name="merge">
<xsl:with-param name="elements" select="*"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="merge">
<xsl:param name="elements"/>
<xsl:for-each select="$elements">
<xsl:variable name="same-elements" select="$elements[name()=name(current()) and count(@*)=count(current()/@*) and count(@*[. = key('atts-by-name',name())[generate-id(..)=generate-id(current())]])=count(@*)]"/>
<xsl:if test="generate-id($same-elements[1]) = generate-id()">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:call-template name="merge">
<xsl:with-param name="elements" select="$same-elements/*"/>
</xsl:call-template>
</xsl:copy>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The tricky part is the definition of same-elements; indexing attributes by name is mandatory for verifying egality of all attributes.
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.
If you are using XSLT2 you should be able to use the grouping facility. Here is an early tutorial:
http://www.xml.com/pub/a/2003/11/05/tr.html
Here is a later one written by a group who produces excellent tutorials:
http://www.zvon.org/xxl/XSL-Ref/Tutorials/index.html
If you are restricted to XSLT1 it's possible but harder.
If you are still stuck try Dave Pawson's FAQ: http://www.dpawson.co.uk/xsl/sect2/N4486.html
精彩评论