删除 Soap Headers 和命名空间

Remove Soap Headers and Namespaces

我有一个要求,需要从 XML 中删除 SOAP Headers 和所有命名空间。我在网上搜索过,我有 2 个单独的 XSLT,一个用于删除 SOAP Headers,另一个用于删除名称空间。我们可以用一个 XSLT 来执行这两个操作吗?

提前致谢!

XML:

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
    <soap-env:Header>
        <n0:Info xmlns:n0="http://www.sap.com/dfg">
            <m:Type xmlns:m="http://www.sap.com/dfg">SA</m:Type>
            <m:App xmlns:m="http://www.sap.com/fghj"/>
            <m:Component xmlns:m="http://www.sap.com/tghj"/>
        </n0:Info>
    </soap-env:Header>
    <soap-env:Body>
        <n1:data xmlns:n1="http://namspace.com" xmlns:prx="urn:sap.com:proxy:XXXX">
            <n1:dataSegement>
                <n1:dataSegementKey>12345678</n1:dataSegementKey>
                <n1:number>123456789</n1:number>
                <n1:dueDate>01/06/2021</n1:dueDate>
                <n1:amount>1200.0000</n1:amount>
            </n1:dataSegement>
            <n1:dataSegement>
                <n1:dataSegementKey>123456789</n1:dataSegementKey>
                <n1:number>12345678</n1:number>
                <n1:dueDate>28/05/2021</n1:dueDate>
                <n1:amount>-1746.4000</n1:amount>
            </n1:dataSegement>
        </n1:data>
    </soap-env:Body>
</soap-env:Envelope>

用于去除 SOAP 的 XLST Headers:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
   <xsl:template match="/">
      <xsl:copy-of select="SOAP-ENV:Envelope/SOAP-ENV:Body/*" />
   </xsl:template>
</xsl:stylesheet>

用于删除命名空间的 XLST:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

预期输出:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <dataSegement>
        <dataSegementKey>12345678</dataSegementKey>
        <number>123456789</number>
        <dueDate>01/06/2021</dueDate>
        <amount>1200.0000</amount>
    </dataSegement>
    <dataSegement>
        <dataSegementKey>123456789</dataSegementKey>
        <number>12345678</number>
        <dueDate>28/05/2021</dueDate>
        <amount>-1746.4000</amount>
    </dataSegement>
</data>

使用以下样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:n1="http://namspace.com">
    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="no"/>

    <!-- 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 all attributes and 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="/soap-env:*">
      <xsl:apply-templates select="soap-env:Body/n1:data" />
    </xsl:template>

</xsl:stylesheet>

输出为:

<?xml version="1.0" encoding="utf-8"?>
<data>
   <dataSegement>
      <dataSegementKey>12345678</dataSegementKey>
      <number>123456789</number>
      <dueDate>01/06/2021</dueDate>
      <amount>1200.0000</amount>
   </dataSegement>
   <dataSegement>
      <dataSegementKey>123456789</dataSegementKey>
      <number>12345678</number>
      <dueDate>28/05/2021</dueDate>
      <amount>-1746.4000</amount>
   </dataSegement>
</data>