Solved

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

Posted on 2007-03-19
8
519 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Curl Multi-exec 13 67
Problem to DocumentFormat 8 74
MS Access how to nest many values to one value when exporting query to XML format 8 44
Help with Syntax 9 25
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

914 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

18 Experts available now in Live!

Get 1:1 Help Now