473,466 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

php5: question about xpath

Hi,

I couldnt find an answer to my question in the php manual, so i try it
here.
Basically i just want to know if the following is possible.
Suppose you have this xml file:

<root>
<news id="1">
<title>Test</title>
<message>Just a test</message>
</news>
<news id="2">
<title>Test 2</title>
<message>yet another test</message>
</news>
</root>

And then you do the following xpath query:

$nodes = $xpath->query("./news/*", $dom->documentElement);

Now, how can i tell how many childs news has? Because $nodes has 4
elements, so one news element could have 4 childs or there are 2 news
elements with 2 childs...
Is there a solution?
Jul 17 '05 #1
2 1415
Börni wrote:
Now, how can i tell how many childs news has? Because $nodes has 4
elements, so one news element could have 4 childs or there are 2 news
elements with 2 childs...
Is there a solution?


<?php

// Get the context nodes
$nodes = $xsl->query("//root/news");
$pos = 0;

while (++$pos < ($nodes->length + 1)) {
$children = $xsl->query(
"descendant::*",
$nodes->item($pos - 1)
);
print "//news[$pos] has ";
print $children->length;
print " child elements<br />";
}

?>
JW

Jul 17 '05 #2
On 2005-01-25, Börni <b.******@onlinehome.de> wrote:
Suppose you have this xml file:
[snip]
$nodes = $xpath->query("./news/*", $dom->documentElement);
Now, how can i tell how many childs news has? Because $nodes has 4
elements, so one news element could have 4 childs or there are 2 news
elements with 2 childs...
Is there a solution?

Have a look at the count function for xpath.

--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #3

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

Similar topics

3
by: Philipp Lenssen | last post by:
I noticed there's an error in PHP when it receives form data. It seems to precede various characters with "\". I was wondering if that's about to get fixed in PHP5, or already is fixed in PHP4? (Or...
8
by: Rob Ristroph | last post by:
I have tried out PHP 5 for the first time (with assistance from this group -- thanks!). The people I was working with have a site that uses lots of php objects. They are having problems with...
1
by: Chris | last post by:
Hi all, I have recently started working with the new XML functionality in PHP5, but I am running into a few problems. Specifically, I am using an Xpath query to try and pull out the data in...
1
by: Philipp Lenssen | last post by:
I'm looking for a way to grab *all* XML of a DOM node (in PHP5). Something like the following, only it doesn't work! Thanks. /* I want everything (all XHTML) in <div id="hell=">...</div> */ ...
3
by: Philipp Lenssen | last post by:
I want to try the new Yahoo API, but I get the PHP5 error message Unknown encoding "utf-8" in the marked line below... (After that, applying the Xpath will fail as symptom of the first bug.) ...
4
by: Skeets | last post by:
When the data structure looks like this: <page> <point id="1"> <premise> 1+1=2 </premise> </point> <point id="2"> <premise> round is better than square </premise> </point> <thought id="1">
5
by: Aziz | last post by:
Hi, I've recently contacted technical service of a web hosting company and asked them wheter or not they're gonna upgrade to PHP5 and MySQL5. Here's a quote from their response which confused me...
2
by: Weston | last post by:
Poking around with XPath using SimpleXML, it looks like there are at least a few reasonably common XPath operators and predicates that aren't supported. I'd like to check my observations against...
1
by: juicymixx | last post by:
I must be completely missing something here. I can't seem to figure out how to parse using the DOM in PHP5... For instance, as a test I'm trying to scrape out the weather conditions table from:...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.