472,807 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,807 software developers and data experts.

xmlns="" in created elements

In the XML document I'm trying to create I do the following

elem = _doc.CreateElement("Author");
elem.InnerText = "something";
parentElem.AppendChild(elem);

Thiw works properly however the resulting XML file has the following

<Author xmlns="">something</Author>

what I'm trying to understand is why the xmlns="" is added...what I'm trying
to get is just
<Author>something</Author>

I'm sure I've just done something silly or not set some attribute that I
need to use here.
Thanks
--
Neil
Nov 12 '05 #1
5 12342


NeilL wrote:
In the XML document I'm trying to create I do the following

elem = _doc.CreateElement("Author");
elem.InnerText = "something";
parentElem.AppendChild(elem);

Thiw works properly however the resulting XML file has the following

<Author xmlns="">something</Author>


If you use CreateElement("Author") then you are creating an element in
no namespace. If you insert the element as a child of some element that
is in a namespace then the serializer has to add the xmlns="" to make
sure the proper markup is created.

If you want to create an element in some namespace then you need to
provide the namespace URI to the CreateElement method e.g.
doc.CreateElement("Author", "http://example.com/2005/whatever")
or perhaps you want
doc.CreateElement("Author", parentElem.NamespaceURI);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement.NamespaceURI ?
--
Neil
"Martin Honnen" wrote:


NeilL wrote:
In the XML document I'm trying to create I do the following

elem = _doc.CreateElement("Author");
elem.InnerText = "something";
parentElem.AppendChild(elem);

Thiw works properly however the resulting XML file has the following

<Author xmlns="">something</Author>


If you use CreateElement("Author") then you are creating an element in
no namespace. If you insert the element as a child of some element that
is in a namespace then the serializer has to add the xmlns="" to make
sure the proper markup is created.

If you want to create an element in some namespace then you need to
provide the namespace URI to the CreateElement method e.g.
doc.CreateElement("Author", "http://example.com/2005/whatever")
or perhaps you want
doc.CreateElement("Author", parentElem.NamespaceURI);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #3


NeilL wrote:
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement.NamespaceURI ?


If you regard doc.DocumentElement.NamespaceURI as the "default for the
document" then pass that to CreateElement.
In my understanding there is no such thing as the default for the
document as you can have structures like
<element xmlns="http://example.com/ns1">
<element xmlns="http://example.com/ns2">
<element xmlns="http://example.com/n3" />
</element>
</element>
where the default namespace is declared and redeclared at different levels.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #4
NeilL wrote:
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement.NamespaceURI ?


Does your Document use no Namespaces? Like:

<root>
<elem>
<Author>Joe</Author>
</elem>
</root>

If you just never use the "Namespaced"-Overloads of CreateElement, you
will get this Document.

If you don't specify explicit prefixes (using CreateElement-Overloads)
your document may look like this:

<root> <!-- empty xmlns -->
<elem xmlns="foo">
<bar /> <!-- foo is in xmlns "foo" -->
<Author xmlns="">Joe</Author>
</elem>
</root>

But if you specify prefixes it looks like this:

<root> <!-- empty xmlns -->
<a:elem xmlns:a="foo">
<bar /> <!-- empty xmlns -->
<Author>Joe</Author>
</a:elem>
</root>

Xml Namespaces are "inherited" through the structure, see
<http://www.w3.org/TR/REC-xml-names/#scoping-defaulting>
--
Pascal Schmitt
Nov 12 '05 #5
Ok, Thanks, I thnk I get it (close to getting it in any case).

Using
doc.CreateElement("Author", parentElem.NamespaceURI);
fixed the problem that I was having.

Thanks

--
Neil
"Pascal Schmitt" wrote:
NeilL wrote:
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement.NamespaceURI ?


Does your Document use no Namespaces? Like:

<root>
<elem>
<Author>Joe</Author>
</elem>
</root>

If you just never use the "Namespaced"-Overloads of CreateElement, you
will get this Document.

If you don't specify explicit prefixes (using CreateElement-Overloads)
your document may look like this:

<root> <!-- empty xmlns -->
<elem xmlns="foo">
<bar /> <!-- foo is in xmlns "foo" -->
<Author xmlns="">Joe</Author>
</elem>
</root>

But if you specify prefixes it looks like this:

<root> <!-- empty xmlns -->
<a:elem xmlns:a="foo">
<bar /> <!-- empty xmlns -->
<Author>Joe</Author>
</a:elem>
</root>

Xml Namespaces are "inherited" through the structure, see
<http://www.w3.org/TR/REC-xml-names/#scoping-defaulting>
--
Pascal Schmitt

Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
0
by: johnsocs | last post by:
All I'm trying to write an xml schema for the following xml from the google web service api. In the schema I'm not sure how to describe the soapenv:encodingStyle attribute. Thanks. <?xml...
1
by: Zhenya Sigal via .NET 247 | last post by:
I have the following code: XmlElement parent= m_xmlDoc.CreateElement("parent", "http://tempuri.org/myns"); parent.InnerXml = "<child1>text</child1><child2>text</child2>"; ...
3
by: Keith Hill | last post by:
I am creating an XmlDocument in code and then using XmlTextWriter via doc.WriteTo(xwriter) to output the result to a text box. I have a root element that defines a default namespace. However, the...
6
by: TS | last post by:
Hi, i have a problem validating xml against schema. I used http://apps.gotdotnet.com/xmltools/xsdvalidator/Default.aspx validator and it says it is fine. Can you tell me why this doesn't work? ...
2
by: Andy Fish | last post by:
Hi, I have discovered that if I use this syntax: public string bar() { return "hello, world"; } then it is not possible to call the web service using the MS soap toolkit.
0
by: R. Ian Lee | last post by:
I've built an XSLT file that transforms data to SpreadsheetML format. The XSLT uses a <xsl:call-template/to build each worksheet. For some reason, when I transform the file, it is inserting...
4
by: BorisBoshond | last post by:
Hi all, Hope someone is able and willing to help me with following problem. I received a xsd file from another company, our company i supposed to return xml based on that xsd. Problem is that I...
4
markmcgookin
by: markmcgookin | last post by:
Hi, I am creating an MXL doc using XSLT but for some reason it is churning out elements like this <DateTimeLastSaved xmlns="" /> <UserName xmlns="" /> when I delete xmlns="" it works fine,...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.