Re: Cannot find root element in XML file
- From: "John Saunders" <john.saunders at trizetto.com>
- Date: Fri, 25 Aug 2006 05:58:11 -0400
"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message news:A90D417D-0A0F-4893-B88C-BF66612B1596@xxxxxxxxxxxxxxxx
Hi John,
"John Saunders" wrote:
"TT (Tom Tempelaere)" <_|\|_0$P@|/\|titi____AThotmailD.Tcom|/\|@P$0_|\|_>
wrote in message
news:8BC434B4-CCE7-43CD-81D2-611DF074BC63@xxxxxxxxxxxxxxxx
Martin,
"Martin Honnen" wrote:
TT (Tom Tempelaere) wrote:
They don't work because from what I understood I only needed the
namespace
thingy for querying the root node. The nodes in the XML file aren't
qualified
so why would I have to qualify them to look them up?
You have not even bothered to show the contents of the root element
but
your root element has
<IncidentDefinitions xmlns="http://tempuri.org/XMLSchema.xsd"
which means any child or descendant elements are in that namespace too
unless they have a redeclaration e.g.
<child xmlns="">
to have the element in no namespace or e.g.
<child xmlns="http://example.com/ns1">
to have the element in a different namespace.
See <http://www.w3.org/TR/REC-xml-names/#defaulting>
Oki, thanks for the information.
What I notice is that using the XPath expressions for the XML file
where
the
root has an xmlns attribute, doesn't work for XML files where the root
has
no
xmlns attribute (and vice versa). Is there any easy way to support both
using
the same XPath expressions? Or would I have to force my clients to
either
add
the xmlns attribute or ommit it?
The presence or absence of the xmlns attribute mean different things.
With
xmlns="urn:something", you're saying that all of the elements and
attributes
in the document are in the namespace "urn:something". For example, in:
<root xmlns="urn:something" id="1"/>
"root" really means {urn:something}:root and "id" means
{urn:something}:id.
When you're doing an XPath lookup for "root", you're looking for {}:root,
which is a totally different thing.
So, it's not that your clients have to either add or omit the xmlns
attribute - it's that they have to send you documents with the elements
and
attributes in the correct namespace. If you've defined things so that
your
elements aren't in a namespace, then they shouldn't send you elements
which
are in a namespace. If you'ev defined things (properly), so that your
elements are in a namespace, then your clients have to send you documents
where the elements are in that namespace.
John
Thanks for that John. So the way I understand is that if I define my root
in
XSD as follows:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="IncidentDefinitions">
...definitions etc...
</xs:schema>
Then this schema basically sais that any element and types that it defines
become members of the "http://tempuri.org/XMLSchema.xsd" namespace because
of
the targetNamespace attribute of the xs:schema element? Or is it the xmlns
attribute (the value of both in the XSD is the same)?
And, that any XML document that is to conform with the schema, has to
attribute the root with xmlns attribute referring to that namespace?
You've got it. It's the targetNamespace which defines these elements in that
namespace. It's the xmlns on the insance document which specifies that those
elements refer to the same ones you defined - which is what you probably had
in mind.
Also, keep in mind that there is more than one way to place instance
elements into your namespace. For instance, <IncidentDefinitions
xmlns="http://tempuri.org/XMLSchema.xsd"/> or <xxx:IncidentDefinitions
xmlns:xxx="http://tempuri.org/XMLSchema.xsd"/> or <yyy:IncidentDefinitions
xmlns:yyy="http://tempuri.org/XMLSchema.xsd"/> are all the same thing.
John
.
- References:
- Re: Cannot find root element in XML file
- From: Martin Honnen
- Re: Cannot find root element in XML file
- From: TT (Tom Tempelaere)
- Re: Cannot find root element in XML file
- From: Martin Honnen
- Re: Cannot find root element in XML file
- From: TT (Tom Tempelaere)
- Re: Cannot find root element in XML file
- From: Martin Honnen
- Re: Cannot find root element in XML file
- From: John Saunders
- Re: Cannot find root element in XML file
- From: TT (Tom Tempelaere)
- Re: Cannot find root element in XML file
- Prev by Date: Re: getting the value from text file to xml
- Next by Date: Re: XMLDataDocument <-> DataSet -- out of sequence
- Previous by thread: Re: Cannot find root element in XML file
- Next by thread: Re: One final question about namespaces :)
- Index(es):
Relevant Pages
|