2010-05-18 36 views
-1

我有下面的代码,我不得不选择所有id为节点=“文本”,而不是已经有一个父id为节点=“文本”:的XPath选择标签的ID没有后裔

<fo:block id="text"> 
    test00 
    <fo:block id="text"> 
     test01 
    </fo:block> 
    <fo:block> 
     test02 
    </fo:block> 
</fo:block> 
<fo:block id="text"> 
    test03 
</fo:block> 

所以在这个例子中,查询必须只返回两个带有内容test00和test03的fo:block。

谢谢。

+0

到目前为止你写了些什么? – Oded 2010-05-18 12:31:56

+1

似乎无效(!),没有人使用属性ID作为“不唯一”! ......在最好的情况下,我们可以说这不是最佳做法。 – 2014-06-30 18:00:35

回答

2

我会像这样的东西去:

//fo:block[@id='text' and not(./*[@id='text'])] 

我要去给它一个测试,现在,以确保它是明智的。是啊。它根据需要返回text00和text03。所以让我解释一下这个表达。

//fo:block    # Select all fo:block elements in the document 
[ 
    @id='text' and  # Get only those whose id attribute's value is "text" 
    not(./*[@id='text']) # and whose children do not have id attributes equal to "text" 
] 
+0

非常感谢你,你太快了,现在你是我的新偶像! – Stefano 2010-05-18 13:01:06