使用XSLT更改SOAP名称空间 [英] Change SOAP namespaces with XSLT

查看:82
本文介绍了使用XSLT更改SOAP名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改SOAP文档的名称空间.仅应更改名称空间,而所有其他SOAP都保留原样.

I need to change namespaces of SOAP document. Only the namespaces should be change and all other SOAP left what it is.

这是我的SOAP文档:

Here is my SOAP document:

<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更改为例如在namespace2中. 这是我的转变:

I want the namespace1 to be change for e.g. in namespace2. Here is my transformation:

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

但是当我运行它时,它会提供以下输出:

But when I run it it gives me this output:

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

我只希望更改名称空间.没有从元素中删除前缀.也许有人知道问题出在哪里?

I want only namespace to be change. Not prefixes removed from elements. Maybe someone know where is the problem?

推荐答案

此转换:

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

这篇关于使用XSLT更改SOAP名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆