问题
I can see many answers to similar questions, but I can't seem to get them work for me. I have some xml files with some sibling element nodes having same tag name. I want to merge these nodes using XSLT. Any help would be deeply appreciated.
Input:
<?xml version="1.0"?>
<Screen>
<Shapes>
<Triangle id="tri1">
<color>red</color>
<size>large</size>
</Triangle>
</Shapes>
<Shapes>
<Rectangle id="rec1">
<color>blue</color>
<size>medium</size>
</Rectangle>
</Shapes>
<Shapes>
<Circle id="cir1">
<color>green</color>
<size>small</size>
</Circle>
</Shapes>
<Shapes>
<Square id="sqr1">
<color>yellow</color>
<size>large</size>
</Square>
</Shapes>
<Device>
<Name>peg</Name>
<type>X11</type>
</Device>
<Utilities>
<Software>QT</Software>
<Platform>Linux</Platform>
</Utilities>
</Screen>
I want to merge all "Shapes" nodes. Required Output
<?xml version="1.0"?>
<Screen>
<Shapes>
<Triangle id="tri1">
<color>red</color>
<size>large</size>
</Triangle>
<Rectangle id="rec1">
<color>blue</color>
<size>medium</size>
</Rectangle>
<Circle id="cir1">
<color>green</color>
<size>small</size>
</Circle>
<Square id="sqr1">
<color>yellow</color>
<size>large</size>
</Square>
</Shapes>
<Device>
<Name>peg</Name>
<type>X11</type>
</Device>
<Utilities>
<Software>QT</Software>
<Platform>Linux</Platform>
</Utilities>
</Screen>
XSLT I tried was:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" />
<xsl:template match="Shapes">
<xsl:if test="not(preceding-sibling::*[local-name() = 'Shapes'])">
<Shapes>
<xsl:apply-templates select="node() | @*" />
<xsl:apply-templates select="following-sibling::*[local-name() = 'Shapes']" />
</Shapes>
</xsl:if>
<xsl:if test="preceding-sibling::*[local-name() = 'Shapes']">
<xsl:apply-templates select="node() | @*" />
</xsl:if>
</xsl:template>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
But the output I got was ( :( )
<Screen>
<Shapes>
<Triangle id="tri1">
<color>red</color>
<size>large</size>
</Triangle>
<Rectangle id="rec1">
<color>blue</color>
<size>medium</size>
</Rectangle>
<Circle id="cir1">
<color>green</color>
<size>small</size>
</Circle>
<Square id="sqr1">
<color>yellow</color>
<size>large</size>
</Square>
</Shapes>
<Rectangle id="rec1">
<color>blue</color>
<size>medium</size>
</Rectangle>
<Circle id="cir1">
<color>green</color>
<size>small</size>
</Circle>
<Square id="sqr1">
<color>yellow</color>
<size>large</size>
</Square>
<Device>
<Name>peg</Name>
<type>X11</type>
</Device>
<Utilities>
<Software>QT</Software>
<Platform>Linux</Platform>
</Utilities>
</Screen>
Is there a simple XSLT code I can use, or is there any modification in my xslt I can apply to get the output?
回答1:
This should do the job:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<Shapes>
<xsl:copy-of select="Shapes/*"/>
</Shapes>
<xsl:apply-templates select="*[name()!='Shapes']"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The idea is to process all the sub-elements of Shapes
separately in one go, and then copy the rest.
来源:https://stackoverflow.com/questions/18514292/xslt-merge-multiple-nodes-with-same-name