Link to home
Start Free TrialLog in
Avatar of den4b
den4bFlag for Ireland

asked on

XML Schema (with a list of requirements and custom date format)

Can someone write an XML Schema document with the following requirement:

* An element of type "note" that contains 4 child elements: "to", "from", "subject", "body";
* All "from", "subject", and "body" are required to occur exactly once;
* "To" must occur one or more times;
* The element "body" may contain arbitrary markup from any XML namespace;
* The elements "to", "subject", and "from" may contain only string data;
* The element "subject" has a required attribute called "date" with the validated format "mm/dd/yyyy" where mm represents month 1-12, dd represents 1-31, and yyyy represents a 4-digit year.

I figured out some of it, but can't get the last point and the arbitrary markup with namespace for body. Would greatly appreciate the whole document, to check stuff that I've written already.

Very urgent!
ASKER CERTIFIED SOLUTION
Avatar of jkmyoung
jkmyoung

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of den4b

ASKER

Thanks for that, it was close to what I had, so I changed mine a bit. Can you tell me if that is a valid schema below, and achieves every point listed in the initial post? Also, I've been searching the net for few hours to find out how to make that date validation. And it seems that everybody goes for patterns. But patterns do not fully achieve the required validation. Is there no other way to get that date validation working? Also, your date pattern is a bit too complicated for my exhausted mind to understand, is it the same as "[0-1][0-9]/[0-3][0-9]/[0-9]{4}". Or is there any advantage of using yours?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified">

    <xs:element name="note" type="km_note"/>

    <xs:complexType name="km_note">
        <xs:sequence minOccurs="1" maxOccurs="1">
            <xs:element name="to" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
            <xs:element name="from" type="xs:string" minOccurs="1" maxOccurs="1"/>
            <xs:element name="subject" type="km_subject" minOccurs="1" maxOccurs="1"/>
            <xs:element name="body" type="km_body" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="km_body" mixed="true">
        <xs:sequence>
            <xs:any namespace="##any"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="km_subject">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="date" type="km_date" use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:simpleType name="km_date">
        <xs:restriction base="xs:string">
            <xs:length value="10"/>
            <xs:pattern value="((0[1-9])|(1[0-2]))/((0[1-9])|([1-2][0-2])|(3[0-1]))/[0-9]{4}"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>
"[0-1][0-9]/[0-3][0-9]/[0-9]{4}" will make 19/38/2006 legally

Also, there is a typo in jkmyoung pattern:  [0-2] should be [0-9] for day pattern.
"((0[1-9])|(1[0-2]))/((0[1-9])|([1-2][0-2])|(3[0-1]))/[0-9]{4}"  Should be
"((0[1-9])|(1[0-2]))/((0[1-9])|([1-2][0-9])|(3[0-1]))/[0-9]{4}"


Avatar of den4b

ASKER

Thanks!!  Can you clarify that the rest is correct, and satisfies all of the points?
The one thing I am not sure is <xs:any namespace="##any"/>.   Please consult with jkmyoung.
Just a quick note, I suggest that you give full points to jkmyoung.  I only point out a typo in his solution.
Avatar of den4b

ASKER

jkmyoung, are you sure about <xs:any namespace="##any"/> ??  (just checking)

owenli27, thanks a lot for checking the schema and pointing out the typo!

Also, can anyone explain how to interpret this date pattern??
"((0[1-9])|(1[0-2]))/((0[1-9])|([1-2][0-2])|(3[0-1]))/[0-9]{4}"
If you explain me "(0[1-9])|(1[0-2])" - I'll get the rest, thanks!
for "(0[1-9])|(1[0-2])" :
'|' mean 'OR'. So, it will be either (0[1-9]) or (1[0-2])

(0[1-9])  will match 01, 02, 03...or 09    
(1[0-2]) will macth 10, 11, or 12

For (0[1-9]) patten, 07 is OK but 7 is illegal. If you want '0' optional in front of 7, you can change patten to:
<!-- xs:length value="10"/ -->
<xs:pattern value="((0?[1-9])|(1[0-2]))/((0?[1-9])|([1-2][0-9])|(3[0-1]))/[0-9]{4}"/>

Avatar of den4b

ASKER

Cool, thanks man!  I wish I could give you few points for it ;-)