
andrew at morphoss
Jun 7, 2011, 1:56 PM
Post #2 of 3
(758 views)
Permalink
|
On Tue, 2011-06-07 at 09:51 +0200, Jorge L?pez P?rez wrote: > Hi everybody, > > I'm trying to create an XML request that requires the root tag to have > multiple namespace attributes and a prefix specified. To be more > precise, I'm trying to generate a mkcalendar request like this one: > > --8<-- > <?xml version="1.0" encoding="utf-8" ?> > <C:mkcalendar xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav" > xmlns:ical="http://apple.com/ns/ical/"> > <set> > <prop> > <displayname>Test calendar</displayname> > <ical:calendar-color>#000000ff</ical:calendar-color> > </prop> > </set> > </C:mkcalendar> > -->8-- > > By using XMLDocument and XMLElement classes from AWL I can generate > everything all right but the prefix on root tag. I have put my code in > pastebin: http://pastebin.com/UFUNuTi9 . As you can see, the root tag is > lacking the specified prefix. > > It seems that a default namespace (with no prefix) makes the root tag > to lose its own prefix when adding the list of xmlns's (behaviour found > on SetAttribute() @ XMLElement.php). > > Did anyone face the same issue? How did you manage to get the correct > prefix on the root tag? I know I can remove XMLElement relevant lines on > SetAttribute(), but I would like to use AWL as-is. This looks like a bug in the way Render() examines it's arguments for a namespaced root tag. In general these libraries have been used for constructing XML where the root tag is within the default namespace, which is why the issue has not come up before... There are three possible solutions to getting valid XML out, which I can think of. The simplest approach is to call Render() with slightly different parameters, using the abbreviation: ======================================= $ns = array( 'DAV:' => '', 'urn:ietf:params:xml:ns:caldav' => 'C', 'http://apple.com/ns/ical/' => 'ical'); $xml = new XMLDocument($ns); $set = $xml->NewXMLElement('set'); $prop = $set->NewElement('prop'); $xml->NSElement($prop, 'displayname', 'Test calendar'); $xml->NSElement($prop, 'http://apple.com/ns/ical/:calendar-color', '#000000ff'); echo $xml->Render('C:mkcalendar', $set, null, 'urn:ietf:params:xml:ns:caldav'); ======================================= Another approach is to change what is the default tag: ======================================= $ns = array( 'DAV:' => 'D', 'urn:ietf:params:xml:ns:caldav' => '', 'http://apple.com/ns/ical/' => 'ical'); $xml = new XMLDocument($ns); $set = $xml->NewXMLElement('set'); $prop = $set->NewElement('prop'); $xml->NSElement($prop, 'displayname', 'Test calendar'); $xml->NSElement($prop, 'http://apple.com/ns/ical/:calendar-color', '#000000ff'); echo $xml->Render('C:mkcalendar', $set); ======================================= Still a third could be to leave the xmlns tag aliasing entirely to XMLDocument, like: ======================================= $xml = new XMLDocument(); $mkcalendar = $xml->NewXMLElement('mkcalendar', null, null, 'urn:ietf:params:xml:ns:caldav'); $set = $xml->DAVElement($mkcalendar, 'set'); $prop = $xml->DAVElement($set, 'prop'); $xml->DAVElement($prop, 'displayname', 'Test calendar'); $xml->NSElement($prop, 'http://apple.com/ns/ical/:calendar-color', '#000000ff'); echo $xml->Render($mkcalendar); ======================================= And there are other possible variations on that last one as well. I'd be only too happy to see an appropriate patch for the Render() method which properly examined the namespace of the root that was passed in when it was a non-object. Cheers, Andrew. -- ------------------------------------------------------------------------ http://andrew.mcmillan.net.nz/ Porirua, New Zealand Twitter: _karora Phone: +64(272)DEBIAN We should all be glad that Richard Reid wasn't the 'underwear bomber' -- Bruce Schneier ------------------------------------------------------------------------ -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 836 bytes Desc: This is a digitally signed message part URL: <http://lists.davical.org/pipermail/davical-dev/attachments/20110607/d09a4508/attachment.pgp>
|