简体   繁体   English

在SelectSingleNode中使用XPath:从XML中检索单个元素(如果存在)

[英]Using XPath in SelectSingleNode: Retrieving individual element from XML if it's present

My XML looks like : 我的XML看起来像:

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

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. 假设我想要检索元素<Item> two </Item>如果它存在)。 I've tried the following XPaths (in C#). 我尝试了以下XPath(在C#中)。

  • XMLNode node = myXMLdoc.SelectSingleNode("/itemSet[Item='two']") --- If Item two is present, then it returns me only the first element one . XMLNode node = myXMLdoc.SelectSingleNode("/itemSet[Item='two']") ---如果第2项存在,那么它只返回第一个第一个元素。 Maybe this query just points to the first element in itemSet, if it has an Item of value two somewhere as a child. 也许这个查询只指向itemSet中的第一个元素,如果它在某个地方有一个值为2的Item作为子元素。 Is this interpretation correct? 这种解释是否正确?

So I tried: 所以我尝试过:

  • 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'. XMLNode node = myXMLdoc.SelectSingleNode("/itemSet[Item='two']/Item[1]") ---我读了这个查询,返回itemSet中第一个具有value ='two'的<Item>元素。 Am I correct? 我对么?

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. 如果两个不存在,则SelectSingleNode返回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. 因此,我获得一个成功的返回节点的事实确实表明存在元素二,所以如果我想要一个布尔测试chk存在两个 ,任何上述XPath就足够了,但实际上我需要完整的元素<Item>two</Item>作为我的返回节点。

[My first question here, and my first time working with web programming, so I just learned the above XPaths and related xml stuff on the fly right now from past questions in SO. [我的第一个问题,也是我第一次使用网络编程,所以我刚刚从过去的SO问题中学到了上面的XPath和相关的xml内容。 So be gentle, and let me know if I am a doofus or flouting any community rules. 所以请保持温和,让我知道如果我是一个doofus或蔑视任何社区规则。 Thanks.] 谢谢。]

I think you want: 我想你想要:

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

In other words, you want the Item which has text of two, not the itemSet containing it. 换句话说,您需要具有两个文本的Item ,而不是包含它的itemSet

You can also use a single dot to indicate the context node, in your case: 在您的情况下,您还可以使用单个点来指示上下文节点:

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

EDIT: The difference between . 编辑:之间的区别. and text() is that . text()就是这样. means "this node" effectively, and text() means "all the text node children of this node". 有效地表示“此节点”, text()表示“此节点的所有文本节点子节点”。 In both cases the comparison will be against the "string-value" of the LHS. 在这两种情况下,比较都将与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. 对于元素节点,string-value是“文档顺序中元素节点的所有文本节点后代的字符串值的串联”,对于文本节点的集合,比较将检查是否有任何文本节点等于你要测试的那个。

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>

Then an XPath expression of " root/item[.='xy'] " will match the first item, but " root/item[text()='xy'] " will match the second. 然后XPath表达式“ root/item[.='xy'] ”将匹配第一项,但“ root/item[text()='xy'] ”将匹配第二项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM