Wednesday, August 4, 2010

Xpath Node to String

Programmer Question

How can I select the string contents of the following nodes:




word
test



word
test2
more words



I have tried a few things



//span/text()


Doesn't get the bold tag



//span/string(.)


is invalid



string(//span)


only selects 1 node



I am using simple_xml in php and the only other option I think is to use //span which returns:



Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => url
)

[b] => test
)

[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[class] => url
)

[b] => test2
)

)


*note that it is also dropping the "more words" text from the second span.



So I guess I could then flatten the item in the array using php some how? Xpath is preferred, but any other ideas would help too.



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails