# annoying DOM limitation

- Thread: https://externals.io/message/16241
- Original archive: https://news-web.php.net/php.internals/16241
- Started by George Schlossnagle on 2005-05-13T19:49:15+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #16241 — George Schlossnagle — 2005-05-13T19:49:15+00:00

URL: https://externals.io/message/16241#16241

While hacking on a PHPDoc -> WSDL generator, I ran into an annoying  
limitation in the dom extension: you can't add a namespace to a dom  
document unless you have an element in that namespace.  Why might you  
want to do this?  Well, a common thing is to have your xml-schema  
types to be specified as attribute values, i.e.

<element name="foo" type="xsd:string"/>

for this to work I need xsd to be an alias for http://www.w3.org/2001/ 
XMLSchema.

To work around this, I added a DomElement::addNS($uri, $alias) method  
to Dom.  Adding the namespace 'manually' as an attribute on an  
element does not work.

I know this isn't part of the Dom spec, but it's incredibly useful.   
Does anyone (Rob, Chegru) mind me adding it to HEAD?

George

George Schlossnagle

-- Vice President of Engineering
-- OmniTI Computer Consulting
-- http://www.omniti.com

---

## #16242 — Rob Richards — 2005-05-13T21:18:38+00:00

URL: https://externals.io/message/16241#16242 · In reply to: #16241

I had just posted a comment on your blog about this.

$root->setAttributeNS('http://www.w3.org/2000/xmlns/', 
'xmlns:xsd','http://www.w3.org/2001/XMLSchema');
should do exactly what you want.

Rob

George Schlossnagle wrote:

> While hacking on a PHPDoc -> WSDL generator, I ran into an annoying  
> limitation in the dom extension: you can't add a namespace to a dom  
> document unless you have an element in that namespace.  Why might you  
> want to do this?  Well, a common thing is to have your xml-schema  
> types to be specified as attribute values, i.e.
>
> <element name="foo" type="xsd:string"/>
>
> for this to work I need xsd to be an alias for http://www.w3.org/2001/ 
> XMLSchema.
>
> To work around this, I added a DomElement::addNS($uri, $alias) method  
> to Dom.  Adding the namespace 'manually' as an attribute on an  
> element does not work.
>
> I know this isn't part of the Dom spec, but it's incredibly useful.   
> Does anyone (Rob, Chegru) mind me adding it to HEAD?

---

## #16243 — Jared Williams — 2005-05-13T21:29:59+00:00

URL: https://externals.io/message/16241#16243 · In reply to: #16241

> 
> While hacking on a PHPDoc -> WSDL generator, I ran into an 
> annoying limitation in the dom extension: you can't add a 
> namespace to a dom document unless you have an element in 
> that namespace.  Why might you want to do this?  Well, a 
> common thing is to have your xml-schema types to be specified 
> as attribute values, i.e.
> 
> <element name="foo" type="xsd:string"/>
> 
> for this to work I need xsd to be an alias for 
> http://www.w3.org/2001/ XMLSchema.
> 
> To work around this, I added a DomElement::addNS($uri, 
> $alias) method to Dom.  Adding the namespace 'manually' as an 
> attribute on an element does not work.
> 
> I know this isn't part of the Dom spec, but it's incredibly useful.   
> Does anyone (Rob, Chegru) mind me adding it to HEAD?


Hmm, I have been doing this via setAttributeNS(), with the XML namespace namepace


define('NS_NS', 'http://www.w3.org/2000/xmlns/');
define('NS_XLINK', 'http://www.w3.org/1999/xlink');

$document = new DOMDocument();
$root = $document->createElement('root');
$document->appendChild($root);
$root->setAttributeNS(NS_NS, 'xmlns:xlink', NS_XLINK);
echo $document->saveXML();

Haven't encountered any problems with this method, seems to produce XML (SVG in this case) fine.

Jared
