2017-02-17 61 views
0

原帖:XSLT要更改命名空間 - 版本兼容性

關於我先前使用XSLT程序後更換一個名稱空間

XSLT to change namespace in element

它。我有這個問題的答案,但用我的系統測試時,它顯示錯誤,因爲我的應用系統工作支持版本1.0

請幫助使代碼與版本1.0兼容。下面是xsl:

<xsl:stylesheet 
    version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsd="http://www.xx.com" 
    xmlns:ns="http://www.mnv.com/elc/sap" 
    exclude-result-prefixes="ns"> 

    <xsl:output encoding='UTF-8' indent='yes' method='xml'/> 

    <!-- copy everything into the output --> 
    <xsl:template match='@*|node()'> 
     <xsl:copy> 
      <xsl:apply-templates select='@*, node()'/> 
     </xsl:copy> 
    </xsl:template> 

    <!-- template to match ns:IRenvelope element and creating a new element --> 
    <xsl:template match="ns:IRenvelope"> 
     <xsl:element name="IRL" namespace="http://www.xx.com"> 
      <xsl:apply-templates select="@*, node()"/> 
     </xsl:element> 
    </xsl:template> 

    <!-- 
     template to change the namespace 
     of the elements 
     from "http://www.mnv.com/elc/sap" 
     to "http://www.xx.com" 
    --> 

    <xsl:template match="ns:*"> 
     <xsl:element name="{local-name()}" namespace="http://www.xx.com"> 
      <xsl:apply-templates select="@*, node()"/> 
     </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 

我的XML消息是:

<GMessage xmlns="http://www.giffgaff.uk/CM/envelope"> 
    <EnvelopeVersion>2.0</EnvelopeVersion> 
    <body> 
     <IRenvelope xmlns="http://www.mnv.com/elc/sap"> 
      <Keys> 
       <Key Type="TaxOfficeNumber">635</Key> 
      </Keys> 
     </IRenvelope> 
    </body> 
</GMessage> 

預期的結果:

<?xml version="1.0" encoding="UTF-8"?> 
<GMessage xmlns="http://www.giffgaff.uk/CM/envelope"> 
    <EnvelopeVersion>2.0</EnvelopeVersion> 
    <body> 
     <IRL xmlns="http://www.xx.com"> 
      <Keys> 
       <Key Type="TaxOfficeNumber">635</Key> 
      </Keys> 
     </IRL> 
    </body> 
</GMessage> 

回答

1

問題僅僅在於這些線路...

<xsl:apply-templates select='@*, node()'/> 

此語法在XSLT 1.0中無效。在XSLT 2.0中,逗號用於構建「序列」。

但是,你可以簡單地替換這一點,而不是線的出現,這將在兩個XSLT 1.0工作,2.0

<xsl:apply-templates select='@*|node()'/> 

的豎線是聯合運營商加入節點集。

+0

謝謝。它的工作.....請你可以幫助如何使命名空間爲:在xml中? –

+1

這可能是最好的,你問一個新的問題,因爲我不太清楚問題是什麼。謝謝。 –