2016-05-05 78 views
1

從下面的文件開始刪除屬性在同一行:移動節點和XSLT

<foo> 
    <bar> 
    <items> 
     <item attribull="true" name="foo" /> 
     <item attribull="false" name="bar" /> 
     <item attribull="true" name="foobar" /> 
    </items> 
    (...) 
    </bar> 
</foo> 

我想生成以下文件,其中items節點被移動,所有attribull屬性除去。

<foo> 
    <items> 
    <item name="foo" /> 
    <item name="bar" /> 
    <item name="foobar" /> 
    </items> 
    <bar> 
    (...) 
    </bar> 
</foo> 

我知道如何寫一個XSLT移到別處任何節點,我知道如何編寫XSLT刪除特定的屬性太多,但我不知道是否有可能與一個 XSLT (一次通過)。

任何線索?

回答

2

只需複製項目(這裏通過默認的遞歸規則),然後複製<bar>與所有子項項目。要刪除某個屬性的所有實例,只需添加一個空的匹配規則:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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

<xsl:template match="@attribull" /> 

<xsl:template match="bar"> 
    <xsl:apply-templates select="items"/> 

    <xsl:copy> 
    <xsl:apply-templates select="@*|text()|*[not(self::items)]"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

'* [not(name()='items')]'通常寫爲'* [not(self :: items) ]'。 –

+0

@MartinHonnen謝謝,我將其規範化。將來,如果您發現可能的改進,請隨時直接編輯我的帖子。祝你今天愉快! – phihag

+0

謝謝!我想知道如果我需要將「項目」移動到一個完全不同的位置,比如「酒吧」的同胞的子分節點,那麼答案會有多不同。 – Guid