xml - 使用 XSLT 更改 SOAP namespace

标签 xml xslt soap namespaces

我需要更改 SOAP 文档的 namespace 。只有 namespace 应该更改,所有其他 SOAP 保持原样。

这是我的 SOAP 文档:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                  xmlns:tar="namespace1" 
                  xmlns:tar1="namespace1">
   <soapenv:Header/>
   <soapenv:Body>
      <tar:RegisterUser>
         <tar1:Source>?</tar1:Source>
         <tar1:Profile>
            <tar1:EmailAddress>?</tar1:EmailAddress>

            <tar1:NewEmailAddress>?</tar1:NewEmailAddress>

            <tar1:GivenName>?</tar1:GivenName>

         </tar1:Profile>
      </tar:RegisterUser>
   </soapenv:Body>
</soapenv:Envelope>

我想要 namespace1改变例如在命名空间2中。
这是我的转变:
<xsl:stylesheet 
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:old="namespace1"
    xmlns:new="namespace2">

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

</xsl:stylesheet>

但是当我运行它时,它给了我这个输出:
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tar="namespace1" xmlns:tar1="namespace1">
   <soapenv:Header/>
   <soapenv:Body>
      <RegisterUser>
         <Source>?</Source>
         <Profile>
            <EmailAddress>?</EmailAddress>

            <NewEmailAddress>?</NewEmailAddress>

            <GivenName>?</GivenName>

         </Profile>
      </RegisterUser>
   </soapenv:Body>
</soapenv:Envelope>

我只想更改命名空间。不是从元素中删除的前缀。也许有人知道问题出在哪里?

最佳答案

本次改造 :

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:tar="namespace2" xmlns:tar1="namespace2"
  xmlns:old="namespace1">

  <xsl:output omit-xml-declaration="yes"/>

  <xsl:variable name="vNewNamespaces" select=
   "document('')/*/namespace::*[name()='tar' or name()='tar1']"/>

    <xsl:template match="@*|node()">
     <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{name()}">
          <xsl:copy-of select=
          "namespace::*[not(name()='tar' or name()='tar1')] | $vNewNamespaces"/>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="old:*">
        <xsl:element name="{name()}" xmlns="namespace2" >
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

当应用于提供的 XML 文档时 :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns:tar="namespace1"
                  xmlns:tar1="namespace1">
    <soapenv:Header/>
    <soapenv:Body>
        <tar:RegisterUser>
            <tar1:Source>?</tar1:Source>
            <tar1:Profile>
                <tar1:EmailAddress>?</tar1:EmailAddress>
                <tar1:NewEmailAddress>?</tar1:NewEmailAddress>
                <tar1:GivenName>?</tar1:GivenName>
            </tar1:Profile>
        </tar:RegisterUser>
    </soapenv:Body>
</soapenv:Envelope>

产生想要的、正确的结果 :
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tar="namespace2" xmlns:tar1="namespace2">
    <soapenv:Header/>
    <soapenv:Body>
        <tar:RegisterUser>
            <tar1:Source>?</tar1:Source>
            <tar1:Profile>
                <tar1:EmailAddress>?</tar1:EmailAddress>
                <tar1:NewEmailAddress>?</tar1:NewEmailAddress>
                <tar1:GivenName>?</tar1:GivenName>
            </tar1:Profile>
        </tar:RegisterUser>
    </soapenv:Body>
</soapenv:Envelope>

关于xml - 使用 XSLT 更改 SOAP namespace ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11687793/

相关文章:

c# - 用c#保存XML文件并保留空元素的格式

javascript - 来自 Google Docs API 的数据馈送

java - JSP - TransformerFactory.newInstance().newTransformer(xsl) 返回空值?

xml - 引用 XML 中的另一个元素

xslt - 根据参数中定义的索引选择 xsl 元素

c# - WCF SOAP 服务在(反)序列化时更改数组项元素的名称

php - ZF2 SOAP "Procedure not present"错误

sql - 在 SQL Server 2008 存储过程中使用 OPENXML - INSERT 顺序与 XML 文档不同

xslt - 在 XSLT 2.0 中调用错误函数

java - 如何自定义soapUI库以从WSDL生成请求和响应?