File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Patterns in XML Schema - how to do it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Patterns in XML Schema - how to do it?" Watch "Patterns in XML Schema - how to do it?" New topic

Patterns in XML Schema - how to do it?

Kevin Thompson
Ranch Hand

Joined: May 04, 2001
Posts: 237
My examples don't work as expected. Why doesn't my pattern work? This is an example from page 49 of the tutorial on XML Schema (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=""
xsi:schemaLocation=" JavaRanch.xsd">
<Title>Get this stuff figured out!</Title>
<Author>John Doe</Author>
<Publisher>McMillin Publishing</Publisher>
Here is the content of JavaRanch.xsd ==>
<?xml version="1.0"?>
<xsd:schema xmlns:xsd=""
<xsd:simpleType name="TestType">
<xsd:restriction base="xsd:string">
<xsd:length value = "5"/>
<xsd:pattern value="\d{4}-\d{1}"/>
<xsd:element name="BookStore">
<xsd:element name="Book" maxOccurs="unbounded">
<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"/>
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Kevin,
In case you've not done it already, your parser must be validating to generate these errors (you set the Validating property on the DocumentBuilder factory).
Another thing: the length says 5 where the pattern implies it's 6: 4 + 1 (for the dash) + 1
I agree. Here's the link:
subject: Patterns in XML Schema - how to do it?
It's not a secret anymore!