以下是输入和预期的 XML 格式。我尝试了一些模板,但不起作用。
输入XML:
<Root xmlns="http://www.example.com/mdm/readingxml/2011/07">
<child1>child1</child1>
<child2>child2</child2>
<child3>child3</child3>
<child4>child4</child4>
</Root>
预期 XML:
<child1>child1</child1>
<child2>child2</child2>
<child3>child3</child3>
<child4>child3</child4>
使用XSLT-:
<!-- identity template -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<!-- template for the document element -->
<xsl:template match="/*">
<xsl:apply-templates select="node()" />
</xsl:template>
删除根元素非常简单。你可以这样做:
<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="/*">
<xsl:copy-of select="*"/>
</xsl:template>
</xsl:stylesheet>
但是,结果会与您期望的不同,因为复制的子元素与根元素位于相同的默认命名空间中。如果您想将它们移至无命名空间,则必须有效地重命名它们:
<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="/*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>