拉風(fēng)的咖菲貓
2021-06-21 16:09:38
我想復(fù)制一個 xml 的節(jié)點并將其粘貼到同一級別??紤]我有一個 xml,如下所示。<MyXml> <system> <Groups> <Group id="01" check="true"> <name>Value</name> <age>test</age> </Group> <Group id="02" check="true"> <name>Value</name> <age>test</age> </Group> <Group id="03" check="true"> <name>Value</name> <age>test</age> </Group> </Groups> </system></MyXml>我想復(fù)制組 03 并使用 XSL 轉(zhuǎn)換粘貼到與“04”相同的級別(組內(nèi))。預(yù)期產(chǎn)出<MyXml> <system> <Groups> <Group id="01" check="true"> <name>Value</name> <age>test</age> </Group> <Group id="02" check="true"> <name>Value</name> <age>test</age> </Group> <Group id="03" check="true"> <name>Value</name> <age>test</age> </Group> <Group id="04" check="true"> <name>Value</name> <age>test</age> </Group> </Groups> </system></MyXml>有人可以幫忙完成相同的 XSL 樣式表。不確定下面的 xsl 是否正確。提前致謝。<?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" indent="yes" omit-xml-declaration="yes"/><xsl:param name="groupId" /><xsl:param name="newGroupId" /><xsl:template match="node()|@*" name="identity"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template><xsl:template match="MyXML/system/Groups/Group[@id=$groupId]" > <xsl:copy> <xsl:apply-templates select="@*|node()"/> <!--Wanted to do something for pasting the copied node and changing the id value with new Group Id.--> </xsl:copy></xsl:template></xsl:stylesheet>
1 回答

慕碼人2483693
TA貢獻1860條經(jīng)驗 獲得超9個贊
在 XSLT 1.0 中,在模板匹配中包含變量表達式實際上被認為是錯誤的(盡管您可能會發(fā)現(xiàn)某些處理器允許這樣做)。
但是你可能應(yīng)該做的,就是在模板匹配中調(diào)用身份模板Group,然后有一個xsl:if決定是否復(fù)制它。
試試這個模板
<xsl:template match="Group" >
<xsl:call-template name="identity" />;
<xsl:if test="@id = $groupId">
<group id="{$newGroupId}">
<xsl:apply-templates select="@*[name() != 'id']|node()"/>
</group>
</xsl:if>
</xsl:template>
請注意,您不需要Group模板匹配中的完整路徑,除非Group您不想匹配其他級別的元素。(此外,MyXML當(dāng)您的 XML 將其設(shè)為MyXml.XSLT時,您當(dāng)前的匹配指的是 。XSLT 區(qū)分大小寫,因此不會匹配)。
添加回答
舉報
0/150
提交
取消