2015-04-07 82 views
0

我正在嘗試使用XSLT轉換XML。下面是我的XML的結構:使用XSLT進行XML轉換

<?xml version="1.0" encoding="UTF-8"?> 
<GetInvoiceList> 
<Request> 
<BillStatusCode typecode="1">type description</BillStatusCode> 
<EBillProcessStatusCode typecode="2">type description</EBillProcessStatusCode> 
</Request> 
</GetInvoiceList> 

我想這XSLT代碼:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()"> 
<xsl:copy> 
<xsl:element name="{name()}"> 
<xsl:apply-templates select="node()"/> 
</xsl:element> 
<xsl:apply-templates select="@*"/> 
</xsl:copy> 
</xsl:template> 

<xsl:template match="@*"> 
<xsl:element name="{name()}"> 
<xsl:value-of select="."/> 
</xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

輸出:

<GetInvoiceList> 
<GetInvoiceList> 
    <Request> 
     <Request> 
      <BillStatusCode> 
       <BillStatusCode>type description</BillStatusCode> 
       <typecode>1</typecode> 
      </BillStatusCode> 
      <EBillProcessStatusCode> 
       <EBillProcessStatusCode>type description</EBillProcessStatusCode> 
       <typecode>2</typecode> 
      </EBillProcessStatusCode> 
     </Request> 
    </Request> 
</GetInvoiceList> 
</GetInvoiceList> 

預期輸出:

<GetInvoiceList> 
     <Request> 
       <BillStatusCode> 
       <BillStatusCode>type description</BillStatusCode> 
       <typecode>1</typecode> 
      </BillStatusCode> 
      <EBillProcessStatusCode> 
       <EBillProcessStatusCode>type description</EBillProcessStatusCode> 
       <typecode>2</typecode> 
      </EBillProcessStatusCode> 
     </Request> 
     </GetInvoiceList> 

的代碼在所有節點上工作,這就是爲什麼複製tes節點已創建。我希望它只適用於具有文本&屬性的節點。

希望對此有幫助。謝謝!

+0

你能在這種情況下顯示你實際期望的輸出嗎?謝謝! –

+0

我已經編輯了我的問題。請看一看。 –

回答

0

問題出在node()匹配模板;您同時有一個xsl:copyxsl:element,它們都創建相同的元素,導致重複。這適用於所有節點

<xsl:template match="node()"> 
    <xsl:copy> 
     <xsl:element name="{name()}"> 

你需要的是這種改變只匹配屬性的元素

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

然後,你有一個模板匹配只是node()在其中刪除xsl:element元素

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

試試這個XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

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

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

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

沒錯。重複值現在不存在,但它也改變了所需的輸出。 現在沒有單獨的結束標記。 –

+0

完美。它的工作現在。非常感謝。 :) –