【问题标题】:Using compound XPath Predicates to search XML message使用复合 XPath 谓词搜索 XML 消息
【发布时间】:2010-10-18 20:32:54
【问题描述】:

我无法弄清楚如何使用复合 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']

但这没有任何价值。完成此任务的正确方法是什么?

谢谢,

迈克

【问题讨论】:

  • 好问题,+1。请参阅我的答案以获得解决方案。

标签: xml xpath predicate


【解决方案1】:

我已经验证了这个 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多