Welcome to WebmasterWorld Guest from 54.167.177.207

Forum Moderators: httpwebwitch

XML schema question

attribute and non-empty element

   
1:15 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



This is the XML fragment I want to validate:
<word syllables="2">arrow</word>

This is my current complexType for it:

<xs:complexType name="wordType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="syllables" type="ns1:lowint" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>

This schema is adequately diligent with the attribute (using a customized "lowint" type which only allows integers from 1 to 7), but I want constraints on the contents of the element as well. For one thing, it may not be empty, and must match a "[\w]+" pattern (alphanumeric, no punctuation or whitespace)

the finished schema should not validate the following invalid XML fragments:

<word syllables="2"></word>
<word syllables="2">1 .23</word>
<word syllables="2">two words</word>

I'm pretty new to schema... I'm fumbling with it. I've tried dozens of combinations to get this to validate

1:29 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The answer is restrictions [w3schools.com]. I've been able to exert some pretty ironclad control using restrictions.
2:08 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



yeah I've tried quite a few combintations of restrictions... it's driving me nuts. can you supply a simplified example?

something that will validate this:

<elem attr="val">notempty</elem>

I'm using Altova XMLspy to make sure all my XML,DTD,XSD etc validates 100%

2:44 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ooh...XMLSpy. We have buck$, eh?

I'll take a gander at it when I get a chance. I'm pretty tied up right now.

Just so I'm sure:

<elem attr="val">notempty</elem>

You want this to fail:

<elem attr="val"></elem>

But not this:

<elem>notempty</elem>
2:55 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



<!-- these are OK: integer attribute value and word character contents -->

<elem val="123">notempty</elem>
<elem val="1">a</elem>
<elem val="2">b</elem>
<elem val="3">c</elem>

<!-- all of the following should fail -->
<elem val="notnumeric">notempty</elem>
<elem val="123">two words</elem>
<elem val="123"> s p a c e </elem>
<elem val="123">Wow!</elem>
<elem val="123">some<b>bold</b>stuff</elem>
<elem val="123"></elem>
<elem>notempty</elem>
<elem badattr="noway"></elem>
<elem badattr=""></elem>
<elem><eek/></elem>
<elem><eek>definitely not</eek></elem>
<elem/>

3:56 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This will give you what you want for the attribute, but I need to spend time hammering out a regex for the restriction. That may take some time.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="wrapper">
<xs:complexType>
<xs:sequence>
<xs:element name="elem">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="val" type="xs:integer" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
4:20 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I'm working on it at the same time. Egads, why is this simple validation so difficult? and so badly documented? cmarshall, I owe you one
4:45 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and so badly documented?

Welcome to the world of XML, where Google returns 30,000 hits, almost every single one of which is the same article off of XML.com or W3Schools, paraphrased by people who barely understand it.

I figured out, REAL QUICK, that the best thing I could do was to chuck my reference book in the trash and get a copy of <oXygen/>.

Can't guarantee an ETA on the regex. I'm pretty swamped right now...

6:16 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



back to using my real example... where the XML looks like this

<word syllables="2">doughnut</word>

This one enforces having stuff in the Content


<xs:simpleType name="wordType">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z]+"/>
</xs:restriction>
</xs:simpleType>

This one enforces the existence and value of the attribute


<xs:complexType name="wordType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="syllables" type="ns1:lowint" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="lowint">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="7"/>
</xs:restriction>
</xs:simpleType>

now... how do I combine them... hmm

6:41 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Haven't had a chance to run this through the tests, but you piqued my interest.

Try this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="smallInt">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="7"/>
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="myString">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z]+"/>
</xs:restriction>
</xs:simpleType>

<xs:element name="wrapper">
<xs:complexType>
<xs:sequence>
<xs:element name="elem">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="myString">
<xs:attribute name="syllables" type="smallInt" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

7:10 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



ha! I just got it moments ago.

<xs:simpleType name="lowint">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="7"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="isword">
<xs:restriction base="xs:string">
<xs:pattern value="[a-z A-Z']+"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="wordType" mixed="true">
<xs:simpleContent>
<xs:extension base="ns1:isword">
<xs:attribute name="syllables" type="ns1:lowint" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>

Give me a moment to compare yours and mine and see if they do the same thing.

7:17 pm on Jul 31, 2007 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



yup. that nailed it.
later I'll blog this one into the b'sphere, but now I'll tag this thread with some searchable keywords for posterity:

validating attribute value and non-empty content CDATA Text on the same XML node element using Schema

thanks cmarshall!

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month