xsl-list
[Top] [All Lists]

RE: [xsl] text() & childNodes[0] of xPath

2010-03-10 01:46:39
One path selects the element node, the other selects its only text node
child. If an element has a single text node child, then the string-value of
the element is the same as the string-value of the text node, so the queries
have the same effect. However, if the element also contained a comment in
the middle of the text:

<price>30<!-- illegible, might be 39 -->.00</price>

then getting the string value of the element would give the right answer,
whereas using text() would give you two text nodes - chances are your code
would ignore all but the first. Even though this scenario is unlikely in
your book catalog, it is legal, and I would therefore recommend not using
the "/text()" suffix in the path - it makes your code simpler and more
robust.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay  

-----Original Message-----
From: Sanjaya Liyanage [mailto:sanjayacl(_at_)gmail(_dot_)com] 
Sent: 10 March 2010 06:33
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] text() & childNodes[0] of xPath

Hi,
  While I am going through the examples of Xpath @ w3schools 
site (http://www.w3schools.com/xpath/xpath_examples.asp ) I 
came across this doubt.
  <?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

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

<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

</bookstore>

The above xml file is the resource.

1)path=/bookstore/book/price/text()

and the part of the script is given below.

var nodes=xml.selectNodes(path);

for (i=0;i<nodes.length;i++)
  {
  document.write(nodes[i].nodeValue);
  document.write("<br />");
  }

2)path=/bookstore/book/price

and the part of the script is given below.

var nodes=xml.selectNodes(path);

for (i=0;i<nodes.length;i++)
  {
  document.write(nodes[i].childNodes[0].nodeValue);
  document.write("<br />");
  }
}

In both the above cases the output is same.So I want to know 
whether the above two methods are same in functionality wise 
or are there any significant different between these two methods?

Thanks
Sanjaya.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>