简体   繁体   English

使用xpath获取子节点?

[英]Getting child nodes using xpath?

I have the following xml. 我有以下xml。 I need to get all the child nodes of root node using an xpath query. 我需要使用xpath查询获取根节点的所有子节点。 how can i write the xpath expression? 我该如何写xpath表达式?

<rootElement> 

  <rootElementOne xmlns="http://some.com"> 
    <rootElementTwo> 
      <Id>12345</balId> 
      <name>Name1</businessName> 
     </rootElementTwo> 
  </rootElementOne> 

  <rootElementOne xmlns="http://some.com"> 
    <rootElementTwo> 
      <Id>6789</balId> 
      <name>Name2</businessName> 
     </rootElementTwo> 
  </rootElementOne>  

</rootElement>

The expression should return the result below: 该表达式应返回以下结果:

      <rootElementOne xmlns="http://some.com"> 
        <rootElementTwo> 
          <Id>12345</balId> 
          <name>Name1</businessName> 
         </rootElementTwo> 
      </rootElementOne> 

      <rootElementOne xmlns="http://some.com"> 
        <rootElementTwo> 
          <Id>6789</balId> 
          <name>Name2</businessName> 
         </rootElementTwo> 
      </rootElementOne>

I tried using rootElement/rootElementOne/* but no result. 我尝试使用rootElement/rootElementOne/*但没有结果。

Thanks! 谢谢!

Watch out for terminology here. 在这里提防术语。 In XML, at least in XPath terminology, the "root node" is the (invisible) ancestor of all elements, text nodes, comments, processing instructions, and other nodes in a document. 在XML中,至少在XPath术语中,“根节点”是文档中所有元素,文本节点,注释,处理指令和其他节点的(不可见)祖先。 The root node is addressed by the XPath expression / . 根节点由XPath表达式/寻址。 It is not an element, but is the parent of the outermost element , aka the document element . 它不是元素,而是最外层元素 (也称为document元素 )的父元素 In your XML document, the root node is the parent of <rootElement> . 在XML文档中,根节点是<rootElement>的父节点。

All "child nodes of the root node" would be selected by this XPath expression: 此XPath表达式将选择所有“根节点的子节点”:

/node()

but that would return one element, namely <rootElement> , which is not the result you want. 但这将返回一个元素,即<rootElement> ,这不是您想要的结果。

Instead, you probably want all child nodes of the document element , so this is your XPath expression: 相反,您可能需要document元素的所有子节点,所以这是您的XPath表达式:

/*/node()

This will return the <rootElementOne> elements, and (depending on your settings) also the text node between them, which consists of whitespace. 这将返回<rootElementOne>元素,以及(取决于您的设置)它们之间的文本节点,该文本节点由空格组成。

Alternatively, maybe you want all element children of the document element. 或者,也许你想要的文档元素的所有子元素 In other words, you don't care about text nodes, comments, or anything besides elements. 换句话说,除了元素之外,您无需关心文本节点,注释或其他任何内容。 (A lot of people who are unfamiliar with the details of XML say "node" when they mean "element node".) (许多不熟悉XML细节的人在表示“元素节点”时都说“节点”。)

If that's what you want, the XPath expression for it is 如果这就是您想要的,则它的XPath表达式是

/*/*

or in your case, you could do 或者您可以

/rootElement/some:rootElementOne

where some is declared outside the XPath as a namespace prefix for http://some.com . 其中some在XPath外部声明为http://some.com的名称空间前缀。 If you want to know how to declare a namespace prefix for XPath in Java, let us know, and show us what Java code you're already using to call XPath. 如果您想知道如何在Java中声明XPath的名称空间前缀,请告诉我们,并向我们展示您已经在使用什么Java代码来调用XPath。 Or better yet, search on this site because there are already good answers with example code. 或者更好的是,在此站点上搜索,因为示例代码已经有了很好的答案。

When you tried rootElement/rootElementOne/* , you selected nothing, because of namespaces. 尝试使用rootElement/rootElementOne/* ,由于名称空间rootElement/rootElementOne/*选择任何内容。 An XPath step of the form rootElementOne (in XPath 1.0) means "an element named rootElementOne in no namespace." 形式为rootElementOne的XPath步骤(在XPath 1.0中)表示“没有命名空间的名为rootElementOne的元素”。 (In XPath 2.0, it means "in the default XPath namespace," and there are ways outside of XPath to set the default XPath namespace.) So you asked for rootElementOne in no namespace, whereas your <rootElementOne> elements are in the http://some.com namespace. (在XPath 2.0中,它的意思是“在默认XPath命名空间中”,并且XPath之外还有其他方法可以设置默认XPath命名空间。)因此,您在没有命名空间的情况下都要求rootElementOne ,而您的<rootElementOne>元素在http://some.com命名空间。

If you want to be namespace-agnostic, you can use * instead of rootElementOne , or you could use *[local-name() = 'rootElementOne'] . 如果要与名称空间无关,可以使用*代替rootElementOne ,也可以使用*[local-name() = 'rootElementOne'] However, if you do this because you don't know how to use namespaces in XML and XPath, they will probably continue to be a thorn in your flesh until you learn. 但是,如果这样做是因为您不知道如何在XML和XPath中使用命名空间,那么在学习之前,它们可能仍然是您的绊脚石。 :-) :-)

Once that is fixed, you should get two <some:rootElementTwo> elements (because you asked for child elements of rootElementOne ), but this would only work in the context of the root node of the document. 修复此问题后,您应该获得两个<some:rootElementTwo>元素(因为您需要rootElementOne子元素),但这仅在文档根节点的上下文中有效。 That's because an XPath expression that starts with an element name X is really starting with child::X , meaning the child of the context node. 这是因为以元素名称X开头的XPath表达式实际上是以child::X开头的,这意味着上下文节点的子元素。 If you don't know what the context node is at the time, or don't want to be dependent on it, start your XPath expression with / or // . 如果您当时不知道上下文节点是什么,或者不想依赖它,请使用///启动XPath表达式。 That tells XPath to start from the root node of the document. 这告诉XPath从文档的根节点开始。

/rootElement/* will return all rootElementOne where you can do some action on /rootElement/*将返回所有rootElementOne ,您可以在其中执行一些操作

/rootElement//* will return everything below rootElement /rootElement//*将返回rootElement以下的所有内容

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

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