从xml中删除空元素和属性,但使用xslt复制所有namepsce

时间:2017-05-22 08:34:52

标签: javascript java xml xslt

我在xml下面并尝试删除空的xml元素和属性。但必须将所有命名空间复制到xml中。我使用的XSLT工作正常。但我遇到的唯一问题是,它还从xml中删除RewriteCond %{HTTP_HOST} ^(www\.)?\.example.com RewriteRule ^ cn.example.com [R=301] 命名空间。不知道为什么它已从输出xml中删除。有人可以帮我解决这个问题。

XML

xmlns:temp-root="http://xx.xxxxx.com"

XSLT

<?xml version='1.0' encoding='utf-8'?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Body>
            <temp-root:xxxxx_update xmlns:temp-root="http://xx.xxxxx.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <firstname>Kevin</firstname>
                <Status>false</Status>
                <lastname>John</lastname>
                <balance>
                    <tn:balance_InnerSet xmlns:tn="xxxxxxxxxxxxx">
                        <tn:balancesheet>
                            <tn:account>
                                <tn:accounttype>savings</tn:accounttype>
                                <tn:accountno>123456789</tn:accountno>
                            </tn:account>
                        </tn:balancesheet>
                    </tn:balance_InnerSet>
                </balance>
            </temp-root:xxxxx_update>
        </soapenv:Body>
    </soapenv:Envelope>

实际输出

    <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="*[descendant::text() or descendant-or-self::*/@*[string()]]">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@*[string()]">
        <xsl:copy/>
    </xsl:template>
    </xsl:stylesheet>

预期输出

    <?xml version='1.0' encoding='utf-8'?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Body>
            <temp-root:xxxxx_update xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <firstname>Kevin</firstname>
                <Status>false</Status>
                <lastname>John</lastname>
                <balance>
                    <tn:balance_InnerSet xmlns:tn="xxxxxxxxxxxxx">
                        <tn:balancesheet>
                            <tn:account>
                                <tn:accounttype>savings</tn:accounttype>
                                <tn:accountno>123456789</tn:accountno>
                            </tn:account>
                        </tn:balancesheet>
                    </tn:balance_InnerSet>
                </balance>
            </temp-root:xxxxx_update>
        </soapenv:Body>
    </soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

希望这会有所帮助。这是为了更改命名空间。

<强> XML

<?xml version='1.0' encoding='utf-8'?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Body>
            <temp-root:xxxxx_update xmlns:temp-root="http://xx.xxxxx.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                <firstname>Kevin</firstname>
                <Status>false</Status>
                <lastname>John</lastname>
                <balance>
                    <tn:balance_InnerSet xmlns:tn="xxxxxxxxxxxxx">
                        <tn:balancesheet>
                            <tn:account>
                                <tn:accounttype>savings</tn:accounttype>
                                <tn:accountno>123456789</tn:accountno>
                            </tn:account>
                        </tn:balancesheet>
                    </tn:balance_InnerSet>
                </balance>
            </temp-root:xxxxx_update>
        </soapenv:Body>
    </soapenv:Envelope>

<强> XSL

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="temp-root">
        <xsl:element name="temp-root:{local-name()}" namespace="http://xx.xxxxx.com">
            <xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

<强>输出

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <temp-root:xxxxx_update xmlns:temp-root="http://xx.xxxxx.com"
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <firstname>Kevin</firstname>
         <Status>false</Status>
         <lastname>John</lastname>
         <balance>
            <tn:balance_InnerSet xmlns:tn="xxxxxxxxxxxxx">
               <tn:balancesheet>
                  <tn:account>
                     <tn:accounttype>savings</tn:accounttype>
                     <tn:accountno>123456789</tn:accountno>
                  </tn:account>
               </tn:balancesheet>
            </tn:balance_InnerSet>
         </balance>
      </temp-root:xxxxx_update>
   </soapenv:Body>
</soapenv:Envelope>