homepage Welcome to WebmasterWorld Guest from 54.198.42.213
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
XML schema question
attribute and non-empty element
httpwebwitch




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

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

 

cmarshall




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

The answer is restrictions [w3schools.com]. I've been able to exert some pretty ironclad control using restrictions.

httpwebwitch




msg:3409520
 2:08 pm on Jul 31, 2007 (gmt 0)

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%

cmarshall




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

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>

httpwebwitch




msg:3409581
 2:55 pm on Jul 31, 2007 (gmt 0)

<!-- 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/>

cmarshall




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

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>

httpwebwitch




msg:3409672
 4:20 pm on Jul 31, 2007 (gmt 0)

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

cmarshall




msg:3409694
 4:45 pm on Jul 31, 2007 (gmt 0)

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...

httpwebwitch




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

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

cmarshall




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

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>


httpwebwitch




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

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.

httpwebwitch




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

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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved