XML-Schema and comlpexTypes

  • Thread starter Jakob Møbjerg Nielsen
  • Start date
J

Jakob Møbjerg Nielsen

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>
 
M

Martin Honnen

Jakob said:
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.
 
J

Jakob Møbjerg Nielsen

Martin said:
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.
 
M

Martin Honnen

Jakob said:
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.
 
J

Jakob Møbjerg Nielsen

Martin said:
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...
:)
 
J

Jakob Møbjerg Nielsen

Martin said:
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? :)
 
M

Martin Honnen

Jakob said:
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>
 
M

Martin Honnen

Jakob said:
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.
 
P

Patrick TJ McPhee

% 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>
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top