gpt4 book ai didi

xslt合并同名多个节点

转载 作者:行者123 更新时间:2023-12-05 08:58:59 26 4
gpt4 key购买 nike

我可以看到类似问题的许多答案,但我似乎无法让它们为我工作。我有一些 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>

我想合并所有“Shapes”节点。要求的输出

<?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 进行任何修改以获取输出?

最佳答案

这应该可以完成工作:

<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的所有子元素,然后复制其余部分。

关于xslt合并同名多个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18514292/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com