2012-07-27 105 views
1

我需要更改SOAP文档的名称空间。只有命名空间应该改变,所有其他的SOAP都离开它。使用XSLT更改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成为例如改变在namespace2中。 这里是我的转型:

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

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

回答

2

这种转变

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

谢谢。太棒了! – 2012-07-27 13:34:57

+0

@PauliusMatulionis:不客气。 – 2012-07-27 14:23:23