如何从根元素中删除Soap名称空间

时间:2019-05-14 14:13:09

标签: xslt

我在xslt转换中遇到了一些问题。我想删除在xslt转换过程中添加到结果中的多余名称空间。

这是我的源xml:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <Root xmlns="http://www.abc123.org/xyz/ase" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <Header>           
            <CreationDateTime>2019-05-08T06:34:04.2235068-05:0011</CreationDateTime>
            <BODID>51c336d1-8e6a-46211c-973f-a9ca6c8a33ce</BODID>
            <ReferenceID>9078111e00-9b82-46b3-a419-be80521b2a94</ReferenceID>
            <Success>false</Success>
         </Header>
         <Data>
            <ErrorMessage>
               <ID/>
               <Type>Invalid Data</Type>
               <Description>InvalidData</Description>
            </ErrorMessage>
         </Data>
      </Root>
   </soapenv:Body>
</soapenv:Envelope>

这是我的xslt代码:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" exclude-result-prefixes="soapenv">
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="soapenv:*">
    <xsl:apply-templates select="@* | node()" />
  </xsl:template>
</xsl:stylesheet>

预期输出:

<Root xmlns="http://www.abc123.org/xyz/ase" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <Header>           
            <CreationDateTime>2019-05-08T06:34:04.2235068-05:0011</CreationDateTime>
            <BODID>51c336d1-8e6a-46211c-973f-a9ca6c8a33ce</BODID>
            <ReferenceID>9078111e00-9b82-46b3-a419-be80521b2a94</ReferenceID>
            <Success>false</Success>
         </Header>
         <Data>
            <ErrorMessage>
               <ID/>
               <Type>Invalid Data</Type>
               <Description>InvalidData</Description>
            </ErrorMessage>
         </Data>
</Root>

实际输出:

 <Root xmlns="http://www.abc123.org/xyz/ase" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
         <Header>           
            <CreationDateTime>2019-05-08T06:34:04.2235068-05:0011</CreationDateTime>
            <BODID>51c336d1-8e6a-46211c-973f-a9ca6c8a33ce</BODID>
            <ReferenceID>9078111e00-9b82-46b3-a419-be80521b2a94</ReferenceID>
            <Success>false</Success>
         </Header>
         <Data>
            <ErrorMessage>
               <ID/>
               <Type>Invalid Data</Type>
               <Description>InvalidData</Description>
            </ErrorMessage>
         </Data>
 </Root>

在这里,我得到了需要删除的额外名称空间“ xmlns:soapenv”。

建议改正。

谢谢

1 个答案:

答案 0 :(得分:1)

xsl:copy将复制XML中声明的所有名称空间,即使元素本身不使用该名称空间。

您可以做的,不是在元素上使用xsl:copy,而是使用xsl:element创建新元素,然后仅复制所需的名称空间声明。...

尝试将此模板添加到您的XSLT

<xsl:template match="*[namespace-uri() != 'http://schemas.xmlsoap.org/soap/envelope/']" priority="2">
  <xsl:element name="{name()}" namespace="{namespace-uri()}">
    <xsl:copy-of select="namespace::*[. != 'http://schemas.xmlsoap.org/soap/envelope/']" />
    <xsl:apply-templates select="@* | node()" />  
  </xsl:element>
</xsl:template>
相关问题