hi,
i'm having a hellish adventure with PHP5 :xbones: 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
$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
}
}
(2) this is my objective, get the index and use it
volumes and id are on the same level
i really don't care about the id nodes 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 do something?! :glare:Sure, but which position? All [position()] does is filter out the elements that don't have a position - seeing as they all do have positions, they're all still kept.
Thing is, the positions will be 1, 2, ... up to however many there are. Probably not what you're after.
You could look at using XSLT, seeing as it's designed for manipulating the content of XML documents, and XPath was designed for XSLT.Thing is, the positions will be 1, 2, ... up to however many there are. Probably not what you're after.
if the result would be 1 or 2 or ..., that would probably would be the index i'm looking for, right?
but it isn't the value i get. I get the value of element id
but what you are telling me is that, it is impossible to do what i want only with PHP and XPath, correct?
(i want to change the element volumes, of node manga, which has a certain @title)
thanks for the help
edit: what about DOM, SAX, XMLReader? would i be able to accomplish my job with any of those?
(although ultimately i would like to do this job with all of the methods)if the result would be 1 or 2 or ..., that would probably would be the index i'm looking for, right?No, because what position() returns is the position of the element in the list that it receives (the context position (<!-- m --><a class="postlink" href="http://www.w3.org/TR/xpath#dt-context-position">http://www.w3.org/TR/xpath#dt-context-position</a><!-- m -->)). The first element that matches the XPath expression will have position=1, the next will have position=2, and so no, no matter how many other non-matching elements there might be before, after, or between them. Outside that context, the position is meaningless, so having it wouldn't tell you anything useful about where the element is in the document - which you'd need if you wanted to change it. All XPath provides is a means for addressing parts of an XML document, to provide a common syntax for that job to other languages (like XSLT) that are designed for manipulating it. I've already given the link, but here's the obligatory quote from the standard:XPath is a language for addressing parts of an XML document, designed to be used by both XSLT and XPointer.
If you want to avoid XSLT, DOM would probably be easiest (seeing as the other two are XML parsers that just read XML text and convert them to an internal form for later manipulation by something else). Get all the elements with the appropriate tag name, examine each of their title attributes to look for ones that match what you're after, and so on.
As for doing it with "all of the methods" - well, if they were all just as good as each other there'd only be one method, so the question is why there is more than one.
Incidentally, not having seen any of the XML you're traversing, I'm reduced to guessing what it looks like. So I don't know what you're talking about withSorry, i guess i didn't explain myself very well...
my XML file (simplified):....
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 while using SimpleXML?
Because i simply can't do anything with $tmp!!! I can't access it, i don't know it's internal structure.
I simply can't do this: $tmp[0]->object[0] = 12345;
(this doesn't even make sense)
How can i change <numVolumes> value?
Thanks for your time. I appreciate iti was able to get an answer from someone else, for archive purposes, here it is:
$tmp = $mangaObj->xpath('//*[local-name() = "manga" and @title="'.$title.'"]');
$tmp[0]->numVolumes = $new_volume;
i'm having a hellish adventure with PHP5 :xbones: 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
$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
}
}
(2) this is my objective, get the index and use it
volumes and id are on the same level
i really don't care about the id nodes 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 do something?! :glare:Sure, but which position? All [position()] does is filter out the elements that don't have a position - seeing as they all do have positions, they're all still kept.
Thing is, the positions will be 1, 2, ... up to however many there are. Probably not what you're after.
You could look at using XSLT, seeing as it's designed for manipulating the content of XML documents, and XPath was designed for XSLT.Thing is, the positions will be 1, 2, ... up to however many there are. Probably not what you're after.
if the result would be 1 or 2 or ..., that would probably would be the index i'm looking for, right?
but it isn't the value i get. I get the value of element id
but what you are telling me is that, it is impossible to do what i want only with PHP and XPath, correct?
(i want to change the element volumes, of node manga, which has a certain @title)
thanks for the help
edit: what about DOM, SAX, XMLReader? would i be able to accomplish my job with any of those?
(although ultimately i would like to do this job with all of the methods)if the result would be 1 or 2 or ..., that would probably would be the index i'm looking for, right?No, because what position() returns is the position of the element in the list that it receives (the context position (<!-- m --><a class="postlink" href="http://www.w3.org/TR/xpath#dt-context-position">http://www.w3.org/TR/xpath#dt-context-position</a><!-- m -->)). The first element that matches the XPath expression will have position=1, the next will have position=2, and so no, no matter how many other non-matching elements there might be before, after, or between them. Outside that context, the position is meaningless, so having it wouldn't tell you anything useful about where the element is in the document - which you'd need if you wanted to change it. All XPath provides is a means for addressing parts of an XML document, to provide a common syntax for that job to other languages (like XSLT) that are designed for manipulating it. I've already given the link, but here's the obligatory quote from the standard:XPath is a language for addressing parts of an XML document, designed to be used by both XSLT and XPointer.
If you want to avoid XSLT, DOM would probably be easiest (seeing as the other two are XML parsers that just read XML text and convert them to an internal form for later manipulation by something else). Get all the elements with the appropriate tag name, examine each of their title attributes to look for ones that match what you're after, and so on.
As for doing it with "all of the methods" - well, if they were all just as good as each other there'd only be one method, so the question is why there is more than one.
Incidentally, not having seen any of the XML you're traversing, I'm reduced to guessing what it looks like. So I don't know what you're talking about withSorry, i guess i didn't explain myself very well...
my XML file (simplified):....
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 while using SimpleXML?
Because i simply can't do anything with $tmp!!! I can't access it, i don't know it's internal structure.
I simply can't do this: $tmp[0]->object[0] = 12345;
(this doesn't even make sense)
How can i change <numVolumes> value?
Thanks for your time. I appreciate iti was able to get an answer from someone else, for archive purposes, here it is:
$tmp = $mangaObj->xpath('//*[local-name() = "manga" and @title="'.$title.'"]');
$tmp[0]->numVolumes = $new_volume;