2013-11-04 55 views
1

我正在尝试使用C#中的XPath查询解析一些XML数据。但我的查询没有找到我正在寻找的元素(它什么也没找到)。查找XPath查询特定元素失败

我的XPath查询有什么问题? following-sibling的语法不正确吗?如何编辑我的XPath以找到正确的value元素?

<attributes> 
    <real> 
    <name>cover rl</name> 
    <value>89.87414122</value> 
    </real> 
    <real> 
    <name>pit depth</name> 
    <value>2.35620671</value> <!-- This is the value I need --> 
    </real> 
<attributes> 

我的XPath查询失败:

ns:attributes/real/name[text() = 'pit depth']/following-sibling::value 
+0

请加入语言和实际的代码示例 - 所有的语言/库有他们自己的处理方式名称空间前缀/声明。 (还不清楚为什么你甚至需要一个给定的XML)。 –

回答

1

你靠近。大多是摆脱虚假ns:命名空间前缀。另外请注意,您的样品输入XML应该关闭</attributes>元素,而不是另一个开口<attributes>元素

所以,这个XPath结束:

/attributes/real/name[. = 'pit depth']/following-sibling::value 

将产生:

<value>2.35620671</value> 

根据您的要求。

如果你只想要value元素的内容:

/attributes/real/name[. = 'pit depth']/following-sibling::value/text() 

将产生:

2.35620671