xslt合并具有相同名称的多个节点 [英] xslt merge multiple nodes with same name
问题描述
我可以看到许多类似问题的答案,但似乎无法让它们为我工作.我有一些带有相同标签名称的同级元素节点的xml文件.我想使用XSLT合并这些节点.任何帮助将不胜感激.
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.
输入:
<?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是:
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>
我可以使用一个简单的XSLT代码吗,或者我可以对我的xslt进行任何修改以获取输出?
Is there a simple XSLT code I can use, or is there any modification in my xslt I can apply to get the output?
推荐答案
这应该可以完成:
<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>
想法是一次性处理Shapes
的所有子元素,然后复制其余部分.
The idea is to process all the sub-elements of Shapes
separately in one go, and then copy the rest.
这篇关于xslt合并具有相同名称的多个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!