XML DTD Validation, namespaces and best practices

J

johnmack

In my XML instances I want to use the XLink namespace for attributes on
certain elements. I'm having quite a hard time determining how to
reflect this properly in my DTD and XML instances.

In my instance, to declare the namespace, I have this on my root
element:
<root xmlns:xlink="http://www.w3.org/1999/xlink">
...
<mylink xlink:href="http://foo.com">
....

Declaring things as I've done on the <root> element here, is this
proper? Do I have to declare 'xmlns:xlink' as an attribute on <root>
in my DTD? Or do attributes starting with 'xmlns:' trigger an innate
XML processing command and therefore do not need to be defined in a
DTD?

In my DTD I have explicitly defined "xlink:href" as attribute like so:
<!ELEMENT mylink (#PCDATA)>
<!ATTLIST mylink
xlink:href CDATA #REQUIRED>

Is this an acceptable approach or is the following a better way to do
it in the DTD?:
<!ATTLIST mylink
xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink">


I'm using the XML validator at:
"http://www.stg.brown.edu/service/xmlvalid/", and when I have 'Relax
namespace checks' selected, my XML documents validate. To clarify
further they only validate when I have the namespace in <root> ( as
noted above) *and* declaring explicitly 'xlink:href' as an attribute
(<!ATTLIST mylink xlink:href CDATA #REQUIRED>) on 'mylink' in my DTD.
Since the validator says this is OK, I'm using what I've done is OK?
It seems weird to have to explicitly declare 'xlink:href' as an
attribute in my DTD so I'm thinking this may not be proper. Are there
other options for me to pursue? Is there a suggested best practice?
It's been tough to find clear solutions for this matter.

Thanks for any insight,

John Mack
 
R

Richard Tobin

In my instance, to declare the namespace, I have this on my root
element:
<root xmlns:xlink="http://www.w3.org/1999/xlink">
...
<mylink xlink:href="http://foo.com">
....

Declaring things as I've done on the <root> element here, is this
proper?

Yes. You don't have to declare them on the root, but it's often
convenient.
Do I have to declare 'xmlns:xlink' as an attribute on <root>
in my DTD?
Yes.

Or do attributes starting with 'xmlns:' trigger an innate
XML processing command and therefore do not need to be defined in a
DTD?

No, DTDs have not been updated to take account of namespaces, so
namespace declaration attributes have to be declared like any others.
In my DTD I have explicitly defined "xlink:href" as attribute like so:
<!ELEMENT mylink (#PCDATA)>
<!ATTLIST mylink
xlink:href CDATA #REQUIRED>

Is this an acceptable approach or is the following a better way to do
it in the DTD?:
<!ATTLIST mylink
xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink">

The second saves you having to remember to put it on the element in
the instance, but if it's declared in the external subset you'd better
be sure that all the XML processors you use read the external subset.
I'm using the XML validator at:
"http://www.stg.brown.edu/service/xmlvalid/", and when I have 'Relax
namespace checks' selected, my XML documents validate. To clarify
further they only validate when I have the namespace in <root> ( as
noted above) *and* declaring explicitly 'xlink:href' as an attribute
(<!ATTLIST mylink xlink:href CDATA #REQUIRED>) on 'mylink' in my DTD.
Since the validator says this is OK, I'm using what I've done is OK?
It seems weird to have to explicitly declare 'xlink:href' as an
attribute in my DTD so I'm thinking this may not be proper. Are there
other options for me to pursue? Is there a suggested best practice?

The "relaxed namespace checks" are supported by any standard. Your
document is not DTD-valid unless you declare the attribute. On the
other hand, you don't *have* to make your documents DTD-valid at all.
If you find that the "relaxed" validation is useful, and you don't
need true DTD-validity, then it's a reasonable approach.

-- Richard
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top