OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Datatypes in Schematron (was Re: Namespaces,W3C XML Schema (was Re: ANN: SAX Filters forNamespaceProcessing))



> >Elliotte Rusty Harold wrote:
 
> On the other hand, over the last three years as I've taught developers about DTDs, almost invariably the first question is "How do I say that an element contains an int?" and the second question is usually ""How do I say that an element contains a year since 1969?" or some variant thereof. 


In Schematron (untested), I think it would be something like this:

<schema xmlns="http://www.ascc.net/xml/schematron">
    <title>For Rusty's Inquiring Students</title>
    <p>This is an example of creating a datatype using Schematron's "abstract rules"
     and using them. </p>

    <pattern>
        
        <!-- Define an abstract rule on a data value (i.e. a type) for int  -->
        <rule abstract="true" id="int">
            <assert test="string-length(string-normalize(./text())) &gt; 0"
                >This element must contain a value.</assert>
            <assert test="number(./text())"
                >This element should contain an int. An int it a number.</assert>
            <assert test="floor(number(./text())) = ceiling(number(./text())) "
                >This element should contain an int. An int is a whole number.</assert>
            <assert test="number(./text()) &lt; 2147483647"
                >This element should contain an int. An int is less than 2^31-1.</assert>
            <assert test="number(./text()) &gt;= -2147483648"
                >This element should contain an int. An int is greater than -(2^31).</assert>
        </rule>

        <!-- Make the assertions -->
        <rule context="someElement" >
            <extends rule="int" />
        </rule>
        <rule context="someOtherElement">
            <extends rule="int" />
            <assert test="number(./text()) &gt; 1969"
                >This element should contain a year since 1969</assert>
        </rule>
   <pattern>
</schema>

Most other schema languages have built-in types. I guess that since people will
tend to evaluate schema languages using a check-box, they might put
"no datatyping" on Schematron, when really they mean no "built-in"
data types (apart from the XPath ones: number, string, boolean).  

With Schematron's approach, you can restrict a datatype
merely by providing extra assertions before or after the <extend>
element.  (You can over-ride datatype checking by catching the 
text in a previous rule: this is not type extension by allowing
type exceptions, I guess.) 

Cheers
Rick Jelliffe