我想复制 xml 的一个节点并将其粘贴到同一级别。
假设我有一个如下所示的 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>
我想复制组 03 并使用 XSL 转换粘贴到与“04”相同的级别(组内)。
预期输出
<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>
最佳答案
在 XSLT 1.0 中,模板匹配中包含变量表达式实际上被视为错误(尽管您可能会发现某些处理器允许这样做)。
但是您可能应该做的是,在匹配 Group
的模板中调用身份模板,然后使用 xsl:if
来决定是否复制它。
试试这个模板
<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
元素。 (此外,当您的 XML 将其作为 MyXml
时,您当前的匹配引用的是 MyXML
。XSLT 区分大小写,因此不会匹配)。
关于java - 如何使用 XLS 转换复制 XML 节点并粘贴到同一级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51652280/