使用复合 XPath 谓词搜索 XML 消息

发布于 2024-09-27 15:48:56 字数 617 浏览 2 评论 0原文

我无法弄清楚如何使用复合 XPath 查询选择数据,以便基本上可以找到数据中的第 5 列行。这是我的示例 XML:

<sample>
 <OBX>
   <field position="1">1</field>
   <field position="2">My Value</field>
 </OBX>
 <OBX>
   <field position="1">2</field>
   <field position="2">My other (and more important) value</field>
 </OBX>
</sample>

基本上,我需要做的就是在 [field @position='1'] 等于 2 时找到 [field @position='2'] 的值。我正在尝试:

OBX[./field[@position='1']='2']/field[@position='2']

但这并不提出任何价值。实现这一目标的正确方法是什么?

谢谢,

迈克

I'm having trouble figuring out how to select data using a compound XPath query so that I can essentially find the 5th column of row in my data. Here is my example XML:

<sample>
 <OBX>
   <field position="1">1</field>
   <field position="2">My Value</field>
 </OBX>
 <OBX>
   <field position="1">2</field>
   <field position="2">My other (and more important) value</field>
 </OBX>
</sample>

Basically all I need to do is find the value of [field @position='2'] when [field @position='1'] is equal to 2. I am trying:

OBX[./field[@position='1']='2']/field[@position='2']

but this doesn't come up with any value. What would be the proper way to get this accomplished?

Thanks,

Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅 2024-10-04 15:48:56

我已经验证了这个 XPath 表达式

/*/OBX[field[@position='1']='2']/field[@position='2']

根据提供的 XML 文档进行评估时:

<sample>
 <OBX>
   <field position="1">1</field>
   <field position="2">My Value</field>
 </OBX>
 <OBX>
   <field position="1">2</field>
   <field position="2">My other (and more important) value</field>
 </OBX>
</sample>

确实选择了所需的节点

这里是一个演示,使用 XSLT 作为 XPath 托管语言:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select="/*/OBX[field[@position='1']='2']/field[@position='2']"/>
 </xsl:template>
</xsl:stylesheet>

当针对提供的 XML 文档应用此转换时,会生成正确的结果

<field position="2">My other (and more important) value</field>

I have verified that this XPath expression:

/*/OBX[field[@position='1']='2']/field[@position='2']

when evaluated against the provided XML document:

<sample>
 <OBX>
   <field position="1">1</field>
   <field position="2">My Value</field>
 </OBX>
 <OBX>
   <field position="1">2</field>
   <field position="2">My other (and more important) value</field>
 </OBX>
</sample>

does select the required node

Here is a demonstration, using XSLT as the XPath hosting language:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select="/*/OBX[field[@position='1']='2']/field[@position='2']"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied against the provided XML document, the correct result is produced:

<field position="2">My other (and more important) value</field>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文