472,971 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,971 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 2069
..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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.