XML namespace / element name question

B

ByteCoder

I just have a probably simple XML question, but I just can't find an
answer to it:

It seems that two sibling elements, which are in the same namespace, may
not have the same name (tag name). Is this correct? If yes, should my
parser return an error when it sees this?
An example:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<sub1>dskjh</sub1>
<sub2>haskjh</sub2>
<sub2>xzjg</sub2>
</root>

Am I correct to assume the above code is *not* well formed?

<?xml version="1.0" encoding="UTF-8"?>
<root>
<root>
</root>
</root>

I suspect that the above code is well formed, but is it? In other words:
Does XML allow this kind of redundancy?

Thanks for your help.

--
 
W

William Park

ByteCoder said:
I just have a probably simple XML question, but I just can't find an
answer to it:

It seems that two sibling elements, which are in the same namespace, may
not have the same name (tag name). Is this correct? If yes, should my
parser return an error when it sees this?
An example:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<sub1>dskjh</sub1>
<sub2>haskjh</sub2>
<sub2>xzjg</sub2>
</root>

Am I correct to assume the above code is *not* well formed?

<?xml version="1.0" encoding="UTF-8"?>
<root>
<root>
</root>
</root>

I suspect that the above code is well formed, but is it? In other words:
Does XML allow this kind of redundancy?

They both well-formed.

--
William Park <[email protected]>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
 
M

Malcolm Dew-Jones

ByteCoder ([email protected]) wrote:
: I just have a probably simple XML question, but I just can't find an
: answer to it:

: It seems that two sibling elements, which are in the same namespace, may
: not have the same name (tag name). Is this correct? If yes, should my
: parser return an error when it sees this?
: An example:
: <?xml version="1.0" encoding="UTF-8"?>
: <root>
: <sub1>dskjh</sub1>
: <sub2>haskjh</sub2>
: <sub2>xzjg</sub2>
: </root>

: Am I correct to assume the above code is *not* well formed?

No, it looks fine.

: <?xml version="1.0" encoding="UTF-8"?>
: <root>
: <root>
: </root>
: </root>

: I suspect that the above code is well formed, but is it? In other words:
: Does XML allow this kind of redundancy?

Yes, it does, that also looks fine.


You are confusing "well formed" with "valid". An xml file can have a DTD
or a Schema that describes which tags can be used, and where they can be
used (among other things).

If you do not validate the XML then the above is always fine because the
syntax etc is correct.

Of you can choose to validate the XML. To do that you need to define (and
then use) a DTD or Schema to describe what is valid. If your DTD or
Schema allows the tags the way you show them then the XML will validate
ok. But if your DTD or Schema does not allow the tags like that then the
file will fail validation (and cause an error). So, the same xml data can
be either valid or invalid depending on what you want.

Some applications will validate their XML input, for example a config file
needs to be correctly setup or the application can't use it, so the
application validates the config file.

Other xml applications would not validate their input because they don't
care what is in the xml data, for example an xml search and replace
utility doesn't care what the data looks like, as long as it's well formed
xml.
 
M

Martin Honnen

ByteCoder wrote:

It seems that two sibling elements, which are in the same namespace, may
not have the same name (tag name). Is this correct? If yes, should my
parser return an error when it sees this?
An example:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<sub1>dskjh</sub1>
<sub2>haskjh</sub2>
<sub2>xzjg</sub2>
</root>

There are no namespaces used at all in the above XML, all elements are
in no namespace so the example XML does not even apply to the alleged
rule you have given.
But well-formedness does not make any restrictions alike "It seems that
two sibling elements, which are in the same namespace, may
not have the same name (tag name)". Of course you could write a schema
or DTD for a particular XML application where you restrict the contents
of elements as needed but then it is a question of validity and not
well-formedness.
 
B

ByteCoder

(e-mail address removed) (Malcolm Dew-Jones) wrote in
ByteCoder ([email protected]) wrote:
: I just have a probably simple XML question, but I just can't find an
: answer to it:

: It seems that two sibling elements, which are in the same namespace,
: may not have the same name (tag name). Is this correct? If yes,
: should my parser return an error when it sees this?
: An example:
: <?xml version="1.0" encoding="UTF-8"?>
: <root>
: <sub1>dskjh</sub1>
: <sub2>haskjh</sub2>
: <sub2>xzjg</sub2>
: </root>

: Am I correct to assume the above code is *not* well formed?

No, it looks fine.

: <?xml version="1.0" encoding="UTF-8"?>
: <root>
: <root>
: </root>
: </root>

: I suspect that the above code is well formed, but is it? In other
: words: Does XML allow this kind of redundancy?

Yes, it does, that also looks fine.


You are confusing "well formed" with "valid". An xml file can have a
DTD or a Schema that describes which tags can be used, and where they
can be used (among other things).

If you do not validate the XML then the above is always fine because
the syntax etc is correct.

Of you can choose to validate the XML. To do that you need to define
(and then use) a DTD or Schema to describe what is valid. If your DTD
or Schema allows the tags the way you show them then the XML will
validate ok. But if your DTD or Schema does not allow the tags like
that then the file will fail validation (and cause an error). So, the
same xml data can be either valid or invalid depending on what you
want.

Some applications will validate their XML input, for example a config
file needs to be correctly setup or the application can't use it, so
the application validates the config file.

Other xml applications would not validate their input because they
don't care what is in the xml data, for example an xml search and
replace utility doesn't care what the data looks like, as long as it's
well formed xml.

Thank you. Added XML Schemas and DTD reading to my to-do list. ;)

--
 
B

ByteCoder

ByteCoder wrote:



There are no namespaces used at all in the above XML, all elements are
in no namespace so the example XML does not even apply to the alleged
rule you have given.

True, but I meant it more like: If they were in the same namespace...
Anyway, you get the point.
But well-formedness does not make any restrictions alike "It seems
that two sibling elements, which are in the same namespace, may
not have the same name (tag name)". Of course you could write a schema
or DTD for a particular XML application where you restrict the
contents of elements as needed but then it is a question of validity
and not well-formedness.

Thanks. :)

--
 

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

Members online

Forum statistics

Threads
474,001
Messages
2,570,249
Members
46,846
Latest member
BettinaOsw

Latest Threads

Top