如何使用xpath检索xml值?

时间:2021-08-11 01:34:55

My Scenario : I may get the different outputs which is shown below.I want to retrieve the "Units" tag value depends on Code tag.

我的场景:我可能得到不同的输出,如下所示。我想要检索“单位”标记值取决于代码标记。

Output1 :

Output1:

<Riders>
<Rider>
  <Name>ALSP</Name>
  <Code>ALSP</Code>
  <Units>3</Units>
</Rider>
<Rider>
  <Name>Individual</Name>
  <Code>Select Type of Coverage</Code>
  <OptionCode>Individual</OptionCode>
  <IsFeature>true</IsFeature>
</Rider>
</Riders>

Output2 :

Output2:

<Riders>
<Rider>
  <Name>AFO</Name>
  <Code>AFO</Code>
  <Units>6</Units>
</Rider>
<Rider>
  <Name>Individual</Name>
  <Code>Select Type of Coverage</Code>
  <OptionCode>Individual</OptionCode>
  <IsFeature>true</IsFeature>
</Rider>
</Riders>

I have tried below xpath but didn't worked out. Could anyone suggest me.

我尝试过xpath,但没有成功。有人建议我。

/Riders/Rider/Code[text()[contains(.,'AFO')]  or text()     [contains(.,'ALSP')]]/Units

1 个解决方案

#1


1  

Depending on if you want to get the node with regards to the root or just find any matching node, you could use something like...

根据您想要获得与根节点相关的节点,或者只是找到任何匹配的节点,您可以使用以下方法……

*/Rider[Code[contains(.,'ALSP')]]/Units

Which will return you all the Units nodes which belong to any Rider node, which have a Code node, whose text contains ALSP

哪个将返回属于任何附加节点的所有单元节点,该节点具有包含ALSP的代码节点

Of course you could also use...

当然你也可以用……

*/Rider[Code[text() = 'ALSP']]/Units

If the Code must match exactly.

如果代码必须完全匹配。

The above will find all the Units nodes of the Rider node anywhere in the document. If the position is important, you would need to replace */ with /Riders/ instead.

上面的内容将在文档的任何地方找到附加节点的所有单元节点。如果这个职位很重要,你需要用/骑手/代替。

Now, if you want to find both ALSP and AFO, you could use something like...

现在,如果你想同时找到ALSP和AFO,你可以用类似的东西……

*/Rider[Code[text() = 'ALSP' or text() = 'AFO']]/Units

#1


1  

Depending on if you want to get the node with regards to the root or just find any matching node, you could use something like...

根据您想要获得与根节点相关的节点,或者只是找到任何匹配的节点,您可以使用以下方法……

*/Rider[Code[contains(.,'ALSP')]]/Units

Which will return you all the Units nodes which belong to any Rider node, which have a Code node, whose text contains ALSP

哪个将返回属于任何附加节点的所有单元节点,该节点具有包含ALSP的代码节点

Of course you could also use...

当然你也可以用……

*/Rider[Code[text() = 'ALSP']]/Units

If the Code must match exactly.

如果代码必须完全匹配。

The above will find all the Units nodes of the Rider node anywhere in the document. If the position is important, you would need to replace */ with /Riders/ instead.

上面的内容将在文档的任何地方找到附加节点的所有单元节点。如果这个职位很重要,你需要用/骑手/代替。

Now, if you want to find both ALSP and AFO, you could use something like...

现在,如果你想同时找到ALSP和AFO,你可以用类似的东西……

*/Rider[Code[text() = 'ALSP' or text() = 'AFO']]/Units