Order-independent tags in XML

K

keksforscher

Hello,

consider the following DTD and XML:

<?xml version="1.0" encoding="UTF-8"?>
<!--DTD generated by XMLSpy v2007 (http://www.altova.com)-->
<!ELEMENT root ((name, city))>
<!ELEMENT name (#PCDATA)>
<!ELEMENT city (#PCDATA)>

<!DOCTYPE root SYSTEM "d:\1.dtd">
<root>
<name>qw</name>
<city>qw</city>
</root>

Problem is that the tags name and city alsways have to be in the XML
in the order defined in the DTD. The following XML would be invalid:

<!DOCTYPE root SYSTEM "d:\1.dtd">
<root>
<city>qw</city>
<name>qw</name>
</root>

I could change the DTD of course to:

<?xml version="1.0" encoding="UTF-8"?>
<!--DTD generated by XMLSpy v2007 (http://www.altova.com)-->
<!ELEMENT root ((name | city))>
<!ELEMENT name (#PCDATA)>
<!ELEMENT city (#PCDATA)>

Now the tags could inserted in the xml in any order but because of the
OR the tags could be omitted at all..

Ich möchte aber, daß die beiden Tags Pflicht sind aber deren
Reihenfolge mir egal ist.

How could this be solved?

Thanks

Stefan
 
P

Peter Flynn

Hello,

consider the following DTD and XML:

<?xml version="1.0" encoding="UTF-8"?> <!--DTD generated by XMLSpy v2007
(http://www.altova.com)--> <!ELEMENT root ((name, city))>
<!ELEMENT name (#PCDATA)>
<!ELEMENT city (#PCDATA)>

<!DOCTYPE root SYSTEM "d:\1.dtd">
<root>
<name>qw</name>
<city>qw</city>
</root>

Problem is that the tags name and city alsways have to be in the XML in
the order defined in the DTD. The following XML would be invalid:

<!DOCTYPE root SYSTEM "d:\1.dtd">
<root>
<city>qw</city>
<name>qw</name>
</root>

I could change the DTD of course to:

<?xml version="1.0" encoding="UTF-8"?> <!--DTD generated by XMLSpy v2007
(http://www.altova.com)--> <!ELEMENT root ((name | city))>
<!ELEMENT name (#PCDATA)>
<!ELEMENT city (#PCDATA)>

Now the tags could inserted in the xml in any order but because of the
OR the tags could be omitted at all..

Ich möchte aber, daß die beiden Tags Pflicht sind aber deren Reihenfolge
mir egal ist.

How could this be solved?

<!ELEMENT name ((city,name)|(name,city))>

In more complex examples, maybe use SGML to control creation of the
document instead.

<!doctype root [
<!ELEMENT root - - ((name & city))>
<!ELEMENT name - - (#PCDATA)>
<!ELEMENT city - - (#PCDATA)>
]>

The result will also be a well-formed XML document, but you'll need to
use an SGML-conformant editor -- many of the bigger, older XML editors
also handle SGML still (but watch out for default uppercasing).

The & connector was omitted from XML for brevity and simplicity, so the
file created will be well-formed but cannot be validated in XML.

///Peter
 
K

keshlam

(name|city)+ will require that at least one be present, without
constraining the order.It will not prevent multiple instances; if you
want to do that, you need to spell out the possibilities using
something like (name,city?)|(city,name?)

It is often better to just insist that folks writing these documents
provide the elements in the expected order.
 

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

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top