使用复合XPath谓词搜索XML消息
我很难弄清楚如何使用复合XPath查询来选择数据,这样我就基本上可以在数据中找到第5行。以下是我的XML示例:使用复合XPath谓词搜索XML消息,xml,xpath,predicate,Xml,Xpath,Predicate,我很难弄清楚如何使用复合XPath查询来选择数据,这样我就基本上可以在数据中找到第5行。以下是我的XML示例: <sample> <OBX> <field position="1">1</field> <field position="2">My Value</field> </OBX> <OBX> <field position="1">2</field&g
<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>
但这没有任何价值。实现这一目标的正确方法是什么
谢谢
Mike我已经验证了这个XPath表达式:
/*/OBX[field[@position='1']='2']/field[@position='2']
<field position="2">My other (and more important) value</field>
根据提供的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>
<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>
1.
我的价值
2.
我的另一个(也是更重要的)价值观
是否选择所需的节点
下面是一个使用XSLT作为XPath宿主语言的演示:
<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>
<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文档应用此转换时,将生成正确的结果:
/*/OBX[field[@position='1']='2']/field[@position='2']
<field position="2">My other (and more important) value</field>
我的其他(以及更重要的)价值观
我已经验证了这个XPath表达式:
/*/OBX[field[@position='1']='2']/field[@position='2']
<field position="2">My other (and more important) value</field>
根据提供的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>
<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>
1.
我的价值
2.
我的另一个(也是更重要的)价值观
是否选择所需的节点
下面是一个使用XSLT作为XPath宿主语言的演示:
<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>
<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文档应用此转换时,将生成正确的结果:
/*/OBX[field[@position='1']='2']/field[@position='2']
<field position="2">My other (and more important) value</field>
我的其他(以及更重要的)价值观
问得好,+1。看看我的答案,好问题+1。请参阅我的答案以获得解决方案。