我可以看到很多的答案类似的问题,但我似乎不能让他们为我工作。 我有有相同的标记名称一些同级元素节点的一些XML文件。 我想用XSLT合并这些节点。 任何帮助深表感谢。
输入:
<?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>
我想合并所有“形状”的节点。 所需的输出
<?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我想是:
<?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>
但我得到的输出是(:()
<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>
有一个简单的XSLT代码我可以使用,或者是有我的XSLT任何修改我可以申请得到输出?