2012-07-27 107 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