简体   繁体   English

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

[英]remove empty elements and attributes from xml but copy all the namepsce using xslt

I have below xml and trying to remove the empty xml elements and attributes. 我在xml下面并尝试删除空的xml元素和属性。 but have to copy all the namespace into the xml. 但必须将所有名称空间复制到xml中。 the XSLT which i am using is working fine. 我正在使用的XSLT工作正常。 but the only problem i am facing here, it's also removing the xmlns:temp-root="http://xx.xxxxx.com" namespace from the xml. 但是我在这里面临的唯一问题是,它还从xml中删除了xmlns:temp-root="http://xx.xxxxx.com"命名空间。 Not sure why it's been removed from the output xml. 不知道为什么将其从输出xml中删除。 can somebody please help me to resolve this issue. 有人可以帮我解决这个问题吗?

XML 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>

XSLT XSLT

    <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>

ACTUAL OUTPUT 实际输出

    <?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>

EXPECTED OUTPUT 预期的输出

   <?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>

Hope this helps. 希望这可以帮助。 This is for changing the namespaces. 这是用于更改名称空间。

XML 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

<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>

Output 产量

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM