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 Nick Bastin <nb...@opnet.com> on 2004/04/21 17:47:38 UTC
Writing documents with schemas and namespaces...
When writing out documents which have schemas and namespaces, am I
supposed to set up the createDocument() and createElement() calls
differently, or just add the attributes myself?
For example, what code would I need to create:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ndr:Template xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ndr="http://www.opnet.com/ns/NetDoctor"
xsi:schemaLocation="http://www.opnet.com/ns/NetDoctor
/schemas/Report_Template?v11">
<...tags...>
</ndr:Template>
I can read this file successfully, but I'm not having a lot of luck
writing it. Now, I'm assuming that I'm not supposed to write those
attributes in directly, so maybe that's my problem. Basically, the
question leads to - is there a lot of builtin support for writing
documents with schemas and namespaces, or do you just call
createElement with tags that already have their namespace qualifier in
the name (like "ndr:Template"), or do you call it with "Template" and
you've already told it a namespace?
Thanks,
Nick
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: Writing documents with schemas and namespaces...
Posted by Gareth Reakes <ga...@parthenoncomputing.com>.
Hi Nick,
> Ok, but if I want an explicit one, I don't see any way to do that other
> than setting my elements with their prefix already set.
You are correct. If you want an explicit prefix in the output then you
have to set the prefix of the element. You do not then have to create the
namespace attribute, namespace normalization will do this. Note that
setting a prefix on a attribute does not guarantee the prefix will be in
the output.
> I understand why you can't give a prefix if there's no namespaceURI -
> that makes perfect sense. I was just extending that conclusion to mean
> that a prefix was expected if a namespaceURI was specified. Apparently
> that is not the case, but I'm not sure I completely understand that,
> because I don't really want DOM choosing my namespace qualifier in a
> random way. Is it not expected that the user will want to explicitly
> set their namespace qualifier, rather than having it chosen for them?
The pain of namespaces ;) It depends on whether the documents are to be
human readable. If not then it does not matter at all as the documents
will be logically equivalent.
Gareth
--
Gareth Reakes, Managing Director Parthenon Computing
+44-1865-811184 http://www.parthcomp.com
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: Writing documents with schemas and namespaces...
Posted by Nick Bastin <nb...@opnet.com>.
On Apr 22, 2004, at 11:23 AM, Gareth Reakes wrote:
> Hi,
>
>> How does it possibly know what prefix to use?
>
> There is an algorithm in the DOM specs it uses. It looks to see if
> there
> is an appropriate one else makes one up (simplification). Take a look
> if
> you want more info.
Ok, but if I want an explicit one, I don't see any way to do that other
than setting my elements with their prefix already set.
>> Besides, the
>> documentation seems to indicate that a prefix should be supplied as
>> part of the qualifiedName (since, technically, that would include the
>> namespace, that seems logical to me). The documentation says: The
>> 'NAMESPACE_ERR' exception is thrown "...if the qualifiedName has a
>> prefix and the namespaceURI is null", which seems to indicate that
>> prefixes are expected in the qualifiedName field.
>
> The important part is "if the qualifiedName has a prefix and the
> namespaceURI is null". In the XML world you cannot bind a prefix to the
> null namespace so you cannot give a prefix to an element/attr if it
> has no
> namespace. You can give a namespace to an ele/attr and not give it a
> prefix. Try out a few examples and serialize them if this is not clear.
I understand why you can't give a prefix if there's no namespaceURI -
that makes perfect sense. I was just extending that conclusion to mean
that a prefix was expected if a namespaceURI was specified. Apparently
that is not the case, but I'm not sure I completely understand that,
because I don't really want DOM choosing my namespace qualifier in a
random way. Is it not expected that the user will want to explicitly
set their namespace qualifier, rather than having it chosen for them?
--
Nick
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: Writing documents with schemas and namespaces...
Posted by Gareth Reakes <ga...@parthenoncomputing.com>.
Hi,
> How does it possibly know what prefix to use?
There is an algorithm in the DOM specs it uses. It looks to see if there
is an appropriate one else makes one up (simplification). Take a look if
you want more info.
> Besides, the
> documentation seems to indicate that a prefix should be supplied as
> part of the qualifiedName (since, technically, that would include the
> namespace, that seems logical to me). The documentation says: The
> 'NAMESPACE_ERR' exception is thrown "...if the qualifiedName has a
> prefix and the namespaceURI is null", which seems to indicate that
> prefixes are expected in the qualifiedName field.
The important part is "if the qualifiedName has a prefix and the
namespaceURI is null". In the XML world you cannot bind a prefix to the
null namespace so you cannot give a prefix to an element/attr if it has no
namespace. You can give a namespace to an ele/attr and not give it a
prefix. Try out a few examples and serialize them if this is not clear.
The main thing to understand is that the namespace attributes have no
effect on the actual namespace of ele/attr after parse. The only time you
have to worry about them is when you serialize. If you do not "namespace
normalize" at this stage then you may well end up with an XML document
that does not represent what you had in memory.
Gareth
--
Gareth Reakes, Managing Director Parthenon Computing
+44-1865-811184 http://www.parthcomp.com
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: Writing documents with schemas and namespaces...
Posted by Nick Bastin <nb...@opnet.com>.
On Apr 22, 2004, at 5:50 AM, Gareth Reakes wrote:
> Namespace attributes such as xmlns:foo only have an effect at parse
> time.
> You can manipulate them as much as you want after this an it will have
> no
> effect what so ever on the namespace of any elements or attributes. The
> only difference will occur during serialization. Then, if an element is
> found that is in a namespace, an algorithm is used to see what prefix
> should be used and if a namespace attr should be created.
How does it possibly know what prefix to use? Besides, the
documentation seems to indicate that a prefix should be supplied as
part of the qualifiedName (since, technically, that would include the
namespace, that seems logical to me). The documentation says: The
'NAMESPACE_ERR' exception is thrown "...if the qualifiedName has a
prefix and the namespaceURI is null", which seems to indicate that
prefixes are expected in the qualifiedName field.
--
Nick
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: Writing documents with schemas and namespaces...
Posted by Gareth Reakes <ga...@parthenoncomputing.com>.
Hi,
> So I noticed this, but I don't *quite* understand it. You still have
> to put a fully-qualified name in createElementNS, so what's the point
> of calling it all the time? Obviously you need to call it once, so
> that you get the xmlns:ndr="foo" attribute on the correct node, but
> every child of that node should probably, for efficiency, be called
> using createElement(). Or am I missing something? I.e., if I create
> my document node with the right namespace (presuming it is used in the
> entire document), is there any reason to call createElementNS for the
> child nodes? It seems that it would save both memory and time to just
> call createElement(), but I may not have a full understanding of the
> side-effects.
As a general rule, you should not use the level 1 API any more unless you
really know what you are doing. There are some DOM level 3 methods (unless
the recs changed it - I have not read them yet) that can't cope with level
1 nodes.
Namespace attributes such as xmlns:foo only have an effect at parse time.
You can manipulate them as much as you want after this an it will have no
effect what so ever on the namespace of any elements or attributes. The
only difference will occur during serialization. Then, if an element is
found that is in a namespace, an algorithm is used to see what prefix
should be used and if a namespace attr should be created.
If you created a document element in the correct namespace (say,
http://foo.com) and then used createElement then the child nodes would be
in no namespace. You should use createElementNS("http//foo.com", "myEle").
Note there is no need to put a prefix. If you do, when you serialize it
may stay the same.
Gareth
--
Gareth Reakes, Managing Director Parthenon Computing
+44-1865-811184 http://www.parthcomp.com
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: Writing documents with schemas and namespaces...
Posted by Nick Bastin <nb...@opnet.com>.
On Apr 21, 2004, at 5:33 PM, Alberto Massari wrote:
> At 11.47 21/04/2004 -0400, Nick Bastin wrote:
>> When writing out documents which have schemas and namespaces, am I
>> supposed to set up the createDocument() and createElement() calls
>> differently, or just add the attributes myself?
>>
>> For example, what code would I need to create:
>>
>> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
>> <ndr:Template xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xmlns:ndr="http://www.opnet.com/ns/NetDoctor"
>> xsi:schemaLocation="http://www.opnet.com/ns/NetDoctor
>> /schemas/Report_Template?v11">
>> <...tags...>
>> </ndr:Template>
>>
>> I can read this file successfully, but I'm not having a lot of luck
>> writing it. Now, I'm assuming that I'm not supposed to write those
>> attributes in directly, so maybe that's my problem. Basically, the
>> question leads to - is there a lot of builtin support for writing
>> documents with schemas and namespaces, or do you just call
>> createElement with tags that already have their namespace qualifier
>> in the name (like "ndr:Template"), or do you call it with "Template"
>> and you've already told it a namespace?
>
> The choice is up to you: you can use createElement("ndr:Template), but
> you also need to place the namespace definitions using
> setAttribute("xmlns:ndr","http://www.opnet.com/ns/NetDoctor"); or you
> use the DOM L2 API
> createElementNS("http://www.opnet.com/ns/NetDoctor", "ndr:Template")
> and the DOMWriter that will serialize it will create the (hopefully)
> right namespace declarations for you.
So I noticed this, but I don't *quite* understand it. You still have
to put a fully-qualified name in createElementNS, so what's the point
of calling it all the time? Obviously you need to call it once, so
that you get the xmlns:ndr="foo" attribute on the correct node, but
every child of that node should probably, for efficiency, be called
using createElement(). Or am I missing something? I.e., if I create
my document node with the right namespace (presuming it is used in the
entire document), is there any reason to call createElementNS for the
child nodes? It seems that it would save both memory and time to just
call createElement(), but I may not have a full understanding of the
side-effects.
--
Nick
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
Re: Writing documents with schemas and namespaces...
Posted by Alberto Massari <am...@progress.com>.
At 11.47 21/04/2004 -0400, Nick Bastin wrote:
>When writing out documents which have schemas and namespaces, am I
>supposed to set up the createDocument() and createElement() calls
>differently, or just add the attributes myself?
>
>For example, what code would I need to create:
>
><?xml version="1.0" encoding="UTF-8" standalone="no"?>
><ndr:Template xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>xmlns:ndr="http://www.opnet.com/ns/NetDoctor"
>xsi:schemaLocation="http://www.opnet.com/ns/NetDoctor
>/schemas/Report_Template?v11">
> <...tags...>
></ndr:Template>
>
>I can read this file successfully, but I'm not having a lot of luck
>writing it. Now, I'm assuming that I'm not supposed to write those
>attributes in directly, so maybe that's my problem. Basically, the
>question leads to - is there a lot of builtin support for writing
>documents with schemas and namespaces, or do you just call createElement
>with tags that already have their namespace qualifier in the name (like
>"ndr:Template"), or do you call it with "Template" and you've already told
>it a namespace?
The choice is up to you: you can use createElement("ndr:Template), but you
also need to place the namespace definitions using
setAttribute("xmlns:ndr","http://www.opnet.com/ns/NetDoctor"); or you use
the DOM L2 API createElementNS("http://www.opnet.com/ns/NetDoctor",
"ndr:Template") and the DOMWriter that will serialize it will create the
(hopefully) right namespace declarations for you.
Alberto
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org