You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by rchauhan <rc...@micron.com> on 2003/04/19 00:48:34 UTC
DOM Namespace Question
I have a beginners question on using Xerces DOM API to build an XML document that contains namespaces. Checked the archives and sample and could not find the right answer...
If I want to create a XML document shown below:
<myNS:Root xmlns:myNS="http://www.myNamespace.com">
<myNS:Child1"/>
<myNS:Child2/>
</myNS:Root>
Is this the right way to implement with Xerces?
DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(X("Core"));
DOMDocument* doc = impl->createDocument(
0, // root element namespace URI.
0, // root element name
0); // document type object (DTD).
DOMElement* rootElem = doc->createElementNS(X("http://www.myNamespace.com"), X("myNS:Root"));
doc->appendChild(rootElem);
DOMElement *firstChild = doc->createElementNS(X("http://www.myNamespace.com"), X("myNS:Child1"));
rootElem->appendChild(firstChild);
.................
The questions I have are:
1. Do I need to provide the Namespace URI for for the Child1 node also? I assumed the information would be redundant and I don't have to provide it again (can get by with providing an empty namespaceURI).
2. Has anyone modified DOMPrint to print out the Namespace URI also (output similar to the xml doc shown above)?
3. What does the setPrefix() method in DOMNode class do? When would I use it?
4. If I want to copy a namespace'd node from one document to another, are there any special considerations I need to look into.
Any help on this questions would be appreciated,
Thanks,
Ravin
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: DOM Namespace Question
Posted by Gareth Reakes <ga...@decisionsoft.com>.
Hi,
namespace attributes affect the uri of elements /attributes at
parse time only.
> The questions I have are:
>
> 1. Do I need to provide the Namespace URI for for the Child1 node also? I assumed the information would be redundant and I don't have to provide it again (can get by with providing an empty namespaceURI).
Yes, if you set an elements uri it does not cascade.
> 2. Has anyone modified DOMPrint to print out the Namespace URI also (output similar to the xml doc shown above)?
It does print out the namespace information. The problem is that it does
not create the appropriate namespace attributes to make the document
namespace well formed. The normalizeDocument method does this but is not
implemented as yet. I have the code that does this but I need to implement
entity replacement and text node consolidation. It may make it into the
next release.
> 3. What does the setPrefix() method in DOMNode class do? When would I use it?
If it is an element or attribute then it will set the prefix part of the
QName. ie the bit before the :. This can be used so that the document you
print out has the desirable prefixes and is namespace well formed.
> 4. If I want to copy a namespace'd node from one document to another, are there any special considerations I need to look into.
The notion of a namespace node from the infoset does not exist in standard
DOM (it sort of does in DOM 3 XPath). If you mean the namespace attribute
then copying it from one document to another will require a standard
importNode from the document. Bear in mind this will have no effect on the
uri of any nodes that you place this attribute on.
Gareth
--
Gareth Reakes, Head of Product Development +44-1865-203192
DecisionSoft Limited http://www.decisionsoft.com
XML Development and Services
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org