2015-05-27 233 views
1

我有這樣的XML代碼:XSLT改變命名空間

<soapenv:Envelope xmlns:soapenv="something2" xmlns:oas="something1" xmlns:bill="something"> 
    <soapenv:Header> 
    <wsse:Security xmlns:wsse="something_else"> 
    <wsse:UsernameToken> 
     <wsse:Username>SYSUSER</wsse:Username> 
     <wsse:Password Type="PasswordText">sysuser00</wsse:Password> 
    </wsse:UsernameToken> 
    </wsse:Security> 
    </soapenv:Header> 
    <soapenv:Body> 
    <GetBillList xmlns="old_uri" xmlns:xsi="else" xsi:schemaLocation="old_schema"> 
    <Case> 
    <CaseID>699677</CaseID> 
    <BillGroup casref="123"> 
    <BillGroupID>1</BillGroupID> 
    </BillGroup> 
    </Case> 
    <StartDate>2014-06-12</StartDate> 
    </GetBillList> 
    </soapenv:Body> 
    </soapenv:Envelope> 

我想改變old_uri & old_schemanew_uri & new_schema分別與屬性轉換爲BillGroup節點的元件沿。使用

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:bill="something"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="@*|node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 
    <xsl:template match="bill:BillGroup"> 
    <xsl:element name="{local-name()}"> 
    <xsl:value-of select="."/> 
    </xsl:element> 
    </xsl:template> 
    </xsl:stylesheet> 

目標XML:

<soapenv:Envelope xmlns:soapenv="something2" xmlns:oas="something1" xmlns:bill="something"> 
    <soapenv:Header> 
    <wsse:Security xmlns:wsse="something_else"> 
     <wsse:UsernameToken> 
      <wsse:Username>SYSUSER</wsse:Username> 
      <wsse:Password Type="PasswordText">sysuser00</wsse:Password> 
     </wsse:UsernameToken> 
    </wsse:Security> 
    </soapenv:Header> 
    <soapenv:Body> 
    <GetBillList xmlns="new_uri" xmlns:xsi="else" xsi:schemaLocation="new_schema"> 
     <Case> 
      <CaseID>699677</CaseID> 
      <BillGroup> 
       <casref>123</casref> 
       <BillGroupID>1</BillGroupID> 
      </BillGroup> 
     </Case> 
     <StartDate>2014-06-12</StartDate> 
     </GetBillList> 
     </soapenv:Body> 
    </soapenv:Envelope> 

將不勝感激任何幫助。

+2

那不是XSLT。 –

+0

看起來您不小心將您的源XML粘貼了兩次,而不是粘貼XSLT嘗試。你能更新你的問題嗎? –

+0

我很抱歉。現在編輯代碼。當我在xmlns:bill中傳遞原始URL時,caseref被轉換爲元素。只是想改變命名空間。請幫忙。 –

回答

2

在您的輸入XML中,您的billGroup元素是old_uri名稱空間的一部分,而不是something名稱空間的一部分。這意味着你需要相應地調整你的命名空間前綴。您還需要改變它的billGroup屬性相匹配,如果你想改變屬性爲一個元素:

<xsl:template match="old:BillGroup/@*"> 
    <xsl:element name="{local-name()}" namespace="new_uri"> 
     <xsl:value-of select="."/> 
    </xsl:element> 
</xsl:template> 

這裏old是綁定到URI old_uri的前綴。

若要更改old_uri空間URI來new_uri你可以在舊的命名空間

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

而要改變模式位置相匹配的元素模板,有屬性相匹配的模板

<xsl:template match="@xsi:schemaLocation"> 
    <xsl:attribute name="{name()}"> 
     <xsl:text>new_schema</xsl:text> 
    </xsl:attribute> 
</xsl:template> 

試試這個XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="else" xmlns:old="old_uri" > 
<xsl:output method="xml" indent="yes"/> 

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

<xsl:template match="@xsi:schemaLocation"> 
    <xsl:attribute name="{name()}"> 
    <xsl:text>new_schema</xsl:text> 
    </xsl:attribute> 
</xsl:template> 

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

<xsl:template match="old:BillGroup/@*"> 
    <xsl:element name="{local-name()}" namespace="new_uri"> 
    <xsl:value-of select="."/> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 
+0

謝謝蒂姆。你的代碼工作正常。只有架構位置沒有變化。 :( –

+0

我做了一個小小的變化,現在它的工作很好..非常感謝您的幫助。 –