Forum Moderators: open

Message Too Old, No Replies

XML Schema: Using Restrictions Properly

Confused by TMI

         

cmarshall

6:43 pm on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As usual in the XML world, I seem to get a deluge of hits when I search for this, and they are all almost identical.

And unhelpful.

I am also afraid that, as seems to happen often, what should be a simple, straightforward and sensible thing [webmasterworld.com] will actually be something that isn't supported in the basic schema, and needs to be hacked to work [webmasterworld.com].

All I want to do is specify restrictions on both attributes and node value.

Simple and straightforward, yes? However, searches give me either attributes or node value.

I have found only one example [datypic.com] of schema with attributes and node value, but only the node value is restricted, and it is very strange. The restriction encompasses the attributes, which doesn't make sense to me.

Here's what I want:

<someXMLEntity attribute1="123">node value</someXMLEntity>

Where the attribute is restricted to numerical value, and there must be some string data in the node value (no empty entities).

As usual, I'll be trying to track this down, and will post anything I find, but I must be missing something. This should be simple, yes?

httpwebwitch

7:45 pm on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



here's an example where you can have any node value, and restrict the attribute via xs:integer.

XML:


<complex att="123">whatever</complex>

Schema:

<xs:element name="complex">
<xs:complexType mixed="true">
<xs:attribute name="att">
<xs:simpleType>
<xs:restriction base="xs:integer">
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>

It will also validate this:


<complex att="123"></complex>

which isn't what you wanted

I haven't figured out how to require some string in the textnode... just hold on a sec

cmarshall

7:57 pm on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks. You have encountered the same problem I have. Lots of examples for restricting attributes. Lots of examples for restricting values.

Zero examples for restricting both.

I'm using our Safari subscription to peruse "XML Schema."

I have never encountered such obscure, obsfucating, prolix vernacular as can be found in every XML tome I've ever read. I need to hire a translator just to tell me what this says. It isn't even "Academese," which I can usually decode.

httpwebwitch

8:16 pm on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, I've found that Schema is utterly useless except for the most idiotic validation... and then only for idiotically simple XML. No amount of excellent documentation is going to make schema useful in the real world, where nodes have both attributes and text values.

perhaps you would enjoy changing your XML to look like this:


<element att="123">
<schemabites>
must be a string in here
</schemabites>
</element>

then you can define restrictions on <schemabites>

cmarshall

8:35 pm on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That will work. I'm trying to avoid doing it though. The real-world application for this is a pretty large-scale build and integration system with a lot of semantic hand-offs between very loosely coupled components spread throughout the world, written in different languages, maintained by vastly different enterprises.

I'm trying to define the hand-offs in as pedantic and simple a manner as possible to reduce the possibility of errors.

I'll probably leave the node value unrestricted, and rely on comments to make it clear what it needs. This is being generated automatically, so the schema is really for the Japanese engineers that need to develop a software "catcher's mitt" for the output.

httpwebwitch

9:23 pm on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



as suggested last week, we could crete a new Schema, a better one...

but it seems like someone is already working on that [schematron.com]

cmarshall

9:46 pm on Feb 5, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is interesting. I'd heard about Schematron [schematron.com], but never knew very much about it. Thanks for the link.

I don't know if I want to rely on using XSLT [w3schools.com] (which has its own standards problems) as the arbiter of my XML [w3schools.com], but this does address some key deficiencies in Schema [w3schools.com].

You are correct, Schema would benefit from improvement, but Schema itself is a big improvement over DTD [w3schools.com]. For that matter, I think XSLT would benefit from some work as well (like making a free and open source version of XSLT 2.0 available, or stop expecting everyone who uses XSLT to have 2.0 on hand).

The problem with these standards is that they are "camels." The saying is that "A camel is a horse that was designed by committee." I used to live in Morocco, and I can say that, in some cases, I'd rather have a camel handy than a horse, but a horse is a more useful critter generally.

We couldn't just decide to sit down and rewrite schema. It would get strangled in the crib, and would be a tremendous amount of work, besides.

DrDoc

5:10 am on Mar 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It actually works the same way for the element and the attribute. A few examples:

<xs:element name="UPC_EAN"> 
<xs:annotation>
<xs:documentation>The UPC or EAN assigned to a specific item.</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:pattern value="[0-9]+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<xs:element name="Status" minOccurs="0"> 
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:enumeration value="Open"/>
<xs:enumeration value="PartiallyShipped"/>
<xs:enumeration value="Complete"/>
<xs:enumeration value="Cancelled"/>
<xs:enumeration value="Hold"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

Or, an example where both an attribute is defined and the textnode itself is restricted:

<xs:element name="POTotal"> 
<xs:annotation>
<xs:documentation>
The total monetary value represented by this PO.
The value for the Currency attribute should be drawn from the ISO 4217 standard.
</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="Currency" type="xs:token" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

[edited by: DrDoc at 5:12 am (utc) on Mar. 18, 2008]