473,326 Members | 2,081 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,326 software developers and data experts.

simplexml with xpath, getting index of results

hi,
i'm having a hellish adventure with PHP5 i hope someone just can help
me out on this... i'm desperate

All i want to do, is to get the index of the returned elements, so i
can acess them and change them
(i'm guessing this is the only way to do this, as i can't change the
element on-the-fly in XPath )
$mangaLib = simplexml_load_file($mangaLibFile);
$mangaObj = $mangaLib->children($mangaURI);
$tmp = $mangaObj->xpath('//id[../@title="'.$title.'"][position()]');
// (1)

..... do magic here, to get $ind...

$mangaObj->manga[$ind]->volumes = 666; // (2)

(1) var_dump($tmp) returns:
array(1) {
[0]=>
object(SimpleXMLElement)#18 (1) {
[0]=>
string(3) "100" // value of id (which i don't care)
}
}

(2) this is my objective, get the $index and use it

elements "volumes" and "id" are on the same level

i really don't care about the "id" node value, what i want is its
position, but PHP complained if i used it without id:
$tmp = $mangaObj->xpath('//[../@title="'.$title.'"][position()]');

isn't position() supposed to return the index i want?!
OR.... is this impossible to accomplish PHP and XPath....
if so, can i use any of DOM, SAX, XMLReader to accomplish my task?
(although i would like to do this with all methods)
thansk for any help

Jan 2 '06 #1
4 4345
su********@gmail.com wrote:
i really don't care about the "id" node value, what i want is its
position, but PHP complained if i used it without id:
$tmp = $mangaObj->xpath('//[../@title="'.$title.'"][position()]');

isn't position() supposed to return the index i want?!


PHP's complaining about the invalid use of the position() function. Any of
the following would be valid:

//*[../@title="'.$title.'"][position() = 1]
//*[position() = 1]
//*[../@title="'.$title.'"][1]
//*[1]
JW
Jan 2 '06 #2
That doesn't solve my problem.
I guess i didn't explain myself very well...

------------------------ MY XML FILE:
.....
xmlns:lib="http://en.wikipedia.org/wiki/Manga"
.....
<lib:manga title="Fullmetal">
<numVolumes>14</numVolumes>
</lib:manga>
.....
-------------------------
by doing this:
++++++++++++++++++++++++++++++
$mangaLib = simplexml_load_file($mangaLibFile);
$mangaObj = $mangaLib->children($mangaURI);
$tmp = $mangaObj->xpath('//numVolumes[../@title="'.$title.'"]');
var_dump($tmp);
++++++++++++++++++++++++++++++

i get:
---++++++++++---
array(1) {
[0]=>
object(SimpleXMLElement)#18 (1) {
[0]=>
string(2) "14" // <numVolumes>14</numVolumes>
}
}
---++++++++++---


What i want to do is, change <numVolumes> from 14, to some other value!
So i would do this:

$mangaObj->manga[$INDEX]->numVolumes = 12345;

But to accomplish that, i need to know the value of $INDEX, am i
correct or is there other way?
Because i simply can't do anything with $tmp!!! I can't access it and
change it's value! (and i'm assuming it's a reference)

I can't do this: $tmp[0]->object[0] = 12345;
(this doesn't even make sense)

How can i change <numVolumes> value?

thanks in advance

Jan 2 '06 #3
su********@gmail.com wrote:
How can i change <numVolumes> value?


By selecting the parent node instead:

$tmp = $mangaObj->xpath('//*[local-name() = "manga" and
@title="'.$title.'"]');
$tmp[0]->numVolumes = $new_volume;
JW
Jan 2 '06 #4
ooooh, thank you very much! thank you!

it finally worked... i was really getting crazy with this thing.

i've coded from assembly through prolog... but this php+xml+xpath is
really evil
I've read some chapters from XML in a Nutshell, but it seems i didn't
get it quite right
It still fells non-obvious to me.

once again thanks!

Jan 2 '06 #5

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

Similar topics

1
by: hwcowan | last post by:
Hello, I have started using SimpleXML and can do most things, but there are a couple of things that I can't seem to figure out. Currently, I can: 1. Open & load an XML file 2. Manually...
2
by: mandric | last post by:
Hello, Can someone please enlighten me on how to preserve the <!]> element when parsing an xml file or string with simplexml. I'm using libxml 2.6.16 and php 5.1.4. I tried a few variations,...
2
by: mouseit101 | last post by:
Hi, I'm writing a script that would (hopefully) search google images for whatever, and then return a list of URLs for the image. Right now I have: $dom = new DomDocument(); $url =...
0
by: thecoolone | last post by:
I am creating a REST API in php that parses the XML result given by IBM Omnifind. here's the php code: $query=$_GET;...
0
by: pompair | last post by:
Hello, I'm making a quiz game for fun. I have an xml file like this: <?xml version="1.0" encoding="utf-8" ?> <results> <index>99</index> <answers>11</answers> <questions> <question id="1">
1
by: WebCM | last post by:
There are lots of problems with XHTML parsing in PHP by XML functions. I've just solved most of them. However, how to get value of attribute with namespace? <input type="checkbox" id="something"...
7
by: dimo414 | last post by:
So I'm trying to use SimpleXML to get some attribute information about some nodes in my XML document, but it seems like SimpleXML ignores attributes for elements with no children, For instance:...
1
by: Phillip B Oldham | last post by:
I'm sure I'll soon figure out how to find these things out for myself, but I'd like to get the community's advice on something. I'm going to throw together a quick project over the weekend: a...
6
by: Phantom | last post by:
I totally need the help of the XML experts up here, I can't figure this one out, I've been working on it for hours today, googling my brains out. I need to write an xpath query string that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.