Advertisement

05.16.2008 at 06:38PM PDT, ID: 23410099
[x]
Attachment Details

XPath Query in PHP: Get Value of Element in Document with Namespace

Asked by dbnewbie in PHP Scripting Language, Extensible Markup Language (XML)

Tags: XML, XPath, PHP

How do I get the text node associated with the element ReturnObject? In the attached snippet, I need the following string: 8b020dc7-7e3d-46d7-9f95-9f11d06dc475

All the examples I've seen seem to query simple XML documents. None show how to query something with a namespace. SimpleXMLElement seems to allow me to register namespaces, but I am not getting the results I think I should be getting. Can anyone help?

 I do not want a RegEx solution. I need to execute this query via XPath.
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
$MyXmlDocument = new SimpleXMLElement("<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Header><ResponseHeader xmlns='https://secure.paymex.co.nz/payment/'><IsError>false</IsError><Message>Payment Success</Message><HasReturnObject>true</HasReturnObject><ReturnObjectDescription>Customer id</ReturnObjectDescription><ReturnObject xmlns:q1='http://microsoft.com/wsdl/types/' xsi:type='q1:guid'>8b020dc7-7e3d-46d7-9f95-9f11d06dc475</ReturnObject></ResponseHeader></soap:Header><soap:Body><RegisterSubscriptionResponse xmlns='https://secure.paymex.co.nz/payment/'><RegisterSubscriptionResult>0</RegisterSubscriptionResult></RegisterSubscriptionResponse></soap:Body></soap:Envelope>");
	$MyXmlDocument->registerXPathNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
	$MyXmlDocument->registerXPathNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
	$MyXmlDocument->registerXPathNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
	$MyXmlDocument->registerXPathNamespace("q1", "http://microsoft.com/wsdl/types/");
	
	$MyXpath = "//ReturnObject";
	$results = $MyXmlDocument->xpath($MyXpath);
[+][-]05.16.2008 at 06:39PM PDT, ID: 21587670

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.16.2008 at 10:19PM PDT, ID: 21588071

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.17.2008 at 12:07AM PDT, ID: 21588247

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP Scripting Language, Extensible Markup Language (XML)
Tags: XML, XPath, PHP
Sign Up Now!
Solution Provided By: Gertone
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628