使用xpath获取子节点? [英] Getting child nodes using xpath?

查看:190
本文介绍了使用xpath获取子节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml。我需要使用xpath查询获取根节点的所有子节点。如何编写xpath表达式?

I have the following xml. I need to get all the child nodes of root node using an xpath query. how can i write the xpath expression?

<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>

我尝试使用 rootElement / rootElementOne / * 但没有结果。

I tried using rootElement/rootElementOne/* but no result.

谢谢!

推荐答案

请注意这里的术语。在XML中,至少在XPath术语中,根节点是文档中所有元素,文本节点,注释,处理指令和其他节点的(不可见)祖先。根节点由XPath表达式 / 寻址。它不是元素,而是最外层元素的父元素,a.k.a。文档元素。在您的XML文档中,根节点是< rootElement> 的父节点。

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. The root node is addressed by the XPath expression /. It is not an element, but is the parent of the outermost element, a.k.a. the document element. In your XML document, the root node is the parent of <rootElement>.

所有子节点根节点将由此XPath表达式选择:

All "child nodes of the root node" would be selected by this XPath expression:

/node()

但这会返回一个元素,即< rootElement> ,这不是结果你想要的。

but that would return one element, namely <rootElement>, which is not the result you want.

相反,你可能想要文档元素的所有子节点,所以这是你的XPath表达式:

Instead, you probably want all child nodes of the document element, so this is your XPath expression:

/*/node()

这将返回< rootElementOne> 元素,以及(取决于您的设置)它们之间的文本节点,它由空格组成。

This will return the <rootElementOne> elements, and (depending on your settings) also the text node between them, which consists of whitespace.

或者,也许您想要文档元素的所有元素子元素。换句话说,除了元素之外,您不关心文本节点,注释或任何其他内容。 (许多不熟悉XML细节的人在表示元素节点时会说节点。)

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".)

如果这就是你想要的,XPath表达式因为它是

If that's what you want, the XPath expression for it is

/*/*

或者在你的情况下,你可以做到

or in your case, you could do

/rootElement/some:rootElementOne

其中某些在XPath外部声明为命名空间 http://some.com 的前缀。如果您想知道如何在Java中声明XPath的名称空间前缀,请告诉我们,并向我们展示您已经用于调用XPath的Java代码。或者更好的是,在这个网站上搜索,因为示例代码已经有了很好的答案。

where some is declared outside the XPath as a namespace prefix for 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. Or better yet, search on this site because there are already good answers with example code.

当您尝试 rootElement / rootElementOne / * ,由于命名空间,你没有选择任何东西。形式为 rootElementOne 的XPath步骤(在XPath 1.0中)表示没有命名空间中名为rootElementOne的元素。 (在XPath 2.0中,它表示在默认的XPath命名空间中,并且XPath之外的方法可以设置默认的XPath名称空间。)所以你要求 rootElementOne 命名空间,而您的< rootElementOne> 元素位于 http://some.com 命名空间中。

When you tried rootElement/rootElementOne/*, you selected nothing, because of namespaces. An XPath step of the form rootElementOne (in XPath 1.0) means "an element named rootElementOne in no namespace." (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.

如果您想与命名空间无关,可以使用 * 而不是 rootElementOne ,或者您可以使用 * [local-name()='rootElementOne'] 。但是,如果你这样做是因为你不知道如何在XML和XPath中使用命名空间,那么在你学习之前,它们可能会继续成为你肉体中的刺。 : - )

If you want to be namespace-agnostic, you can use * instead of rootElementOne, or you could use *[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. :-)

一旦修复,你应该得到两个< some:rootElementTwo> 元素(因为你要求 rootElementOne 的子元素,但这只能在文档的根节点的上下文中工作。那是因为以元素名称 X 开头的XPath表达式实际上是从 child :: X 开始的,这意味着孩子上下文节点。如果您当时不知道上下文节点是什么,或者不想依赖它,请使用 / 或<$ c $启动XPath表达式C> // 。这告诉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. 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. 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 //. That tells XPath to start from the root node of the document.

这篇关于使用xpath获取子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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