[Date Prev]
| [Thread Prev]
| [Thread Next]
| [Date Next]
--
[Date Index]
| [Thread Index]
A bad idea to use the XML Schema list type?
- From: "Costello, Roger L." <costello@mitre.org>
- To: "xml-dev@lists.xml.org" <xml-dev@lists.xml.org>
- Date: Mon, 9 Feb 2009 10:10:40 -0500
Hi Folks,
Consider these two ways of expressing the numbers drawn in a lottery:
Approach #1: List of Values
<Lottery-Drawing>89 12 41 66 2 26</Lottery-Drawing>
Approach #2: List of Nodes
<Lottery-Drawing-List>
<li>89</li>
<li>12</li>
<li>41</li>
<li>66</li>
<li>2</li>
<li>26</li>
</Lottery-Drawing-List>
Here's how the two approaches may be expressed using XML Schemas:
Approach #1: Use the XML Schema List Type
Constrain the list to six values:
<xs:simpleType name="LotteryNumbers">
<xs:restriction base="NumbersList">
<xs:length value="6"/>
</xs:restriction>
</xs:simpleType>
Constrain the size of each value to 1 - 99:
<xs:simpleType name="OneToNinetyNine">
<xs:restriction base="xs:positiveInteger">
<xs:maxInclusive value="99"/>
</xsd:restriction>
</xs:simpleType>
<xs:simpleType name="NumbersList">
<xs:list itemType="OneToNinetyNine"/>
</xs:simpleType>
Declare the <Lottery-Drawing> element:
<xs:element name="Lottery-Drawing" type="LotteryNumbers" />
Approach #2: Declare Child Elements
Declare the <Lottery-Drawing-List> element to contain six <li> elements:
<xs:element name="Lottery-Drawing-List">
<xs:complexType>
<xs:sequence>
<xs:element name="li" type="OneToNinetyNine" minOccurs="6" maxOccurs="6"/>
</xs:sequence>
</xs:complexType>
</xs:element>
(The type OneToNinetyNine is shown above)
ANALYSIS OF THE TWO APPROACHES
Both approaches are relatively straighforward to express with XML Schemas.
There are, however, significant differences with regard to processing. For example, consider the task of summing all the values in the list. With appproach #2 it is easily accomplished using this XSLT:
sum(li)
With approach #1 it is not easily accomplished. With XSLT 1.0 it requires creating a named template that splits the string "89 12 41 66 2 26" and then sums the individual tokens. Not an easy task. XSLT 2.0 makes the job a bit easier, but it's still not as easy as with approach #2.
From reading Dimitre's paper [1] I see lots of benefits to using approach #2 in terms of enabling functional programming.
RECOMMENDATION
Don't use the XML Schema list type. Instead, create a list of elements.
Do you agree?
/Roger
[1] Higher-Order Functional Programming with XSLT 2.0 and FXSL by Dimitre Novatchev
http://www.idealliance.org/papers/extreme/proceedings/xslfo-pdf/2006/Novatchev01/EML2006Novatchev01.pdf
[Date Prev]
| [Thread Prev]
| [Thread Next]
| [Date Next]
--
[Date Index]
| [Thread Index]