仅用其他名称替换根元素-XSLT

时间:2019-02-13 08:47:57

标签: xml xslt

我对XSLT进行了微小的更改,因为我想用其他名称替换Top根节点。我已经尝试在XSLT下面添加新的根,但是无法删除输入xml的初始根。有人可以让我知道我在这里想念的东西吗。谢谢。

下面是尝试过的XSLT代码。

                     <?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" encoding="UTF-8"/>

                        <xsl:template match="root">
                            <ns:NewParent xmlns:ns="http://test.com/user">
                                <xsl:copy-of select="." />
                            </ns:NewParent>
                        </xsl:template>

                        <xsl:template match="@*|node()">
                            <xsl:copy>
                                <xsl:apply-templates select="@*|node()"/>
                            </xsl:copy>
                        </xsl:template>

输入XML:

                    <root>
                        <root>
                            <a>TestA</a>
                            <b>Testb</b>
                            <c>Testc</c>
                        </root>
                        <root>
                            <a>TestA1</a>
                            <b>Testb1</b>
                            <l>Testl1</l>
                        </root>
                        <root>
                            <a>TestA12</a>
                            <b>Testb12</b>
                            <l>Testl2</l>
                        </root>
                    </root>

输出/所需的XML:

                    <ns:NewParent xmlns:ns="http://test.com/user">
                        <root>
                            <a>TestA</a>
                            <b>Testb</b>
                            <c>Testc</c>
                        </root>
                        <root>
                            <a>TestA1</a>
                            <b>Testb1</b>
                            <l>Testl1</l>
                        </root>
                        <root>
                            <a>TestA12</a>
                            <b>Testb12</b>
                            <l>Testl2</l>
                        </root>
                    </ns:NewParent>

1 个答案:

答案 0 :(得分:1)

匹配root的模板执行<xsl:copy-of select="." />,它复制节点本身以及所有子节点。您应该改为<xsl:copy-of select="node()" />

<xsl:template match="root">
    <ns:NewParent xmlns:ns="http://test.com/user">
        <xsl:copy-of select="node()" />
    </ns:NewParent>
</xsl:template>

请注意,在这种情况下,您不需要身份模板。仅此模板即可。如果确实要使用身份模板,或者需要转换其他节点,请改用xsl:apply-templates

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="UTF-8"/>

    <xsl:template match="/root">
        <ns:NewParent xmlns:ns="http://test.com/user">
            <xsl:apply-templates />
        </ns:NewParent>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

请注意,模板现在如何与/root匹配,因此它仅与顶级root元素匹配,而不与子元素匹配。

相关问题