简体   繁体   English

关于w3school提供的XPath教程的问题

[英]Questions about the XPath tutorial given by w3school

Here is the link http://www.w3schools.com/xpath/xpath_examples.asp 这是链接http://www.w3schools.com/xpath/xpath_examples.asp

So clicked on the first try it yourself , you will see a fragment of code. 因此,单击第一个自己尝试一下 ,您将看到一段代码。 And I don't know what this part means 我不知道这是什么意思

while (result)
{
    document.write(result.childNodes[0].nodeValue);
    document.write("<br>");
    result=nodes.iterateNext();
}

The result there should be the least significant node there and should hava no childNodes. 结果那里应该有最低有效节点,并且应该没有子节点。 And according to the W3C standard, the first node should be denoted as node[1]. 并且根据W3C标准,第一个节点应表示为node [1]。 So I started to think that the atomic value is a node, and then made some modifications to the code. 因此,我开始认为原子值是一个节点,然后对代码进行了一些修改。

I changed xpath expression and the while loop 我更改了xpath表达式和while循环

  path="/bookstore/book";
  ...
  // original codes
  ...
  while (result)
  {
      document.write(result.childNodes[1].childNodes[0].nodeValue);
      document.write("<br>");
      result=nodes.iterateNext();
  }
  /* result
    Everyday Italian
    Harry Potter
    XQuery Kick Start
    Learning XML
  */

Seems it works well. 似乎效果很好。 Then another modification: 然后再进行修改:

  path="/bookstore/book";
  ...
  // original codes
  ...
  while (result)
  {
      // the only change is here      **
      document.write(result.childNodes[2].childNodes[0].nodeValue);
      document.write("<br>");
      result=nodes.iterateNext();
  }
  /* result
    blank
  */

Seems strange now, I expected a list of authors, but what I have here is blank. 现在看来很奇怪,我希望能看到作者列表,但是这里的内容是空白。 Then another tiny modification: 然后是另一个小的修改:

      document.write(result.childNodes[3].childNodes[0].nodeValue);
 /* result
   Giada De Laurentiis
   J K. Rowling
   James McGovern
   Erik T. Ray
 */

The childNode[1] denotes the fisrt childNode of book - title, and the childNode[3] denoted the second - author? childNode [1]表示书籍的第一子节点-标题,而childNode [3]表示第二个-作者? Then I was convinced that the childNode[2] should be the atomic value of title . 然后,我确信childNode [2]应该是title的原子值。 But when I tried to display it using childNode[2] and failed, I won't show you more codes - already a load of codes here. 但是,当我尝试使用childNode [2]显示它并失败时,我不会再显示更多代码-这里已经是很多代码了。 So, can any one explain it to me? 那么,有人可以向我解释吗?

And another question: 还有一个问题:

see link here http://www.w3schools.com/xpath/xpath_axes.asp 请参阅此处的链接http://www.w3schools.com/xpath/xpath_axes.asp

It introduced xpath axes and Location Path Expression , but no example is given within a code. 它引入了xpath轴和“ 位置路径表达式” ,但是在代码中未给出示例。 I can hardly imagine how to use it, may anyone posts some examples and enlighten me? 我几乎无法想象如何使用它,有人可以举一些例子并启发我吗? (I mainly use java) (我主要使用java)

You are confusing the DOM model and the XPath model. 您在混淆DOM模型和XPath模型。 In the DOM model a node has a childNodes property where the index starts with 0 and where the childNodes contains all kind of nodes (element nodes, text nodes, CDATA section nodes, comment nodes, processing instruction nodes). 在DOM模型中,节点具有childNodes属性,其中索引以0开头,并且childNodes包含所有类型的节点(元素节点,文本节点,CDATA节节点,注释节点,处理指令节点)。 The XPath data model is different and its path expressions are different from the DOM child nodes. XPath数据模型与DOM子节点不同,并且其路径表达式也不同。 Positional predicates in XPath start with 1 , not with 0 . XPath中的位置谓词以1开头,而不是以0开头。 Your sample uses XPath to address book elements in a DOM tree and then changes to address child nodes using the DOM API. 您的示例使用XPath寻址DOM树中的book元素,然后使用DOM API进行更改以寻址子节点。 That is often done in the browser environment as the browsers implement the W3C DOM but sometimes offer XPath selection over the DOM tree. 这通常是在浏览器环境中完成的,因为浏览器实现了W3C DOM,但有时会在DOM树上提供XPath选择。

And you have to understand that in both the DOM and the XPath model with <foo>bar</foo> there is foo element node with a child node, a text node with contents bar . 而且您必须了解,在具有<foo>bar</foo>的DOM和XPath模型中,都存在带有子节点的foo元素节点,以及带有内容bar的文本节点。 The examples you see are using XPath to select an element node, then sometimes use the childNodes collection to address a text child node and to extract its DOM nodeValue property. 您看到的示例使用XPath选择元素节点,然后有时使用childNodes集合寻址文本子节点并提取其DOM nodeValue属性。 At least for the Mozilla, Opera, Chrome branch of the code that is not necessary, instead of foo.childNodes[0].nodeValue you could simply access foo.textContent . 至少对于不需要代码的Mozilla,Opera,Chrome分支,可以直接访问foo.textContent而不是foo.childNodes[0].nodeValue But all that is DOM and not XPath. 但是,所有这些都是DOM而不是XPath。

A tree for 一棵树

<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

can have a book element node which has 9 child nodes, the first (with index 0 in the DOM childNodes collection, with index 1 in XPath ./child::node() ) being a text node with white space, the second being the title element, the third being a text node with white space, the fourth (with DOM index 3 ) being the author element. 可以有一个book元素节点,该节点具有9个子节点,第一个(文本节点在DOM childNodes集合中的索引为0 ,在XPath ./child::node()索引为1 )是一个带有空白的文本节点,第二个是节点title元素,第三个是带有空白的文本节点,第四个(DOM索引为3 )是author元素。

Of course with XPath, if you know you are only interested in element nodes you would not select node()[4] , instead you would select *[2] for the second element child or simply author for the author element child node. 当然,对于XPath,如果知道只对元素节点感兴趣,则不会选择node()[4] ,而是将*[2]用于第二个元素子节点,或者仅选择author作为author元素子节点。

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

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