2017-04-11 132 views
0

我要删除元素d和评论<!-- d -->当家长有属性c="string1"XSLT:删除一个孩子时,父母有一个属性

输入:

<a> 
<b c="string1"> 
    <!-- d --> 
    <d> 
    <e/> 
    </d> 
    <f/> 
</b> 

<b c="string2"> 
    <!-- d --> 
    <d> 
    <e/> 
    </d> 
    <f/> 
</b> 
</a> 

所需的输出:

<a> 
<b c="string1"> 
    <f/> 
</b> 

<b c="string2"> 
    <!-- d --> 
    <d> 
    <e/> 
    </d> 
    <f/> 
</b> 
</a> 

XSLT:

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

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

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

    <!-- Those templates do not work --> 
    <xsl:template match="d[???]" /> 
    <xsl:template match="comment()="d" /> 
</xsl:stylesheet> 

回答

1

这里你可以看看它的一种方法:

<xsl:template match="b[@c='string1']/d" /> 
<xsl:template match="b[@c='string1']/comment()[.=' d ']" /> 

或者,如果你喜欢:

<xsl:template match="b[@c='string1']"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()[not(self::d or self::comment()[.=' d '])]"/> 
    </xsl:copy> 
</xsl:template> 

注意,在给定的例子注释的值实际上是" d "即由空格包围的字符"d"

0

找到了解决方案。这两个xsl:模板可以工作。

<xsl:template match="d[ancestor::b[@c='string1']]" /> 
<xsl:template match="comment()[contains(.,'d') and ancestor::b[@c='string1']]" /> 

我宁愿不使用contains(.,'d')但对注释的文本的平等表达,但我不知道怎么写的表达。

+0

你想要的平等表达式是'。 ='d''。不要忽视'd'两边的空间。 –

+0

此外,使用普通路径表达式代替祖先轴上的谓词表达式测试节点更为传统。例如,'match =“b [@ c ='string1']/d”'。 –

相关问题