除了一个元素之外,我如何复制元素的所有子元素?我研究了几个例子,但这个解决方案在我的情况下显然行不通。
示例输入:
<?xml version="1.0" encoding="utf-8"?>
<company>
<staff attrib="select" id="1001">
<name>should-1</name>
<role>copy-1</role>
</staff>
<staff id="1002">
<name>should-3</name>
<role>not-copy-3</role>
</staff>
<staff attrib="select" id="1003">
<name>should-2</name>
<role>copy-2</role>
</staff>
</company>预期产出:因此,不包括:
<?xml version="1.0" encoding="utf-8"?>
<staff attrib="select" id="1001">
<name>should-1</name>
</staff>
<staff id="1002">
<name>should-3</name>
</staff>
<staff attrib="select" id="1003">
<name>should-2</name>
</staff>我的XSLT脚本:尝试了很多次,但没有成功。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- try 1 -->
<xsl:template match="/company/staff/role" />
<!-- try 2 -->
<xsl:template match="role" />
<!-- try 3 -->
<xsl:template match="//role" />
<xsl:template match="/company">
<parent>
<xsl:copy-of select="staff"/>
</parent>
</xsl:template>发布于 2022-07-14 19:39:45
你可以这样做:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- Remove all role elements -->
<xsl:template match="role"/>
<!-- Identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>标识转换复制所有内容,角色模板捕获所有角色元素并对它们不做任何操作,从而有效地删除它们。
https://stackoverflow.com/questions/72985396
复制相似问题