php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42325 xmlns parsing issues
Submitted: 2007-08-17 04:08 UTC Modified: 2007-08-18 09:48 UTC
From: cpriest at warpmail dot net Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.4RC2 OS: Windows
Private report: No CVE-ID: None
 [2007-08-17 04:08 UTC] cpriest at warpmail dot net
Description:
------------
SimpleXML is having some difficulty in parsing XML which contains a default xmlns entry.

Reproduce code:
---------------
<?
	header('Content-Type: text/plain');
	
	$ResponseBody = '<?xml version="1.0" encoding="UTF-8"?>
<result xmlns="https://zzz.com/webservices" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">Test</result>';
	
	$objXML = new SimpleXMLElement($ResponseBody);
	
	$tMatches = $objXML->xpath('/result');
	print_r((string)$tMatches[0]);
?>

Expected result:
----------------
Expect print_r() to print "Test"

Actual result:
--------------
$tMatches is an empty array()

If you change the xmlns="https://zzz.com/webservices" to xmlns:zzz="https://zzz.com/webservices" (add a namespace sub-string), it works fine.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-17 07:01 UTC] chregu@php.net
You have to register default namespaces with

http://ch2.php.net/manual/en/function.simplexml-element-
registerXPathNamespace.php



 [2007-08-17 14:35 UTC] cpriest at warpmail dot net
I'm not sure I understand why I would have to call registerXPathNamespace() to use a default namespace?  I thought one of the reasons to use SimpleXML was to parse XML documents.

If I make the one default namespace (xmlns="http://zzz.com/webservices") a non-default such as xmlns:test=, then it properly queries the /result value.

To do what you're suggestion I would have to parse the XML document myself looking for the namespace I would want to query against.

The code below also works if you simply remove the xmlns="" attribute.  Are you sure that I'm supposed to be using registerXPathNamespace()
 [2007-08-17 14:45 UTC] rrichards@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2007-08-17 16:30 UTC] cpriest at warpmail dot net
Why is this bogus?  Does anyone actually even read these reports?
 [2007-08-17 19:52 UTC] rrichards@php.net
This is not a help desk. Thats how XPath and default namespaces work.
 [2007-08-18 04:42 UTC] cpriest at warpmail dot net
Okay, this is a little frustrating here, I don't have infinite amount of time to get a bug report through to you.

I am trying to use the DEFAULT NAMESPACE.  I couldn't use registerXPathNamespace() if I wanted to, there is no prefix or namespace to declare to use it with.

PLEASE READ THE REPORT.
 [2007-08-18 09:48 UTC] rrichards@php.net
DO NOT RE-OPEN. This is not a bug.
A simple online search would give you more than enough information on this (since you are ignoring Chregu's advice). This has nothing to do with SimpleXML or PHP. It is simply how you need to work with XPath and default namespaces (yes I said DEFAULT NAMESPACES) within any language.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC