1

I am having trouble getting the text "TestTwo" where the parent has an attribute with Name=SN from the XML below. I am able to get the SimpleXMLElement, but cannot figure out how to get the child.

What is the best way to return the text "TestTwo"?

Thank you!

$xml = simplexml_load_string($XMLBELOW); $xml->registerXPathNamespace('s','urn:oasis:names:tc:SAML:2.0:assertion'); $result = $xml->xpath("//s:Attribute[@Name='sn']"); var_dump( $result); $XMLBELOW = <<<XML <?xml version="1.0" ?> <saml2:Assertion ID="SAML-4324423" IssueInstant="2012-09-24T17:49:39Z" Version="2.0" xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion"> <saml2:Issuer> Test </saml2:Issuer> <saml2:Subject> <saml2:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified" NameQualifier="Test"> Tester </saml2:NameID> <saml2:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"> <saml2:SubjectConfirmationData NotBefore="2012-09-24T17:48:39Z" NotOnOrAfter="2012-09-24T17:51:39Z"/> </saml2:SubjectConfirmation> </saml2:Subject> <saml2:Conditions NotBefore="2012-09-24T17:48:39Z" NotOnOrAfter="2012-09-24T17:51:39Z"/> <saml2:AuthnStatement AuthnInstant="2012-09-24T17:49:39Z" SessionNotOnOrAfter="2012-09-24T17:51:39Z"> <saml2:SubjectLocality Address="105.57.487.48"/> <saml2:AuthnContext> <saml2:AuthnContextClassRef> urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified </saml2:AuthnContextClassRef> </saml2:AuthnContext> </saml2:AuthnStatement> <saml2:AttributeStatement> <saml2:Attribute Name="sn" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"> <saml2:AttributeValue> TestTwo </saml2:AttributeValue> </saml2:Attribute> <saml2:Attribute Name="departmentNumber" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified"> <saml2:AttributeValue> OPERS </saml2:AttributeValue> </saml2:Attribute> </saml2:AttributeStatement> </saml2:Assertion> XML; 

1 Answer 1

1

How about this?

$result = $xml->xpath("//s:Attribute[@Name='sn']/*"); var_dump( $result[0]->__toString() ); // or just `echo $result[0];` 

The updated expression will get you all children of your target element (you can narrow the result set further, if you'd like).

As xpath() method returns an array, you need to take some of its elements, and just call 'toString' on these objects to get its text. Here I've done with the first one.

Sign up to request clarification or add additional context in comments.

1 Comment

/* is what I was forgetting. Thank you so much for the quick solution!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.