Solved

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

Posted on 2007-03-19
8
535 Views
Last Modified: 2009-12-16
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!
0
Comment
Question by:den4b
  • 4
  • 3
8 Comments
 
LVL 12

Accepted Solution

by:
jkmyoung earned 250 total points
ID: 18749781
<?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">
            <xs:complexType>
                  <xs:sequence>
                        <xs:element name="to" maxOccurs="unbounded">
                              <xs:simpleType>
                                    <xs:restriction base="xs:string"/>
                              </xs:simpleType>
                        </xs:element>
                        <xs:element name="from">
                              <xs:simpleType>
                                    <xs:restriction base="xs:string"/>
                              </xs:simpleType>
                        </xs:element>
                        <xs:element name="subject">
                              <xs:complexType>
                                    <xs:simpleContent>
                                          <xs:extension base="xs:string">
                                                <xs:attribute name="date" type="km_date" use="required"/>
                                          </xs:extension>
                                    </xs:simpleContent>
                              </xs:complexType>
                        </xs:element>
                        <xs:element name="body">
                              <xs:complexType mixed="true">
                                    <xs:sequence minOccurs="0" maxOccurs="unbounded">
                                          <xs:any namespace="##any"/>
                                    </xs:sequence>
                              </xs:complexType>
                        </xs:element>
                  </xs:sequence>
            </xs:complexType>
      </xs:element>
      <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>

I think this should cover what you're looking for.
Seperated the date format check into a seperate type, arbitrarily named 'km_date'
Note the pattern used for the date validation.
0
 
LVL 6

Author Comment

by:den4b
ID: 18750257
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
 
LVL 9

Expert Comment

by:owenli27
ID: 18751085
"[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}"


0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 6

Author Comment

by:den4b
ID: 18757768
Thanks!!  Can you clarify that the rest is correct, and satisfies all of the points?
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18760544
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.
0
 
LVL 6

Author Comment

by:den4b
ID: 18760874
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!
0
 
LVL 9

Expert Comment

by:owenli27
ID: 18761075
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}"/>

0
 
LVL 6

Author Comment

by:den4b
ID: 18763036
Cool, thanks man!  I wish I could give you few points for it ;-)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question