【问题标题】:Removal of SourceNamespace by appending new tags to XML using XSLT通过使用 XSLT 将新标签附加到 XML 来删除 SourceNamespace
【发布时间】:2019-09-16 13:12:48
【问题描述】:

我有一个 xml 作为源,并希望进行以下更改。 1.删​​除第一个根标签 2.添加新的soapenv根标签 3. 从源中删除命名空间。

我可以添加新标签,但是 XSLT1.0 版本没有完成删除。(我已经用 XSLT2.0 实现了它,但我的 XSLT 处理器是 1.0)。我知道我在这里遗漏了一些基本逻辑。有人可以帮帮我吗。非常感谢。

源 XML:

 <?xml version="1.0" encoding="UTF-8"?>
                <ns0:Header xmlns:ns0="http://xyz987.com">
                    <Main>
                        <Parent2>
                            <status>12</status>
                            <status_txt>Helo</status_text>
                        </Parent2>
                        <Parent3>
                            <Child1>112</Child1>
                            <Child2>Hai</Child2>
                        </Parent3>
                        <Parent4>
                            <Child3>Valley</Child3>
                            <Parent5>
                                <Child7>Kind</Child7>
                                <Child8>Pls</Child8>
                            </Parent5>
                        </Parent4>
                    </Main>
                </ns0:Header>

目标 XML:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                    <soapenv:Header/>
                    <soapenv:Body>
                        <Main>
                            <Parent2>
                                <status>12</status>
                                <status_txt>Helo</status_txt>
                            </Parent2>
                            <Parent3>
                                <Child1>112</Child1>
                                <Child2>Hai</Child2>
                            </Parent3>
                            <Parent4>
                                <Child3>Valley</Child3>
                                <Parent5>
                                    <Child7>Kind</Child7>
                                    <Child8>Pls</Child8>
                                </Parent5>
                            </Parent4>
                        </Main>
                    </soapenv:Body>
                </soapenv:Envelope>

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" omit-xml-declaration="yes"/>

                        <xsl:template match="*">

                            <soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
                                <soapenv:Header/>
                                <soapenv:Body>

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

                                </soapenv:Body>
                            </soapenv:Envelope>

                        </xsl:template>
                    </xsl:stylesheet>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    XSLT 1.0 不支持使用copy-namespaces。事实上,你可以只做&lt;xsl:copy-of select="*" /&gt;,但你最终会得到这样的 XML..

    <Main xmlns:ns0="http://xyz987.com">
    

    像这样有一个未使用的命名空间声明实际上不会导致问题。 Main 元素及其所有后代实际上并不在该命名空间中。但是,如果您确实想摆脱它,则可以改用xsl:apply-templates,并使用模板来创建没有附加任何命名空间声明的新元素

    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:copy-of select="@*" />
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
    

    在这种情况下,您需要更改主模板以匹配 /*,以避免冲突。试试这个 XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
    
        <xsl:template match="/*">
            <soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
                <soapenv:Header/>
                <soapenv:Body>
                    <xsl:apply-templates />
                </soapenv:Body>
            </soapenv:Envelope>
        </xsl:template>
    
        <xsl:template match="*">
            <xsl:element name="{local-name()}">
                <xsl:copy-of select="@*" />
                <xsl:apply-templates />
            </xsl:element>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多