如何仅复制特定元素及其子元素?所以,不是父元素
XLST文件示例:<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"/>
<header>Some inserted text</header>
<xsl:template match="staff">
<xsl:apply-templates select="subelement"/>
</xsl:template>
<xsl:template match="subelement">
<xsl:copy-of select="*"/>
</xsl:template>
</parent>
示例XML文件:
<?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="1002">
<name>should-2</name>
<role>copy-2</role>
</staff>
</company>
想要的结果:(so, no 'company'元素)
<?xml version="1.0" encoding="UTF-8"?>
<parent><header>Some inserted text</header>
<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="1002">
<name>should-2</name>
<role>copy-2</role>
</staff>
</parent>
结果:
<?xml version="1.0" encoding="UTF-8"?>
<parent><p>Some inserted text</p>
<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="1002">
<name>should-2</name>
<role>copy-2</role>
</staff>
</company>
</parent>
尝试:
XSLT 1.0
<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"/>
<xsl:template match="/company">
<parent>
<header>Some inserted text</header>
<xsl:copy-of select="staff"/>
</parent>
</xsl:template>
</xsl:stylesheet>