XML-Schema and comlpexTypes

Discussion in 'XML' started by Jakob Møbjerg Nielsen, Dec 9, 2003.

  1. Hi

    Is it possible to represent a linked list in XML-Schema. Somthing like:

    <xsd:complexType name="Llist" >
    <xsd:sequence>
    <xsd:element name="content" type="xsd:string"/>
    <xsd:element name="next" type="Llist"/>
    </xsd:sequence>
    </xsd:complexType>

    --
    Jakob Møbjerg Nielsen | "Nine-tenths of the universe is the
    | knowledge of the position and direction
    http://www.jakobnielsen.dk/ | of everything in the other tenth."
    | -- Terry Pratchett, Thief of Time
     
    Jakob Møbjerg Nielsen, Dec 9, 2003
    #1
    1. Advertising

  2. Jakob Møbjerg Nielsen wrote:

    > Is it possible to represent a linked list in XML-Schema. Somthing like:
    >
    > <xsd:complexType name="Llist" >
    > <xsd:sequence>
    > <xsd:element name="content" type="xsd:string"/>
    > <xsd:element name="next" type="Llist"/>
    > </xsd:sequence>
    > </xsd:complexType>


    Why not, the only flaw with your scheme is that I see no way to
    terminate the list so I think

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:complexType name="Llist" >
    <xs:sequence>
    <xs:element name="content" type="xsd:string"/>
    <xs:element name="next" type="Llist" minOccurs="0" maxOccurs="1" />
    </xs:sequence>
    </xs:complexType>

    <xs:element name="list" type="Llist" />

    </xs:schema>

    is better.
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 9, 2003
    #2
    1. Advertising

  3. Martin Honnen wrote:
    > Why not, the only flaw with your scheme is that I see no way to
    > terminate the list so I think

    [snip]
    > is better.


    Thanks, but I still have my doubts wether it is legal or not to do
    recursion in XML-Schema. I can't find anything about it in the
    recommendation.

    --
    Jakob Møbjerg Nielsen | "Nine-tenths of the universe is the
    | knowledge of the position and direction
    http://www.jakobnielsen.dk/ | of everything in the other tenth."
    | -- Terry Pratchett, Thief of Time
     
    Jakob Møbjerg Nielsen, Dec 9, 2003
    #3
  4. Jakob Møbjerg Nielsen wrote:

    > Thanks, but I still have my doubts wether it is legal or not to do
    > recursion in XML-Schema. I can't find anything about it in the
    > recommendation.


    Recursion is allowed in XML documents and with XML schemas, why
    shouldn't it. Try that example schema with any parser validating against
    XML schema and I don't think you will get any errors.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 9, 2003
    #4
  5. Martin Honnen wrote:
    > Recursion is allowed in XML documents and with XML schemas, why
    > shouldn't it. Try that example schema with any parser validating
    > against XML schema and I don't think you will get any errors.


    Thanks again... I'm convinced, but I'm having just a *bit* trouble in
    seeing how it will look in "straight" XML. My guess is:

    <list>
    <Llist>
    <content>El.1</content>
    <Llist>
    <content>El.2</content>
    <Llist>
    <content>El.3</content>
    <Llist>
    <content>El.4</content>
    </Llist>
    </Llist>
    </Llist>
    </Llist>
    </list>

    Is that all wrong? It looks kind of ugly, but if that's the only way...
    :)

    --
    Jakob Møbjerg Nielsen | "Nine-tenths of the universe is the
    | knowledge of the position and direction
    http://www.jakobnielsen.dk/ | of everything in the other tenth."
    | -- Terry Pratchett, Thief of Time
     
    Jakob Møbjerg Nielsen, Dec 9, 2003
    #5
  6. Martin Honnen wrote:
    > Recursion is allowed in XML documents and with XML schemas, why
    > shouldn't it. Try that example schema with any parser validating
    > against XML schema and I don't think you will get any errors.


    I just found out that Schemas like
    http://www.w3.org/2001/03/XMLSchema/TypeLibrary-nn-list.xsd (which
    contains an example of how W3 would implement a linked list), cannot be
    viewed with Mozilla. It only displays the documentation elements. IE
    shows everything, though. Why, Mozilla... why? :)

    --
    Jakob Møbjerg Nielsen | "Nine-tenths of the universe is the
    | knowledge of the position and direction
    http://www.jakobnielsen.dk/ | of everything in the other tenth."
    | -- Terry Pratchett, Thief of Time
     
    Jakob Møbjerg Nielsen, Dec 9, 2003
    #6
  7. Jakob Møbjerg Nielsen wrote:

    > Martin Honnen wrote:
    >
    >>Recursion is allowed in XML documents and with XML schemas, why
    >>shouldn't it. Try that example schema with any parser validating
    >>against XML schema and I don't think you will get any errors.

    >
    >
    > Thanks again... I'm convinced, but I'm having just a *bit* trouble in
    > seeing how it will look in "straight" XML. My guess is:
    >
    > <list>
    > <Llist>
    > <content>El.1</content>
    > <Llist>
    > <content>El.2</content>
    > <Llist>
    > <content>El.3</content>
    > <Llist>
    > <content>El.4</content>
    > </Llist>
    > </Llist>
    > </Llist>
    > </Llist>
    > </list>
    >
    > Is that all wrong? It looks kind of ugly, but if that's the only way...


    Your schema I improved is

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:complexType name="Llist" >
    <xs:sequence>
    <xs:element name="content" type="xsd:string"/>
    <xs:element name="next" type="Llist" minOccurs="0" maxOccurs="1" />

    An instance is

    <?xml version="1.0" encoding="iso-8859-1"?>
    <list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="test20031209Xsd.xml">
    <content>1</content>
    <next>
    <content>2</content>
    <next>
    <content>3</content>
    </next>
    </next>
    </list>
    </xs:sequence>
    </xs:complexType>

    <xs:element name="list" type="Llist" />

    </xs:schema>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 9, 2003
    #7
  8. Jakob Møbjerg Nielsen wrote:

    > Martin Honnen wrote:
    >
    >>Recursion is allowed in XML documents and with XML schemas, why
    >>shouldn't it. Try that example schema with any parser validating
    >>against XML schema and I don't think you will get any errors.

    >
    >
    > I just found out that Schemas like
    > http://www.w3.org/2001/03/XMLSchema/TypeLibrary-nn-list.xsd (which
    > contains an example of how W3 would implement a linked list), cannot be
    > viewed with Mozilla. It only displays the documentation elements. IE
    > shows everything, though. Why, Mozilla... why? :)


    Well, IE(5/6)/Win applies an XSLT default stylesheet to the XML which
    renders a clickable tree with dynamic HTML. IE doesn't recognize that
    there are any XHTML elements in the document as IE doesn't support XHTML.
    Mozilla however supports XHTML and recognizes that there are elements
    with the XHTML namespace http://www.w3.org/1999/xhtml in the document
    and therefore tries to render the XML file as it is, meaning it applies
    the XHTML rendering rules to the XHTML elements and CSS default rules to
    the unknown xs:tagname elements. That has not the result that you want
    but is a possible approach which allows you to write mixed namespace XML
    documents using XHTML elements and style them with CSS and have them
    rendered by Mozilla.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 9, 2003
    #8
  9. In article <br4feg$qp8$>,
    Jakob Møbjerg Nielsen <> wrote:

    % Is it possible to represent a linked list in XML-Schema. Somthing like:
    %
    % <xsd:complexType name="Llist" >
    % <xsd:sequence>
    % <xsd:element name="content" type="xsd:string"/>
    % <xsd:element name="next" type="Llist"/>
    % </xsd:sequence>
    % </xsd:complexType>

    I wouldn't call this a linked-list representation. After all, `next'
    embeds the next element of the list as content. I'd go so far as to
    say it's not a correct representation of a list at all. A linked list would
    assign an id to each element, and have next refer to that id

    <xsd:complexType name="Llist" >
    <xsd:simplecontent>
    <xsd:extension base="xsd:string">
    <xsd:attribute name="id" type="xsd:ID"/>
    <xsd:attribute name="next" type="xsd:IDREF" minOccurs='0'/>
    </xsd:extension>
    </xsd:complexType>

    <xsd:element name='list1' type='Llist'/>

    or something like that. The XML would come out like this:

    <list1 id='a' next='b'>data 1</list1>
    <list1 id='b' next='c'>data 2</list1>
    <list1 id='c' next='d'>data 3</list1>
    <list1 id='d'>data 4</list1>


    Having said that, it doesn't make any sense. A linked list makes sense
    as a data structure because it simplifies the implementation of certain
    algorithms and sometimes makes memory management easier, but in an XML
    document, it sounds goofy, and it's much more work since you have to
    keep track of all the ids. Why not make it a real list?

    <xsd:complexType name="Rlist" >
    <xsd:element name="item" type="xsd:string" minOccurs='0'
    maxOccurs='unbounded'/>
    </xsd:complexType>

    <xsd:element name='list2' type='Rlist'/>

    which would appear like this

    <list2>
    <item>data 1</item>
    <item>data 2</item>
    <item>data 3</item>
    <item>data 4</item>
    </list2>
    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Dec 9, 2003
    #9
    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.

Share This Page