Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Patterns in XML Schema - how to do it?

 
Kevin Thompson
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My examples don't work as expected. Why doesn't my pattern work? This is an example from page 49 of the xfront tutorial (PowerPoint #1) which I have edited slightly for readability. Why doesn't my pattern spit out an error for the Testing field(with the TestType pattern) which is supposed to be only 5 positions long with a dash after the first 4?
Here is the content of JavaRanch.xml ==>
<?xml version="1.0"?>
<BookStore xmlns="http://www.books.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.books.org JavaRanch.xsd">
<Book>
<Title>Get this stuff figured out!</Title>
<Author>John Doe</Author>
<Date>1998</Date>
<Testing>BLAHBLAH9999</Testing>
<Publisher>McMillin Publishing</Publisher>
</Book>
</BookStore>
Here is the content of JavaRanch.xsd ==>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.books.org"
xmlns="http://www.books.org"
elementFormDefault="qualified">

<xsd:simpleType name="TestType">
<xsd:restriction base="xsd:string">
<xsd:length value = "5"/>
<xsd:pattern value="\d{4}-\d{1}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="BookStore">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Book" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Title" type="xsd:string"/>
<xsd:element name="Author" type="xsd:string"/>
<xsd:element name="Date" type="xsd:gYear"/>
<xsd:element name="Testing" type="TestType"/>
<xsd:element name="Publisher" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
[ December 06, 2002: Message edited by: Kevin Thompson ]
 
Niraj Sheth
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin,
I tried your example with Xerces. It gave following error message.
[Error] test.xml:10:32: cvc-pattern-valid: Value 'BLAHBLAH9999' is not facet-valid with respect to pattern '[0-9?-??-??-??-??-??-??-??-??-??-??-??-??-??-?]{4}-[0-9?-??-??-??-??-??-??-??-??-??-??-??-??-??-?]{1}'.
[Error] test.xml:10:32: cvc-type.3.1.3: The value 'BLAHBLAH9999' of element 'Testing' is not valid.

May be you are using wrong parser.
 
Kevin Thompson
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naraj - thank you!
But this is what drives me insane. I thought when displaying the file JavaRanch.xml in a browser (IE6) that it was supposed to give me an error message.
I did not know I was supposed to be running a Java program against it to determine if it had an error.
Is this type of thing obvious to other people? Just when I think I have a clear understanding of XML, something like this happens and I wonder where things went really really wrong.
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too noticed lot of such pitfalls while using xsv as my schema validator.
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please search for 'IE validate' using the search link up on the page.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic