删除元素的名称空间和名称空间前缀

时间:2019-07-02 09:14:27

标签: xslt

尊敬的专家们,我有SOAP Env的输入请求,该请求只需要使用XSLT捕获即可 同时,需要删除元素上的名称空间以及每个元素的名称空间前缀。

输入请求

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
xmlns:v1="http://xmldefs. ag.com/Applications/eer/V1">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<v1:ProcessDistr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <v1:Group xmlns:ns2="http://xmldefs.ag.com/DD/Commons">
    <v1:GroupID>437848</v1: GroupID>
    <v1:Status>true</v1:Status>
    <v1:Parent>45434554</v1:Parent>
  </v1:Group>
</v1:ProcessDistr>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

已收到输出

    <?xml version="1.0" encoding="UTF-8"?>
    <v1:ProcessDistr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsu=http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://xmldefs. ag.com/Applications/eer/V1">
      <v1:Group xmlns:ns2="http://xmldefs.ag.com/DD/Commons">
        <v1:GroupID>437848</v1: GroupID>
        <v1:Status>true</v1:Status>
        <v1:Parent>45434554</v1:Parent>
      </v1:Group>
    </v1:ProcessDistr>

XSLT代码

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"  xmlns:v1="http://xmldefs. ag.com/Applications/eer/V1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsltc="http://xml.apache.org/xalan/xsltc" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sap="http://www.sap.com/sapxsl" xmlns:prof="http://ixult.net/ProfileExchange" exclude-result-prefixes ="v1" xmlns:wsu=http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" >
       <!-- Output -->
       <xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="yes"/>


       <xsl:template match="/">
          <!-- filter ProcessDistr-->

             <xsl:copy-of select="//v1:ProcessDistr"/>

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

我期望这个输出

<?xml version="1.0" encoding="UTF-8"?>
<ProcessDistr >
  <Group >
    <GroupID>437848</GroupID>
    <Status>true</Status>
    <Parent>45434554</Parent>
  </Group>
</ProcessDistr>

请您分享一下我对此的看法。

非常感谢您。

致以最诚挚的问候, 卫星N

2 个答案:

答案 0 :(得分:0)

您想要的结果可以通过以下方式轻松实现:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v1="http://xmldefs.ag.com/Applications/eer/V1">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

</xsl:stylesheet>

演示(从输入中删除多余的空格后):https://xsltfiddle.liberty-development.net/pPzifqc

答案 1 :(得分:-1)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" 
    xmlns:v1="http://xmldefs. ag.com/Applications/eer/V1"
    xmlns:ns2="http://xmldefs.ag.com/DD/Commons"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    exclude-result-prefixes="xs SOAP-ENV wsu v1 ns2 xsi"
    version="2.0">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:template match="SOAP-ENV:Envelope">
        <xsl:apply-templates select="SOAP-ENV:Body/v1:ProcessDistr"/>
    </xsl:template>
    <xsl:template match="v1:ProcessDistr">
        <xsl:element name="ProcessDistr">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="v1:Group">
        <xsl:element name="Group">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="v1:GroupID">
        <xsl:element name="GroupID">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="v1:Status">
        <xsl:element name="Status">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="v1:Parent">
        <xsl:element name="Parent">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
Try this