简体   繁体   中英

remove namspaces for all attributes except soapenv:* using XSLT 1.0

I am trying remove all namespaces except soapenv:.

Below is the code I am using.

  <?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>
    <!-- Stylesheet to remove all namespaces from a document -->
    <!-- NOTE: this will lead to attribute name clash, if an element contains
        two attributes with same local name but different namespace prefix -->
    <!-- Nodes that cannot have a namespace are copied as such -->
    <!-- template to copy elements -->
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>
    <!-- template to copy attributes -->
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
    </xsl:template>
    <!-- template to copy the rest of the nodes -->
    <xsl:template match="comment() | text() | processing-instruction()">
        <xsl:copy/>
    </xsl:template>
</xsl:stylesheet>

Input XML

<soapenv:Envelope xmlns:urn="urn:cidx:names:specification:ces:schema:all:5:0" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <ns0:ShipNotice xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="urn:cidx:names:specification:ces:schema:all:5:0" Version="5.0">
            <ns0:Header>
                <ns0:ThisDocumentIdentifier>
                    <ns0:DocumentIdentifier>0000001053685572</ns0:DocumentIdentifier>
                </ns0:ThisDocumentIdentifier>
                <ns0:ThisDocumentDateTime>
                    <ns0:DateTime DateTimeQualifier="On">2014-07-21T12:16:28</ns0:DateTime>
                </ns0:ThisDocumentDateTime>
                <ns0:RequestingDocumentDateTime>
                    <ns0:DateTime DateTimeQualifier="On">2014-07-21T12:16:29</ns0:DateTime>
                </ns0:RequestingDocumentDateTime>
            </ns0:Header>
        </ns0:ShipNotice>
    </soapenv:Body>
</soapenv:Envelope>

Output from the code

<Envelope>
    <Header/>
    <Body>
        <ShipNotice Version="5.0">
            <Header>
                <ThisDocumentIdentifier>
                    <DocumentIdentifier>0000001053685572</DocumentIdentifier>
                </ThisDocumentIdentifier>
                <ThisDocumentDateTime>
                    <DateTime DateTimeQualifier="On">2014-07-21T12:16:28</DateTime>
                </ThisDocumentDateTime>
                <RequestingDocumentDateTime>
                    <DateTime DateTimeQualifier="On">2014-07-21T12:16:29</DateTime>
                </RequestingDocumentDateTime>
            </Header>
        </ShipNotice>
    </Body>
</Envelope>

This is the desired output I am looking for.

<soapenv:Envelope xmlns:urn="urn:cidx:names:specification:ces:schema:all:5:0" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <soapenv:Body>
        <ShipNotice Version="5.0">
            <Header>
                <ThisDocumentIdentifier>
                    <DocumentIdentifier>0000001053685572</DocumentIdentifier>
                </ThisDocumentIdentifier>
                <ThisDocumentDateTime>
                    <DateTime DateTimeQualifier="On">2014-07-21T12:16:28</DateTime>
                </ThisDocumentDateTime>
                <RequestingDocumentDateTime>
                    <DateTime DateTimeQualifier="On">2014-07-21T12:16:29</DateTime>
                </RequestingDocumentDateTime>
            </Header>
        </ShipNotice>
        </soapenv:Body>
</soapenv:Envelope>

Can anyone please advise how I can achieve this?

UPDATE

I have added this and it solve my problem.

<xsl:template match="soapenv:Envelope | soapenv:Header | soapenv:Body">
        <xsl:copy>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>

You need to add this template:

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

For the unlikely event of an attribute being in the soapenv: namespace (and assuming you want to keep it there), add also:

<xsl:template match="@soapenv:*">
    <xsl:copy/>
</xsl:template>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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