Order-independent tags in XML

Discussion in 'XML' started by keksforscher, Jan 12, 2008.

  1. keksforscher

    keksforscher Guest

    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
    keksforscher, Jan 12, 2008
    #1
    1. Advertising

  2. keksforscher

    Peter Flynn Guest

    On Sat, 12 Jan 2008 12:14:15 -0800, keksforscher wrote:

    > 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
    --
    XML FAQ: http://xml.silmaril.ie/
    Peter Flynn, Jan 12, 2008
    #2
    1. Advertising

  3. keksforscher

    keshlam Guest

    (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.
    keshlam, Jan 13, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Schanks
    Replies:
    0
    Views:
    643
    Raymond Schanks
    Aug 3, 2010
  2. Mazen S. Alzogbi

    XML output tags order

    Mazen S. Alzogbi, Sep 28, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    100
    Mazen S. Alzogbi
    Sep 28, 2004
  3. Neal Becker

    order independent hash?

    Neal Becker, Nov 30, 2011, in forum: Python
    Replies:
    5
    Views:
    162
    88888 Dihedral
    Dec 3, 2011
  4. Peter Otten

    Re: order independent hash?

    Peter Otten, Nov 30, 2011, in forum: Python
    Replies:
    49
    Views:
    677
    Lie Ryan
    Dec 11, 2011
  5. Dan Stromberg

    Re: order independent hash?

    Dan Stromberg, Dec 5, 2011, in forum: Python
    Replies:
    3
    Views:
    266
    Dan Stromberg
    Dec 5, 2011
Loading...

Share This Page