我有一个给定的XML文档(结构不能改变),并希望得到那些节点上面写的评论。文档看起来像这样:

<!--Some comment here-->    
    <attribute name="Title">Book A</attribute>
    <attribute name="Author">
       <value>Joe Doe</value>
       <value>John Miller</value>
    </attribute>
<!--Some comment here-->
    <attribute name="Code">1</attribute>

所以注释是可选的,但如果有一个,我想每个属性上面的评论。 使用/*/comment()[n]会给我建议N,但对于n = 2我自然会得到第三个属性的评论,所以属性之间没有任何关系,评论任何想法? 感谢

有帮助吗?

解决方案

使用

//comment()[following-sibling::*[1][self::attribute]]

<强>这是更紧凑,比当前选择的答案精确。所述//缩写是必要的,因为没有简洁(wellformed)XML文档提供和注释节点的嵌套级别是不知道。

其他提示

如果您想选择后跟一个attribute元素的评论,那么这应该工作:

/*/comment()[following-sibling::*[position()=1 and name()='attribute']]
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top