Jump to content
Sign in to follow this  
Guest eleven

Help needed to spot schema error

Recommended Posts

Guest eleven

Hi all,I am a newbie to XML and have been trying to learn how to use schema to validate my entries. Can anyone try to help me spot what is wrong with my XSD because it keeps having error? I am not sure if I have used it correctly. Thank you for your help.My XML Doc<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="vehicles.xsl" ?><vehicle> <car> <reg>1FTN-707</reg> <date>2006-07-28</date> <start>10001</start> <end>10010</end> <id>101786J</id> <purpose>demo</purpose> <fuel>20.5</fuel> <cost>31</cost> </car> <car> <reg>1FTN-707</reg> <date>2006-07-28</date> <start>10010</start> <end>10015</end> <id>101786J</id> <purpose>demo</purpose> </car> <car> <reg>1FTN-707</reg> <date>2006-07-27</date> <start>9100</start> <end>10001</end> <id>101786J</id> <purpose>seminar</purpose> <fuel>95</fuel> <cost>133</cost> </car> <car> <reg>1CBL-099</reg> <date>2006-07-02</date> <start>1001</start> <end>1050</end> <id>195767B</id> <purpose>to conference</purpose> </car> <car> <reg>1CBL-099</reg> <date>2006-07-02</date> <start>1050</start> <end>1100</end> <id>195767B</id> <purpose>from conference</purpose> </car> <car> <reg>1CBL-099</reg> <date>2006-07-03</date> <start>1100</start> <end>1500</end> <id>195767B</id> <purpose>Customer tour</purpose> <fuel>80</fuel> <cost>120</cost> </car> <car> <reg>1ADA-095</reg> <date>2006-07-10</date> <start>27675</start> <end>27700</end> <id>234100K</id> <purpose>private</purpose> </car> <car> <reg>1ADA-095</reg> <date>2006-07-11</date> <start>27700</start> <end>27710</end> <id>234100K</id> <purpose>private</purpose> </car> <car> <reg>1ADA-095</reg> <date>2006-07-11</date> <start>27710</start> <end>27730</end> <id>234100K</id> <purpose>private</purpose> <fuel>50</fuel> <cost>70.5</cost> </car> <car> <reg>1CPP-101</reg> <date>2006-07-21</date> <start>30054</start> <end>30090</end> <id>101001A</id> <purpose>Service call</purpose> <fuel>25.5</fuel> <cost>32.3</cost> </car> <car> <reg>1BSC-303</reg> <date>2006-07-09</date> <start>45171</start> <end>45221</end> <id>100101B</id> <purpose>Test drive</purpose> </car> <car> <reg>1BSC-303</reg> <date>2006-07-10</date> <start>45221</start> <end>45250</end> <id>100101B</id> <purpose>Car service</purpose> </car> <car> <reg>1BSC-303</reg> <date>2006-07-11</date> <start>45250</start> <end>45300</end> <id>100101B</id> <purpose>private</purpose> </car> <car> <reg>1BSC-303</reg> <date>2006-07-11</date> <start>45300</start> <end>45375</end> <id>100101B</id> <purpose>demo</purpose> <fuel>60</fuel> <cost>75.5</cost> </car> <car> <reg>1BSC-303</reg> <date>2006-07-11</date> <start>45375</start> <end>45420</end> <id>100101B</id> <purpose>Service call</purpose> </car></vehicle>My XSD<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"><xs:element name="vehicle"><xs:complexType><xs:sequence><xs:element ref="car" maxOccurs="unbounded" /></xs:sequence></xs:complexType></xs:element><xs:element name="car"><xs:complexType><xs:sequence><xs:element ref="reg"/><xs:element ref="date"/><xs:element ref="start"/><xs:element ref="end"/><xs:element ref="id"/><xs:element ref="purpose"/><xs:element ref="fuel" minOccurs="0"/><xs:element ref="cost" minOccurs="0"/></xs:sequence></xs:complexType></xs:element><xs:element name="reg"><xs:simpleType><xs:restriction base="xs:string"><xs:pattern value="1[a-z][a-z][a-z]-[0-9][0-9][0-9]"/></xs:restriction></xs:simpleType></xs:element><xs:element name="date" type="xs:gYear"/><xs:element name="start" type="xs:integer"/><xs:element name="end" type="xs:integer" /><xs:element name="id"><xs:simpleType><xs:restriction base="xs:string"><xs:pattern value="[0-9][0-9][0-9][0-9][0-9][0-9][a-z]" /></xs:restriction></xs:simpleType></xs:element><xs:element name="purpose" type="xs:string"/><xs:element name="fuel" type="xs:decimal" /><xs:element name="cost" type="xs:decimal" /></xs:schema>

Share this post


Link to post
Share on other sites

Regular expressions are case sencetive. So [a-z] is different then [A-Z]. If you want to allow both lower case and upper case letters, you need to use [A-Za-z].Also, your <date> element contains a whole date, not only years, so you need to use xs:date for it instead of xs:gYear.Last, but not least. Try to shorten repeating patterns. Instead of writing [0-9][0-9][0-9][0-9][0-9][0-9], you could write [0-9]{6,6}. The first digit in the parenthesis means the minimum amount of times the previous character (in this case: a number from 0 to 9) must appear, and the second is the maxumum number of times. You may omit the comma and/or any of the numbers, but that doesn't work with all validators. The above way works in every validator that supports parenthesis.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...