xslt 和 xpath:匹配前面的注释 [英] xslt and xpath: match preceding comments
问题描述
给定这个xml:
<root>
<list>
<!-- foo's comment -->
<item name="foo" />
<item name="bar" />
<!-- another foo's comment -->
<item name="another foo" />
</list>
</root>
我想使用 XPath 来选择所有紧跟在它们之前有注释的项目节点,也就是说,我喜欢选择foo"和另一个 foo"项目,而不是bar"项目.
I'd like to use a XPath to select all item-nodes that have a comment immediately preceding them, that is I like to select the "foo" and "another foo" items, but not the "bar" item.
我已经摆弄过前面的兄弟轴和 comment() 函数,但无济于事.
I already fiddled about the preceding-sibling axis and the comment() function but to no avail.
推荐答案
这似乎有效:
//comment()/following-sibling::*[1]/self::item
它寻找紧随其后的注释兄弟姐妹,这些兄弟姐妹也是
元素.我不知道更好的方式来表达 ::*[1]/self::item
部分,这很丑陋;请注意,如果它是写成 ::item[1]
那么它也会发现
s not immediately 由注释处理.
It looks for immediately following siblings of comments which are also <item>
elements. I don't know a better way to express the ::*[1]/self::item
part, which is ugly; note that if it were written ::item[1]
then it would also find <item>
s not immediately proceded by a comment.
这篇关于xslt 和 xpath:匹配前面的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!