Xslt 如何在soap请求中使用xsl更改命名空间uri

Xslt 如何在soap请求中使用xsl更改命名空间uri,xslt,xml-namespaces,Xslt,Xml Namespaces,我有一个soap请求: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <soapenv:Header> <wsse:Security mustUnd

我有一个soap请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"         xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<soapenv:Header>
    <wsse:Security mustUnderstand="1">
        <wsse:UsernameToken>
            <wsse:Username>test</wsse:Username>
            <wsse:Password>test</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>
<soapenv:Body>
  <hel:docTypeRef_tns_sayHello xmlns:hel="http://aaa/bbb.fr">
     <arg0>John</arg0>
     <arg1>111-222-333</arg1>
  </hel:docTypeRef_tns_sayHello>
</soapenv:Body>
</soapenv:Envelope>
我想首先将出现在hel:doctyperf\u tns\u sayHello元素中的名称空间uri更改为其他内容,例如:。此命名空间定义可以出现在 hel:docTypeRef\u tns\u sayHello元素与上面的代码或根信封元素相同,因此我只想在信封元素中添加此名称空间定义

然后我想将mustUnderstand属性的值修改为0

结果应为以下形式:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"       xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-    1.0.xsd" 
**xmlns:hel="http://test.fr"**>
<soapenv:Header>
    <wsse:Security mustUnderstand="**0**">
        <wsse:UsernameToken>
            <wsse:Username>test</wsse:Username>
            <wsse:Password>test</wsse:Password>
        </wsse:UsernameToken>
    </wsse:Security>
</soapenv:Header>
<soapenv:Body>
  <hel:docTypeRef_tns_sayHello>
     <arg0>John</arg0>
     <arg1>111-222-333</arg1>
  </hel:docTypeRef_tns_sayHello>
</soapenv:Body>
</soapenv:Envelope>
谁能帮帮我吗? 坦克斯

I.这里是一个通用的参数化XSLT 1.0解决方案:

将此转换应用于此XML文档时:

二,。下面是一个完整的XSLT 2.0解决方案:

将此转换应用于上述同一XML文档时,将生成所需的正确结果:


可能重复的是您必须只使用XSLT 1.0还是可以使用XSLT 2.0?我提供了一个XSLT1.0解决方案,但对于名称空间创建需求,需要在通用解决方案中使用扩展函数。在XSLT2.0中,不需要扩展函数。
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pElemName" select="'hel:docTypeRef_tns_sayHello'"/>
 <xsl:param name="pOldNamespace" select="'http://aaa/bbb.fr'"/>
 <xsl:param name="pNewNamespace" select="'http://test.fr'"/>

 <xsl:variable name="vPrefix" select="substring-before($pElemName, ':')"/>

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

 <xsl:template match="*">
  <xsl:choose>
      <xsl:when test=
       "not(name() = $pElemName
          and
            namespace-uri() = $pOldNamespace
           )
       ">
        <xsl:call-template name="identity"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="{name()}" namespace="{$pNewNamespace}">
         <xsl:copy-of select=
           "namespace::*
                 [not(name() = $vPrefix
                    and
                      . = $pOldNamespace
                      )
                ]"/>

         <xsl:apply-templates select="@*"/>

         <xsl:apply-templates select="node()" mode="removeNS"/>
        </xsl:element>
      </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template match="*" mode="removeNS">
  <xsl:choose>
   <xsl:when test=
     "not(starts-with(name(), $vPrefix)
        and
          namespace-uri() = $pOldNamespace
         )">
     <xsl:element name="{name()}" namespace="{namespace-uri()}">
         <xsl:copy-of select=
           "namespace::*
                 [not(name() = $vPrefix
                    and
                      . = $pOldNamespace
                      )
                ]"/>
       <xsl:apply-templates select="@*"/>
       <xsl:apply-templates select="node()" mode="removeNS"/>
     </xsl:element>
   </xsl:when>
  </xsl:choose>
 </xsl:template>

 <xsl:template match="@mustUnderstand">
  <xsl:attribute name="{name()}">0</xsl:attribute>
 </xsl:template>
</xsl:stylesheet>
<soapenv:Envelope
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <soapenv:Header>
        <wsse:Security mustUnderstand="1">
            <wsse:UsernameToken>
                <wsse:Username>test</wsse:Username>
                <wsse:Password>test</wsse:Password>
            </wsse:UsernameToken>
        </wsse:Security>
    </soapenv:Header>
    <soapenv:Body>
        <hel:docTypeRef_tns_sayHello xmlns:hel="http://aaa/bbb.fr">
            <arg0>John</arg0>
            <arg1>111-222-333</arg1>
        </hel:docTypeRef_tns_sayHello>
    </soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
   <soapenv:Header>
      <wsse:Security mustUnderstand="0">
         <wsse:UsernameToken>
            <wsse:Username>test</wsse:Username>
            <wsse:Password>test</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <hel:docTypeRef_tns_sayHello xmlns:hel="http://test.fr">
         <arg0>John</arg0>
         <arg1>111-222-333</arg1>
      </hel:docTypeRef_tns_sayHello>
   </soapenv:Body>
</soapenv:Envelope>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
 exclude-result-prefixes="ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <xsl:variable name="vrtfDoc">
   <t xmlns:hel="http://test.fr"/>
  </xsl:variable>

  <xsl:variable name="vNS" select=
   "ext:node-set($vrtfDoc)/*/namespace::*
                               [name()='hel']"/>

  <xsl:element name="{name()}"
               namespace="{namespace-uri()}">
    <xsl:copy-of select="namespace::*"/>

    <xsl:copy-of select="$vNS"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>
<t xmlns:someNS="some:NS"/>
<t xmlns:someNS="some:NS" xmlns:hel="http://test.fr"/>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pElemName" select="'hel:docTypeRef_tns_sayHello'"/>
 <xsl:param name="pOldNamespace" select="'http://aaa/bbb.fr'"/>
 <xsl:param name="pNewNamespace" select="'http://test.fr'"/>

 <xsl:variable name="vPrefix" select="substring-before($pElemName, ':')"/>

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

 <xsl:template match="/*">
  <xsl:element name="{name()}" namespace="{namespace-uri()}">
   <xsl:namespace name="{$vPrefix}" select="$pNewNamespace"/>

   <xsl:copy-of select=
       "namespace::*
             [not(name() = $vPrefix
                and
                  . = $pOldNamespace
                  )
              ]"/>

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

 <xsl:template match="*">
  <xsl:choose>
      <xsl:when test=
       "not(name() = $pElemName
          and
            namespace-uri() = $pOldNamespace
           )
       ">
        <xsl:call-template name="identity"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:element name="{name()}" namespace="{$pNewNamespace}">
         <xsl:copy-of select=
           "namespace::*
                 [not(name() = $vPrefix
                    and
                      . = $pOldNamespace
                      )
                ]"/>

         <xsl:apply-templates select="@*"/>

         <xsl:apply-templates select="node()" mode="removeNS"/>
        </xsl:element>
      </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template match="*" mode="removeNS">
  <xsl:choose>
   <xsl:when test=
     "not(starts-with(name(), $vPrefix)
        and
          namespace-uri() = $pOldNamespace
         )">
     <xsl:element name="{name()}" namespace="{namespace-uri()}">
         <xsl:copy-of select=
           "namespace::*
                 [not(name() = $vPrefix
                    and
                      . = $pOldNamespace
                      )
                ]"/>
       <xsl:apply-templates select="@*"/>
       <xsl:apply-templates select="node()" mode="removeNS"/>
     </xsl:element>
   </xsl:when>
  </xsl:choose>
 </xsl:template>

 <xsl:template match="@mustUnderstand">
  <xsl:attribute name="{name()}">0</xsl:attribute>
 </xsl:template>
</xsl:stylesheet>
<soapenv:Envelope xmlns:hel="http://test.fr"
                  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
   <soapenv:Header>
      <wsse:Security mustUnderstand="0">
         <wsse:UsernameToken>
            <wsse:Username>test</wsse:Username>
            <wsse:Password>test</wsse:Password>
         </wsse:UsernameToken>
      </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
      <hel:docTypeRef_tns_sayHello>
         <arg0>John</arg0>
         <arg1>111-222-333</arg1>
      </hel:docTypeRef_tns_sayHello>
   </soapenv:Body>
</soapenv:Envelope>