问题
I recently asked a question about how to select a parent's node attribute and its values at the same time using xpath, What i ended up with is :
Parent/@attr|Parent/x
It grab the parent attr
's value and all of its x
's nodes..
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
)
The thing now.. In case there are several Parent
nodes, it will mix them together and i wouldn't know which attr
belongs to its x
's .. it will result something like this :
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[attr] => attrValue
)
)
[1] => SimpleXMLElement Object
(
[0] => 1
)
[2] => SimpleXMLElement Object
(
[0] => 2
)
[3] => SimpleXMLElement Object
(
[0] => 3
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[attr] => anotherAttrValue
)
)
[5] => SimpleXMLElement Object
(
[0] => 1
)
[6] => SimpleXMLElement Object
(
[0] => 2
)
...
)
You see, they are all treated as a normal result, what i am trying to do is to make the attr
contain all of it's results ( 1, 2, 3 ) in an array or something and the same thing goes to the other attr
's to end up with something like this :
Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[attr] => attrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[attr] => anotherAttrValue
)
)
Array
(
[0] => SimpleXMLElement Object
(
[0] => 1
)
[1] => SimpleXMLElement Object
(
[0] => 2
)
[2] => SimpleXMLElement Object
(
[0] => 3
)
)
)
using the xpath and the way given at first
来源:https://stackoverflow.com/questions/13887094/xml-xpath-attribute-value-result-to-contain-other-results