Nice work. It makes sense now that you've explained it. Pity we don't have a knowledge base to add this tidbit to. (I don't think it qualifies as a frequently asked question, since I don't think it has come up before.)


From: T A [mailto:[EMAIL PROTECTED]
Sent: Monday, June 21, 2004 1:59 PM
To: [EMAIL PROTECTED]
Subject: RE: Creating multiple namespaces

After more investigation, I finally got it using only DOM2 methods. Here is the trick, and it is kind of subtle, at least to me anyway, when using
 
virtual DOMAttr *  createAttributeNS (const XMLCh *namespaceURI, const XMLCh *qualifiedName)
 
or
 
virtual void  setAttributeNS (const XMLCh *namespaceURI, const XMLCh *qualifiedName, const XMLCh *value)
 
to add namespaces to an element node, the value of namespaceURI argument must be http://www.w3.org/2000/xmlns/  if the qualifiedName is "xmlns:whatever" oherwise it will not work. Then, the value argument can be anything you want, like "urn:ISBN:0-395-36341-6". The thing I was doing wrong was that I was setting the namespaceURI argument to the same thing as the value argument and that is not allowed when the prefix is xmlns. I pieced it together from the line in Exception portion of the definition of setAttributeNS or createAttributeNS, which reads:
 
if the qualifiedName has a prefix and the namespaceURI is null, if the qualifiedName has a prefix that is "xml" and the namespaceURI is different from " http://www.w3.org/XML/1998/namespace", or if the qualifiedName, or its prefix, is "xmlns" and the namespaceURI is different from "http://www.w3.org/2000/xmlns/".
 
 
When creating an element for either namespace, use
 
virtual DOMElement *  createElementNS (const XMLCh *namespaceURI, const XMLCh *qualifiedName)
 
and the namespaceURI argument's value should be the same value as the namespace attribute's value, "urn:ISBN:0-395-36341-6" or whatever, not "http://www.w3.org/2000/xmlns/".
 
Thanks to both of you for your help and suggestions.
 
 
 

Jesse Pelton <[EMAIL PROTECTED]> wrote:
Note that Adam's approach works by avoiding Xerces' namespace processing. He appears to be mixing DOM 1 non-namespace aware calls with DOM 2 namespace-aware calls, which is documented as potentially dangerous. You may get away with it if you know enough about Xerces internals (and they don't change).
 
You may have found a real limitation in Xerces and possibly in the DOM. This seems pretty basic, though. You might try using DOMPrint (with the -n flag to enable namespace processing) to parse a document with multiple namespaces declared in the root element. Make sure it reserializes it correctly, then run it under a debugger to see how the namespace declarations are handled.


From: T A [mailto:[EMAIL PROTECTED]
Sent: Friday, June 18, 2004 6:25 PM
To: [EMAIL PROTECTED]
Subject: RE: Creating multiple namespaces

Thanks for replying. Yes, your XML looks correct. Shoot!
 
I use createElementNS to create the element and then appendChild.
 
I use either setAttributeNS or setAttributeNodeNS to create the namespace attribute.
 
I am also wondering if the issue is that if the namespace attribute URI is has to be something like http://www.w3.org/1999 if the name is xlmns:fo or whatever

Adam Heinz <[EMAIL PROTECTED]> wrote:
My XML that goes back and forth between namespaces pretty regularly:
 
<dlg:page xmlns:dlg="http://www.exstream.com/2003/XSL/Dialogue" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:declarations>
  <dlg:paper-type size="612.00pt 792.00pt"/>
  <dlg:objects>
    <dlg:text>
      <dlg:rect bottom="727.20pt" left="216.00pt" right="540.00pt" top="684.00pt"/>
      <fo:flow>
        <fo:block>
          <fo:inline font-family="Arial" font-size="36.00pt" font-weight="bold">Saturday April 22</fo:inline>
        </fo:block>
      </fo:flow>
    </dlg:text>
  </dlg:objects>
</dlg:page>
What function are you using to append elements?  I consistently use something like:
 
DOMElement* pxInline = m_pDoc->createElement(_X("fo:inline"));
pxParent->appendChild(pxInline);
Adam Heinz
Senior Software Developer
Exstream Software
-----Original Message-----
From: T A [mailto:[EMAIL PROTECTED]
Sent: Friday, June 18, 2004 3:03 PM
To: [EMAIL PROTECTED]
Subject: RE: Creating multiple namespaces

Thanks for the reply, but unfortunately, this does not work correctly.
 
It will indeed create a root element like so
 
<?xml version="1.0"?>
<bk:book xmlns:bk='urn:loc.gov:books'
         xmlns:isbn='urn:ISBN:0-395-36341-6'>
However, when I try to create an element using the second namespace, isbn, it looks like this
 
 
<?xml version="1.0"?>
<bk:book xmlns:bk='urn:loc.gov:books'
         xmlns:isbn='urn:ISBN:0-395-36341-6'>
    <bk:title>Cheaper by the Dozen</bk:title>
    <isbn:number xmlns:isbn='urn:ISBN:0-395-36341-6'>1568491379</isbn:number>
</bk:book>
It is defining the isbn namespace again at the element that uses it. To me, this means that the attribute in the root element is not being picked up as a namespace, only an attribute with the name "xmlns:isbn"
 
This is what I am trying to get
 
<?xml version="1.0"?>
<bk:book xmlns:bk='urn:loc.gov:books'
         xmlns:isbn='urn:ISBN:0-395-36341-6'>
    <bk:title>Cheaper by the Dozen</bk:title>
    <isbn:number>1568491379</isbn:number>
</bk:book>
 
Any opinions? I have been doing more investigation, and I starting to think that this kind of functionality is just not supported by Xerces C++. In JDOM, there is a method called DeclareNamespace or something to that effect, that seem to be what is needed.
 
Ted


Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!


Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.

Reply via email to