Xml 分级列/名称值属性的XPath?
我试图找到下面XML的XPath。它由属性数组列表组成。我需要取“计算机”的值Xml 分级列/名称值属性的XPath?,xml,xpath,soa,Xml,Xpath,Soa,我试图找到下面XML的XPath。它由属性数组列表组成。我需要取“计算机”的值 <output xmlns:tns="http://www.ariuy.org/" custname="marcus" > <tns:column name="customer_english_name">marcus ag</tns:column> <tns:column name="c
<output xmlns:tns="http://www.ariuy.org/" custname="marcus" >
<tns:column name="customer_english_name">marcus ag</tns:column>
<tns:column name="customer_primary_name">marcus ag</tns:column>
<tns:reqline>
<tns:orderline user_item_description="xyz">
<tns:column name="properties">
<tns:column name="name">COMPUTER</tns:column>
<tns:column name="value">HCL </tns:column>
</tns:column>
<tns:column name="properties">
<tns:column name="name">LAPTOP</tns:column>
<tns:column name="value">HP</tns:column>
</tns:column>
<tns:column name="properties">
<tns:column name="name">PHONE</tns:column>
<tns:column name="value">MI</tns:column>
</tns:column>
<tns:column name="properties">
<tns:column name="name">JOB</tns:column>
<tns:column name="value">Developer</tns:column>
</tns:column>
......
</tns:orderline>
</tns:reqline>
</output>
但是它不起作用。假设您有名称空间前缀
tns
来缩写http://www.ariuy.org/
命名空间值
这个XPath
/output/tns:reqline/tns:orderline
/tns:column[@name='properties'][tns:column[@name='name']='COMPUTER']
/tns:column[@name='value']
将选择
<tns:column name="value">HCL </tns:column>
HCL
根据要求。要查看fecthHCL
(计算机下的值),您可以选择:
(//*[name()="tns:column"][@name="properties"])[1]/*[2]/text()
查找具有页面特定属性的第一列元素。然后选择它的第二个子对象
或更安全的选项:
//*[name()="tns:column"][preceding-sibling::*[name()="tns:column"][.="COMPUTER"]]/text()
在任何位置查找列元素,其中前面的同级元素包含值“COMPUTER”。您说:
/tns:output/tns:reqline/tns:OrderLine/tns:properties[@name='COMPUTER']/@value
嗯
(a) 输出
元素不在tns命名空间中
(b) orderline
元素以o
开头,而不是o
(c) 您没有名为tns:properties的元素
(d) 没有值为“计算机”的@name
属性
(e) 任何地方都没有@value
属性
这是一行代码的许多错误
其他人给了你有效的解决方案,但我认为你必须理解你的尝试为什么是错误的。你到底想获取什么价值?Downvoter:告诉我你认为是错误的,我会纠正或解释。谢谢,谢谢。。但如果您犯了错误,它会为meThen返回null值,这可能与名称空间前缀声明有关。请参阅提供的链接以更正该问题。这些答案是一类答案的特征,可以比我发布的答案更短、更一般(+1),但会牺牲效率,这对于典型文档来说根本不重要,但可以用于数据库转储或其他大型文档。嗯……没有称为属性的元素,但有元素数组。我需要获取计算机的价值根据你,路径应该是什么?在修复OP的XPath时,我在心里开始了这样一个列表,但失去了方向,决定不值得因为这么多令人震惊的错误而返回。有趣的是,现在无论是要修复的错误列表还是可行的解决方案都不合适。@Anurag,对于这样一个问题,我更愿意鼓励您自己解决问题,而不是为您解决问题。对不起,如果我在这里有点像保姆,但我认为这样可以更好地利用我的时间。
//*[name()="tns:column"][preceding-sibling::*[name()="tns:column"][.="COMPUTER"]]/text()