XML - xmlns

V

Ve

Hi,

I have XmlDocument object.

-how to get information, if the XmlObject have default schema
(xmlns=â€something†in main node) ?
-how to get default schema (as a string or other object) ?
-how to delete default schema ?
-how to set default schema ?

Thanks for help
 
M

Martin Honnen

Ve said:
I have XmlDocument object.

-how to get information, if the XmlObject have default schema
(xmlns=â€something†in main node) ?

xmlns="something" is a "default namespace declaration", it usually has
nothing to do with a schema. And there is nothing like a "default schema".

If you want to apply XPath expressions to select elements that are in
the default namespace then you need to use an XmlNamespaceManager to
bind a prefix (in my example "df") to the namespace name e.g.
XmlDocument doc = new XmlDocument();
doc.Load("file.xml");
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("df", "something");
then you can use that prefix in XPath expressions e.g.
XmlNodeList list = doc.SelectNodes("df:root/df:foo/df:bar", nsMgr);
 
V

Ve

Martin Honnen pisze:
xmlns="something" is a "default namespace declaration", it usually has
nothing to do with a schema. And there is nothing like a "default schema".

Of course you are right!
If you want to apply XPath expressions to select elements that are in
the default namespace then you need to use an XmlNamespaceManager to
bind a prefix (in my example "df") to the namespace name e.g.
XmlDocument doc = new XmlDocument();
doc.Load("file.xml");
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("df", "something");
then you can use that prefix in XPath expressions e.g.
XmlNodeList list = doc.SelectNodes("df:root/df:foo/df:bar", nsMgr);

But using this code I am able to select nodes in default namespace.

But how can I check if the xml is containing a default namespace ?
How to delete default namespace from XmlDocument?
How to get name of the default namespace ?

Thanks
 
M

Martin Honnen

Ve said:
But how can I check if the xml is containing a default namespace ?
How to delete default namespace from XmlDocument?
How to get name of the default namespace ?

Generally for your DOM and XPath processing it should not matter whether
a default namespace (e.g. xmlns="http://example.com/ns1") or a normal
namespace declaration (e.g. xmlns:pf="http://example.com/ns1") is
applied, you simply want to know whether an element or attribute node is
in a certain namespace or not. For that in the DOM model you can check
the NamespaceURI property of an element or attribute node
<URL:http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.namespaceuri.aspx>.
The DOM implementation in the .NET framework also exposes two methods
GetNamespaceOfPrefix
<URL:http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.getnamespaceofprefix.aspx>
and GetPrefixOfNamespace
<URL:http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.getprefixofnamespace.aspx>
that help dealing with namespaces.
So to get the name (URI) of the default namespace defined on the root
element you can do
doc.DocumentElement.GetNamespaceOfPrefix("")
and you can check the result against the empty string to find out
whether a default namespace declaration exists.

doc.LoadXml(
"<root xmlns=\"http://example.com/ns1\"><foo>bar</foo></root>");
Console.WriteLine("GetNamespaceOfPrefix(\"\"): \"{0}\".",
doc.DocumentElement.GetNamespaceOfPrefix(""));
// GetNamespaceOfPrefix(""): "http://example.com/ns1".
doc.LoadXml("<root><foo>bar</foo></root>");
Console.WriteLine("GetNamespaceOfPrefix(\"\"): \"{0}\".",
doc.DocumentElement.GetNamespaceOfPrefix(""));
// GetNamespaceOfPrefix(""): "".

As for removing a namespace, that is technically not possible, at least
not in the way that you can change the namespace a node is in, the
NamespaceURI property of a node is readonly. So you need to create new
nodes in no namespace. An XSLT stylesheet is good at implementing such
transformations:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@* | comment() | processing-instruction() |
text()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
This stylesheet strips any namespace from element nodes in the input XML
document. XSLT transformations can be applied using
System.Xml.Xsl.XslCompiledTransform.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top