Click to See Complete Forum and Search --> : [RESOLVED] Trouble with switching from DTD to XSD
Mr Initial Man
03-10-2009, 01:39 AM
Here is an element from a DTD that I'm rewriting in XSD:
<!ELEMENT c (#PCDATA|a|em|movie|publication|strip)*>
<!ATTLIST c
poster CDATA #REQUIRED
named CDATA #FIXED "named"
>
<!ELEMENT a (#PCDATA)>
<!ATTLIST a href CDATA #REQUIRED>
<!ELEMENT em (#PCDATA)>
<!ELEMENT movie (#PCDATA)>
<!ELEMENT publication (#PCDATA)>
<!ELEMENT strip (#PCDATA)>
As you can see, a, em, movie, publication, and strip can occur any number of times in any order.
Here is the XSD for this element:
<xs:element name="c" minOccurs="1" maxOccurs="unbounded">
<xs:complexType mixed="true">
<xs:what>
<xs:element name="strip" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="publication" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="movie" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="em" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="a" minOccurs="0" maxOccurs="unbounded">
<xs:complexType mixed="true">
<xs:attribute name="href" use="required" type="xs:anyURI" />
</xs:complexType>
</xs:element>
</xs:what>
<xs:attribute name="poster" type="xs:string" />
<xs:attribute name="named" type="xs:string" fixed="named" />
</xs:complexType>
</xs:element>
What do I replace xs:what with?
Below is a list of what I CAN'T use.
sequence
The elements contained in xs:what may occur in any order.
choice
All of the elements may occur together, any number of times, or not at all.
all
The elements contained in xs:what may occur any number of times.
JohnBampton
03-10-2009, 03:21 AM
sequence The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times
choice Allows only one of the elements contained in the <choice> declaration to be present within the containing element
all Specifies that the child elements can appear in any order. Each child element can 1 time
therefore it must be sequence
rpgfan3233
03-10-2009, 06:39 AM
You could use xs:choice, where xs:choice has @minOccurs set to "0" and @maxOccurs set to "unbounded":
<xs:complexType mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="strip" type="xs:string" minOccurs="0"
maxOccurs="unbounded"/>
<xs:element name="publication" type="xs:string" minOccurs="0"
maxOccurs="unbounded"/>
<xs:element name="movie" type="xs:string" minOccurs="0"
maxOccurs="unbounded"/>
<xs:element name="em" type="xs:string" minOccurs="0"
maxOccurs="unbounded"/>
<xs:element name="a" minOccurs="0" maxOccurs="unbounded">
<xs:complexType mixed="true">
<xs:attribute name="href" use="required"
type="xs:anyURI"/>
</xs:complexType>
</xs:element>
</xs:choice>
<xs:attribute name="poster" type="xs:string"/>
<xs:attribute name="named" type="xs:string" fixed="named"/>
</xs:complexType>
It is logical, right? xs:choice allows for one of its child elements to appear, and you can use @minOccurs and @maxOccurs to your advantage just as I did. :D
JohnBampton
03-10-2009, 07:02 AM
lol, you cannot use choice as it will only ouput one element between 0 and unbounded times.
rpgfan3233
03-10-2009, 07:11 AM
A schema doesn't output anything; it is used to define a specific grammar. You can use the choice element perfectly fine in this situation. I tested against the W3C's own XSV as well as the lxml.etree package for Python. Both had no problem with my instance document (after removing @minOccurs and @maxOccurs to allow the 'c' element to be the root element).
Mr Initial Man
03-10-2009, 07:14 AM
JohnBampton, you do realize you sent me rpgfan's EXACT SUGGESTION in your original reply before you edited it (I get reply notifications emailed to me).
I'd like to say "thank you", since it worked.
Here's the file I tested it on:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../Style/chron_pages.xsl"?>
<!--
To those looking to see how I really did it, guess what: I'm cheating!
-->
<comic
xmlns="Coach_Random"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Coach_Random ../Schemas/cr_chroncomics.xsd"
>
<info>
<name num="1" soloname="Donut Boy" />
<date od="July 2001" sem="0" />
<image fname="donutboy.gif" drawn="May 3, '01" />
</info>
<note>Start of the Coach Random Comic Strip<br />Cavalier Daily Summer 2001 Edition</note>
<comments>
<c poster="JD Kraaikamp" named="named">This is the first <strip>Coach Random</strip> strip ever drawn, and it appeared in a summer edition of the <publication>Cavalier Daily</publication>. 4 or 5 strips wanted to be in the <publication>Cavalier Daily</publication>, and there was an online vote for which 2 would enter. Obviously, <strip>Coach Random</strip> was one of those two.</c>
<c poster="Scott Ruhl" named="named">This was the first in a series of nine strips submitted to the <publication>Cavalier Daily</publication> at the end of my first year at U.Va., and it is truly the first <strip>Coach Random</strip> I ever did. To this day I cannot explain my fascination with T-Tops or 79 Camaro Z-28's.</c>
</comments>
</comic>
JohnBampton
03-10-2009, 07:31 AM
sequence works as well, so I was right twice
Mr Initial Man
03-10-2009, 07:43 AM
Actually, "sequence" does NOT work as well. I did specify that the elements could be used IN ANY ORDER. "sequence" does not allow that.
JohnBampton
03-10-2009, 07:59 AM
lol, sequence min 0 max unbounded works
rpgfan3233
03-10-2009, 08:24 AM
lol, sequence min 0 max unbounded works
I suppose, but only if the element declarations also have @minOccurs and @maxOccurs as they do in the schema originally posted. However, the DTD uses '|', which pretty much means only xs:choice or xs:all is logical. In addition, the '*' quantifier is used, so that rules out xs:all. xs:choice is all that is left, and it only requires a single @minOccurs and a single @maxOccurs on the xs:choice element rather than on each element.
Mr Initial Man
03-10-2009, 08:50 AM
Actually, xs:sequence @minOccurs="0" @maxOccurs="unbounded" DOES work, but only if ALL children have @minOccurs="0" and @maxOccurs="unbounded". xs:choice doesn't require that.
But, JohnBampton, you are not "right twice", since in your edit you said "therefore it must be sequence" (emphasis mine). Choice and sequence both work.