首页 > 解决方案 > 移除 xml 的命名空间

问题描述

我正在低于 XML 作为源,必须通过添加段进行转换。但也必须删除根节点中添加的命名空间。但无法删除命名空间。

有人可以分享我在哪里添加到 XSLT。

源 XML:

 <?xml version="1.0" encoding="UTF-8"?>
                    <Header 
                      xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                        <Main>
                            <Parent2>
                                <status>12</status>
                                <statusmsg>Helo</statusmsg>
                            </Parent2>
                            <Parent3>
                                <Child1>112</Child1>
                                <Child2>Hai</Child2>
                            </Parent3>
                            <Parent4>
                                <Child3>Valley</Child3>
                                <Parent5>
                                    <Child7>Kind</Child7>
                                    <Child8>Pls</Child8>
                                </Parent5>
                            </Parent4>
                        </Main>
                    </Header>

目标 XML:

 <Header>
                        <Main Mainattribute="1">
                            <Parent2 childattribute="1">
                                <status>12</status>
                                <statusmsg>Helo</statusmsg>
                            </Parent2>
                            <Parent3 childattribute="1">
                                <Child1>112</Child1>
                                <Child2>Hai</Child2>
                            </Parent3>
                            <Parent4 childattribute="1">
                                <Child3>Valley</Child3>
                                <Parent5>
                                    <Child7>Kind</Child7>
                                    <Child8>Pls</Child8>
                                </Parent5>
                            </Parent4>
                        </Main>
                    </Header>

XSLT 从以下链接尝试: 从第 4 个父节点向 XML 文件的所有父节点填充属性和值

        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
            <xsl:output method="xml" indent="yes"/>
            <xsl:template match="Main">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:attribute name="Mainattribute"><xsl:value-of select="1"/></xsl:attribute>
                    <xsl:apply-templates mode="parent_mode"/>
                </xsl:copy>
            </xsl:template>
            <xsl:template match="*" mode="parent_mode">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:attribute name="childattribute"><xsl:value-of select="1"/></xsl:attribute>
                    <xsl:apply-templates/>
                </xsl:copy>
            </xsl:template>
            <xsl:template match="*">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:apply-templates/>
                </xsl:copy>
            </xsl:template>
        </xsl:stylesheet>

标签: xmlxsltnamespaces

解决方案


XSLT 2.0 有一种更简单的方法,即使用

<xsl:copy-of select="/" copy-namespaces="no"/>

它执行深层复制删除所有未使用的命名空间。


推荐阅读