?
Solved

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

Posted on 2007-03-19
8
Medium Priority
?
553 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 12

Accepted Solution

by:
jkmyoung earned 1000 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
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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