473,387 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

xpath query "//b[1]//c" gives different results when b is nested inanother tag

<?php
$content = '
<a>
<d>
<b>
<c/><c/>
</b>
</d>
<b>
<c/><c/><c/>
</b>
<b>
<c/>
</b>
</a>';
$content = '
<a>
<b>
<c/><c/>
</b>
<b>
<c/><c/><c/>
</b>
<b>
<c/>
</b>
</a>';
$xml = new DOMDocument();
$xml->loadXML($content);
$xpath = new DOMXPath($xml);

$result = $xpath->query('//b[1]//c');
echo $result->length;
?>

Why is it that the first $content gives a different answer than the
second $content? Shouldn't they both be the same? Shouldn't they
both be 2? What I'm getting, instead, is that the former gives me 5
and the latter, 2. Which doesn't make any sense to me.

Any ideas?
Sep 14 '08 #1
1 2102
..oO(yawnmoth)
><?php
$content = '
<a>
<d>
<b>
<c/><c/>
</b>
</d>
<b>
<c/><c/><c/>
</b>
<b>
<c/>
</b>
</a>';
$content = '
<a>
<b>
<c/><c/>
</b>
<b>
<c/><c/><c/>
</b>
<b>
<c/>
</b>
</a>';
$xml = new DOMDocument();
$xml->loadXML($content);
$xpath = new DOMXPath($xml);

$result = $xpath->query('//b[1]//c');
echo $result->length;
?>

Why is it that the first $content gives a different answer than the
second $content? Shouldn't they both be the same? Shouldn't they
both be 2? What I'm getting, instead, is that the former gives me 5
and the latter, 2. Which doesn't make any sense to me.
//b[1] matches the first 'b' child in a node set anywhere in the tree.
In the first example this matches two elements: the first 'b' is the
first child of the 'd' element, while the next 'b' is the first child of
'a'. So the result contains 5 'c' elements.

In the second example all 'b's are on the same level. Only the first one
matches your query, so you'll get 2 'c' as the result.

Your current XPath query is equivalent to this more verbose form:

/descendant-or-self::node()/b[1]//c

You could try this instead:

/descendant::b[1]//c

Micha
Sep 19 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
1
by: dima | last post by:
I'm using msxml3 xml looks like this: <a desc="&quot;B&quot;" /> how to query it in xpath? here's what i tried: a -- This works under XSL Patterns, but not under Xpath (using msxml 3) a --this...
8
by: Hostile17 | last post by:
Consider the following HTML. ---------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> <html> <head> <meta...
7
by: Scott W Gifford | last post by:
Hello, I'm considering using XML to represent a stream of location information, and XPath to do queries against it. I've got most of it figured out (at least on paper), but I can't figure out...
4
by: Greg | last post by:
I keep getting an error when I have a tick mark in a text value that I am searching for in my XPath Query. Example: <Authors> <Author LastName="O'Donnel"> <Author LastName="Smith">...
3
by: jmagaram | last post by:
I have a DataSet I want to work with as Xml using XmlDataDocument. I can't figure out how to query the resultant Xml using XPath. From the following XML below, what XPath query will return the list...
0
by: passion | last post by:
"Specialized Search Engines" along with Google Search Capability (2 in 1): http://specialized-search-engines.blogspot.com/ Billions of websites are available on the web and plenty of extremely...
1
by: yawnmoth | last post by:
<a> <b> <c/><c/> </b> <b> <c/><c/><c/> </b> <b> <c/> </b>
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.