2017-02-21 38 views
0

我需要能夠去除取決於包含段落的子元素<version>中的屬性指定的父節點(及其子女)的屬性中刪除父節點。因此,在下面的例子中,我需要XSLT找到<version version="ABCD">的實例,並從父<para0>元素中刪除所有內容。換句話說,我試圖完成條件文本過濾。 i的元素需要匹配(和刪除)將永遠是<applic>父,但可能不總是<para0>作爲例子,所以我需要指定某種方式,它需要匹配「應用程」元素的父元素,而不是明確指定para0。C#XSLT基於childnode

應該從例子中清晰。我需要刪除版本屬性爲ABCD的所有para0數據。

所以這是一些示例XML

<root> 
    <para0> 
    <applic> 
     <model> 
     <version version="ABCD"></version> 
     </model> 
    </applic> 
     <subpara1><title>First Title</title> 
      <para>Some text relating to ABCD configuration</para> 
     </subpara1> 
     <subpara1><title>Second Title</title> 
      <para>Some other text and stuff relating to ABCD configuration</para> 
     </subpara1> 
    </para0> 
    <para0> 
    <applic> 
     <model> 
     <version version="TRAINING"></version> 
     </model> 
    </applic> 
     <subpara1><title>First Title</title> 
      <para>Some text relating to TRAINING configuration</para> 
     </subpara1> 
     <subpara1><title>Second Title</title> 
      <para>Some other text and stuff relating to TRAINING configuration</para> 
     </subpara1> 
    </para0> 
</root> 

這裏是XSLT我有這麼遠,但我需要它,一旦匹配到ABCD,基本上選擇並刪除父「應用程」和所有的子節點。

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

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

     <xsl:template match = "//applic/model[@*]/version[@version='ABCD']" /> 

     </xsl:stylesheet> 
+0

的可能的複製[如何使用XPath的 「不」?](http://stackoverflow.com/questions/1550981/how-to -use-不在位的XPath) –

回答

0

您的「移除」模板必須與您想移除的元素相匹配 - 不是它的後代之一。

嘗試:

XSLT 1.0

<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="*[applic/model/version/@version='ABCD']" /> 

</xsl:stylesheet> 
+0

謝謝,但它不會永遠是一個「para0」,我很匹配。它可以是'appic'標籤所遵循的任何數量的元素。所以我需要刪除一個未知元素,不是特別'para0'。 –

+0

好吧,現在呢? –

+0

非常感謝。 –