0

Could someone explain to me why $xml2 fails to parse, but $xml3 works? Am I not escaping something within the xml string?

$xml2 = "<multistatus xmlns=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">Test</multistatus>";

$xml3 = "<multistatus>Test</multistatus>";

$root = new SimpleXMLElement($xml2);
//echo $xml;
foreach ($root->xpath('//multistatus') as $node) {
    echo $node . "<BR>";
}

Ok, this is the latest copy with the requested modifications and it still does not appear to work:

$xml2 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><multistatus xmlns=\"DAV:\" xmlns:C=\"urn:ietf:params:xml:ns:caldav\">Test</multistatus>";

$xml3 = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><multistatus>Test</multistatus>";

$root = new SimpleXMLElement($xml2);
$root->registerXPathNamespace("n", "urn:ietf:params:xml:ns:caldav");
//echo $xml;
foreach ($root->xpath('//n:multistatus') as $node) {
    echo $node . "<BR>";
}
user978122
  • 5,531
  • 7
  • 33
  • 41

2 Answers2

0

Your biggest problem probably comes from this not being a well formed XML string. Both simplxmlelement and simplexml_load_string both require well formed XML strings. Try adding normal XML headers to see if that helps.

$xml = <<<XML
<?xml version="1.0">
<root>
<multistatus xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">Test</multistatus>
</root>
XML;
mseancole
  • 1,662
  • 4
  • 16
  • 26
  • $xml2 = "Test"; $xml3 = "Test"; //$xml->registerXPathNamespace("n", "http://iptc.org/std/NewsML/2003-10-10/"); $root = new SimpleXMLElement($xml2); //echo $xml; foreach ($root->xpath('//multistatus') as $node) { echo $node . "
    "; }
    – user978122 Mar 19 '12 at 22:50
  • Should combine our two answers mathieu's and mine. Should do the trick. And you need some sort of base container for those multistatus, unless you only plan on having the one node under it. – mseancole Mar 20 '12 at 00:41
  • Indeed. I am simply trying to get the basics working. I've combined both, and it still does not appear to want to work. – user978122 Mar 20 '12 at 04:08
  • I would say its because you are declaring your namespaces wrong. You need to use "C" (note its caps too) instead of "n". I'm not sure where you got the "n" from. Also, the default namespace needs to be registered, just assign a random letter, you can use "n" here if you wish. – mseancole Mar 20 '12 at 13:40
  • I hate to be a bother, but I'd be very gracious if you could solve this problem for me: $xml2 = "Test"; $xml3 = "Test"; $root = new SimpleXMLElement($xml2); $root->registerXPathNamespace("C", "urn:ietf:params:xml:ns:caldav"); //echo $xml; echo 'Test'; foreach ($root->xpath('//C:multistatus') as $node) { echo 'Test'; echo $node . "
    "; }
    – user978122 Mar 20 '12 at 23:35
  • Hmm. Print_r() shows the data, and SimpleXMLElement seems to eat the outermost tag. Interesting... – user978122 Mar 21 '12 at 00:48
  • print_r shows the data because $xml is an array. If you want to iterate over the array, do something like `foreach($root as $node) { echo $node; }` after you have registered the namespaces. I'm not sure what you meant by "eat" it should just populate it into an array. Just noticed that you are printing $xml and not $root. I'm really confused now. – mseancole Mar 21 '12 at 02:13
0

You need to declare your namespace using registerXPathNamespace()

This question should point in the right direction: PHP simplexml: why does xpath stop working?

Community
  • 1
  • 1
Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • $xml2 = "Test"; $xml3 = "Test"; $root = new SimpleXMLElement($xml2); $root->registerXPathNamespace("n", "urn:ietf:params:xml:ns:caldav"); //echo $xml; foreach ($root->xpath('//n:multistatus') as $node) { echo $node . "
    "; }
    – user978122 Mar 19 '12 at 22:57
  • Assuming I am declaring the namespace correctly, it does not appear to have any effect. – user978122 Mar 19 '12 at 22:58