2016-07-11 78 views
0

我有以下結構的XML:XSLT刪除標籤的子元素,但保留內容

<tuple> 
    <something>One</something> 
    <somethingelse>Two</somethingelse> 
    <somethingextra> 
     <tuple> 
      <somethingextra2>Three</somethingextra2> 
     </tuple> 
    </somethingextra> 
</tuple> 

我試圖找出如何刪除子<tuple>元素,同時保持內容。

<tuple> 
    <something>One</something> 
    <somethingelse>Two</somethingelse> 
    <somethingextra> 
     <somethingextra2>Three</somethingextra2> 
    </somethingextra> 
</tuple> 

任何幫助或建議將非常受歡迎。

+0

對'tuple'使用帶有覆蓋的身份模板,而不是複製元素並將模板應用於子元素,您只需將模板應用於其子元素。首先,你需要決定使用哪個'tuple'元素來做這件事 - 也許所有的非root元組元素? – kjhughes

回答

0

michael.hor257k出色地回答了這個問題。 這是我的變化,也處理了我有一些其他變化。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" 
    indent="yes" /> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()" /> 
     </xsl:copy> 
    </xsl:template> 
    <!-- Convert any atom, table or tuple element to a new element with the name attribute as the element name --> 
    <xsl:template match="atom[@name] | table[@name] | tuple[@name]"> 
     <xsl:element name="{@name}"> 
      <xsl:apply-templates /> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 
0

您需要一種方法來區分要刪除的根元素tuple元素和tuple元素。

例如,這將刪除具有父元素任何tuple元素:

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

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

<xsl:template match="*/tuple"> 
    <xsl:apply-templates/> 
</xsl:template> 

</xsl:stylesheet> 
+0

謝謝(抱歉,要花很長時間才能回覆)。在其他活動中淹沒了。 我結束了上面也處理其他組合的變化。非常感謝。 – Havado

0

你也可以切換到一個額外的模式,同時遍歷你的XML樹。在標準模式下複製所有元素,但在模式'noTuple'中,您跳過節點複製並返回到正常模式。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="somethingextra"> 
    <xsl:copy> 
     <!-- here you switch the mode --> 
     <xsl:apply-templates select="*" mode="noTuple"/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- in standard mode - copy the node an go on in standard mode --> 
    <xsl:template match="*"> 
     <xsl:copy> 
      <xsl:apply-templates select="*|text()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="text()"> 
    <xsl:value-of select="."/> 
    </xsl:template> 

    <!-- matches only for tuple nodes in extra mode --> 
    <xsl:template match="tuple" mode="noTuple"> 
     <!-- back to normal mode --> 
     <xsl:apply-templates select="*|text()"/> 
    </xsl:template> 

    <!-- in extra mode - copy the node an go on in extra mode --> 
    <xsl:template match="*" mode="noTuple"> 
     <xsl:copy> 
      <xsl:apply-templates select="*|text()" mode="noTuple"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="text()" mode="noTuple"> 
    <xsl:value-of select="."/> 
    </xsl:template> 

</xsl:stylesheet> 
+0

** 1。**這不是有效的XSLT樣式表。 - ** 2。**即使您解決該問題,也不會產生預期結果。 –

+0

@ michael.hor257k thnx,我會檢查這個 – OkieOth

+0

@ michael.hor257k編輯我的帖子,現在得到了理想的結果 - 對不起,在這裏很熱: - ] – OkieOth

相關問題