在的SelectSingleNode使用XPath:从检索XML单个元素,如果它存在 [英] Using XPath in SelectSingleNode: Retrieving individual element from XML if it's present

查看:736
本文介绍了在的SelectSingleNode使用XPath:从检索XML单个元素,如果它存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML看起来像<?/ p>

My XML looks like :

<?xml version=\"1.0\"?>
<itemSet>
       <Item>one</Item>
       <Item>two</Item>
       <Item>three</Item>
       .....maybe more Items here.
</itemSet>



一些个别的项目的可能会或可能不会出现。说我要检索的元素的 <项目> 两个 < /项目> 的,如果它的存在。我试过以下的XPath(在C#)。

Some of the individual Item may or may not be present. Say I want to retrieve the element <Item>two</Item> if it's present. I've tried the following XPaths (in C#).


  • 的XMLNode节点= myXMLdoc.SelectSingleNode(/套装[项目='两']) ---如果项目的两个的存在,那么它返回我的只有第一个元素的有一个的。也许这只是查询指向在套装的第一要素,如果它有值的两个项目一个地方作为孩子。这是理解是否正确

  • XMLNode node = myXMLdoc.SelectSingleNode("/itemSet[Item='two']") --- If Item two is present, then it returns me only the first element one. Maybe this query just points to the first element in itemSet, if it has an Item of value two somewhere as a child. Is this interpretation correct?

所以,我想:


  • 的XMLNode节点= myXMLdoc.SelectSingleNode(/套装[项目='两'] / [1]项) ---我读此查询作为,回到我的第一的 <项目> 的内套装元素具有价值='二'。我是正确的?

  • XMLNode node = myXMLdoc.SelectSingleNode("/itemSet[Item='two']/Item[1]") --- I read this query as, return me the first <Item> element within itemSet that has value = 'two'. Am I correct?

这仍然只返回的第一个元素的有一个的。我究竟做错了什么?
在这两种情况下,使用的兄弟姐妹,我可以遍历子节点,并获得的两个的,但是这不是我所期待的。此外,如果二是没有那么的SelectSingleNode返回null。因此,张女士我得到一个全成返回节点确实表明元素两人的存在,所以有我想要一个布尔测试中的两个的,上述任何的XPath的就足够了CHK的存在,但我实际需要的全部元素的 <项目>二< /项目方式> 的作为我返回节点

This still returns only the first element one. What am I doing wrong? In both the cases, using the siblings I can traverse the child nodes and get to two, but that's not what I am looking at. Also if two is absent then SelectSingleNode returns null. Thus the very fact that I am getting a successfull return node does indicate the presence of element two, so had I wanted a boolean test to chk presence of two, any of the above XPaths would suffice, but I actually the need the full element <Item>two</Item> as my return node.

[我在这里的第一个问题,我的第一次与Web编程工作,所以我刚学飞上面的XPath和相关的XML的东西,现在从过去的问题在左右。所以,要温柔,让我知道,如果我是一个doofus或藐视任何一个社会规则。谢谢]

推荐答案

我觉得你想要的:

myXMLdoc.SelectSingleNode("/itemSet/Item[text()='two']")

在换句话说,你希望的项目的有没有套装包含它的两个文本。

In other words, you want the Item which has text of two, not the itemSet containing it.

您还可以使用单点表示上下文节点,在您的情况:

You can also use a single dot to indicate the context node, in your case:

myXMLdoc.SelectSingleNode("/itemSet/Item[.='two']")

编辑:。之间的差异 文本() 的含义是节点有效,而文本()表示所有这些节点的文本子节点。在这两种情况的比较将是对LHS的字符串值。对于元素节点,字串值和文本节点的集合的文档顺序的元素节点的所有文本节点后代的字符串值的串联,比较会检查任何文本节点是否等于。一个你对测试

The difference between . and text() is that . means "this node" effectively, and text() means "all the text node children of this node". In both cases the comparison will be against the "string-value" of the LHS. For an element node, the string-value is "the concatenation of the string-values of all text node descendants of the element node in document order" and for a collection of text nodes, the comparison will check whether any text node is equal to the one you're testing against.

因此,它无关紧要的元素内容只有一个文本节点,但假设我们有:

So it doesn't matter when the element content only has a single text node, but suppose we had:

<root>
  <item name="first">x<foo/>y</item>
  <item name="second">xy<foo/>ab</item>
</root>



随后的根/项目[XPath表达式。='XY' ] 将匹配的第一个项目,但根/项目[文本()='XY'] 将匹配第二。

Then an XPath expression of "root/item[.='xy']" will match the first item, but "root/item[text()='xy']" will match the second.

这篇关于在的SelectSingleNode使用XPath:从检索XML单个元素,如果它存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆