Solved

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

Posted on 2007-03-19
8
504 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
 
LVL 6

Author Comment

by:den4b
ID: 18757768
Thanks!!  Can you clarify that the rest is correct, and satisfies all of the points?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now