Need help with xml schema

need  XML Schema  with following requirement:
1. An element of type root  that contains 4 child elements: A, B, C,D
2. All "B", "C", and "D" are required to occur exactly once.
3. "A" must occur one or more times
4. The element "D" may contain arbitrary markup from any XML namespace
5. The elements "A","C", and "B" may contain only string data
6. The element "C" 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

This schema should validate a xml with values in Encoding: ISO-8859-1
LVL 7
Maverick_CoolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

abelCommented:
Something like this? I changed the dateformat to the default XSD date format, I assume it is easier to use a validatable dateformat which is guaranteed to contain a legal date. The rest I left the same.

-- Abel --

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
    <xs:element name="root" type="rootType"/>
    <xs:complexType name="rootType">
        <xs:sequence>
            <xs:element maxOccurs="unbounded" minOccurs="0" name="A" type="xs:string"/>
            <xs:element name="B" type="xs:string"/>
            <xs:element name="C" type="Ctype"/>
            <xs:element name="D" type="xs:anyType"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Ctype">
        <xs:attribute name="date" type="xs:date" use="required"/>
    </xs:complexType>
</xs:schema>

Open in new window

ScreenShot313.png
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abelCommented:
There was a tiny mistake. Add xs:string as default type for C element:

<!-- schema.xsd, see below for xml test doc -->
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
    <xs:element name="root" type="rootType"/>
    <xs:complexType name="rootType">
        <xs:sequence>
            <xs:element maxOccurs="unbounded" minOccurs="0" name="A" type="xs:string"/>
            <xs:element name="B" type="xs:string"/>
            <xs:element name="C" type="Ctype"/>
            <xs:element name="D" type="xs:anyType"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Ctype">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="date" type="xs:date" use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:schema>
 
 
 
<!-- input.xml for testing -->
<!-- it now validates this document -->
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="schema.xsd">
    <A>hello A</A>
    <A>hello A</A>
    <A>hello A</A>
    <A>hello A</A>
    <B>hello B</B>
    <C date="2008-12-12">hello C</C>
    <D>
        <hello>
            <other xmlns="urn:other" />
        </hello>
        Any content
    </D>
</root>

Open in new window

0
abelCommented:
(you also said):
This schema should validate a xml with values in Encoding: ISO-8859-1

a schema does not demand the encoding. Basically, XML is encoding-agnostic (a good thing). The encoding is specified in the <?xml ...?> header of any XML file. I have to recommend that you use UTF-8 or UTF-16 encoding, which are the ones that any XML processor has to support minimally. If you have a specific reason to use a much smaller character set like ISO-8859-1, you can do so by using the following line on top of each XML file and by saving the file correctly in that encoding:
<?xml version="1.0" encoding="ISO-8859-1" ?>
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Geert BormansInformation ArchitectCommented:
There are three things I would change to abels schema

1:  for A, you need minOccurs="0"
2:  the use of anyType would still require the types to be defined in the schema, so you need
3:  currently CType does not allow content, so you will need an extension in a simpleContent

From your question, it seems as if you want the A,B,C,D unordered,
basically I would strongly recommend that you keep them ordered as in abels schema, because the unbounded will make an unordered schema very difficult

I agree that you should use a schema date type, because that is the only "pattern" that really validates as a date
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root" type="rootType"/>
    <xs:complexType name="rootType">
        <xs:sequence>
            <xs:element maxOccurs="unbounded" minOccurs="1" name="A" type="xs:string"/>
            <xs:element name="B" type="xs:string"/>
            <xs:element name="C" type="Ctype"/>
            <xs:element name="D">
                <xs:complexType mixed="true">
                    <xs:choice  minOccurs="0" maxOccurs="unbounded">
                        <xs:any processContents="lax"/>
                    </xs:choice>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Ctype">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="date" type="xs:date" use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:schema>

Open in new window

0
Geert BormansInformation ArchitectCommented:
You spotted 3: allready :-)
I was too hasty
0
Geert BormansInformation ArchitectCommented:
Just for the fun of it.
Here is the pattern you wanted for dates
But nothing will stop you from having February 31st in there,
unless you make the pattern really really complex...
as said, I would go for a slightly different syntax, validating as an xs:date
    <xs:complexType name="Ctype">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="date" use="required">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:pattern value="(0?[1-9])|(1[012])/(0?[1-9])|([12][0-9])|(3[01])/[0-9]{4}"></xs:pattern>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

Open in new window

0
Maverick_CoolAuthor Commented:
hey guys for a prompt reply.
but there too scattered answerers here. Can any one give a unified answere.
more over minOccurs for "A" should be 1.
And more things guys go the question throughly you might missed some points like, the pattern should be
mm/dd/yyyy
0
Geert BormansInformation ArchitectCommented:
unified solutiuon with the date pattern as you asked
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root" type="rootType"/>
    <xs:complexType name="rootType">
        <xs:sequence>
            <xs:element maxOccurs="unbounded" minOccurs="1" name="A" type="xs:string"/>
            <xs:element name="B" type="xs:string"/>
            <xs:element name="C" type="Ctype"/>
            <xs:element name="D">
                <xs:complexType mixed="true">
                    <xs:choice  minOccurs="0" maxOccurs="unbounded">
                        <xs:any processContents="lax"/>
                    </xs:choice>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Ctype">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="date" use="required">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:pattern value="(0?[1-9])|(1[012])/(0?[1-9])|([12][0-9])|(3[01])/[0-9]{4}"></xs:pattern>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:schema>

Open in new window

0
abelCommented:
> 1:  for A, you need minOccurs="0"
that was there in the first place, wasn't it?
> You spotted 3: allready :-)

indeed, my first test document did not have content in, it didn't take long to spot ;-)

> 2:  the use of anyType would still require the types to be defined in the schema,

that is the most interesting of your remarks ;-). I was thinking about xs:any, as then you do not need validation (if specified). Can it be that certain validators are more lenient with xs:anyType? I noticed Xerces not reporting any errors with xs:anyType, but others I tested did.

According to http://www.w3.org/TR/xmlschema-1/#ur-type-itself, I'd consider the built-in xs:anyType just equal to the new definition I use below (in particulare the lax validation), but this one validates with all processors:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
    <xs:element name="root" type="rootType"/>
    <xs:complexType name="rootType">
        <xs:sequence>
            <xs:element maxOccurs="unbounded" minOccurs="0" name="A" type="xs:string"/>
            <xs:element name="B" type="xs:string"/>
            <xs:element name="C" type="CType"/>
            <xs:element name="D" type="DType"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="CType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="date" type="xs:date" use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType mixed="true" name="DType">
        <xs:sequence>
            <xs:any maxOccurs="unbounded" minOccurs="0" namespace="##any" processContents="lax"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Open in new window

0
Maverick_CoolAuthor Commented:
can you please explain
            
<
xs:element
 
name
=
"D"
>

                
<
xs:complexType
 
mixed
=
"true"
>

                    
<
xs:choice
  
minOccurs
=
"0"
 
maxOccurs
=
"unbounded"
>

                        
<
xs:any
 
processContents
=
"lax"
/>

                    
</
xs:choice
>

Open in new window

0
Geert BormansInformation ArchitectCommented:
> 1:  for A, you need minOccurs="0"

my mistake, the schema change I made was correct 0 => 1, my comment was wrong,
should read
> 1:  for A, you need minOccurs="1"
0
Maverick_CoolAuthor Commented:
i MEAN
WHAT DOES, THIS DO
<xs:any processContents="lax"/>

0
Maverick_CoolAuthor Commented:
i MEAN
WHAT DOES, THIS DO
<xs:any processContents="lax"/>

CAN EXPLAIN D PART, A LIITLE BIT

0
abelCommented:
ahaahah, yes, you're right, I was a bit to quick with not double checking the requirements. You're right, thanks for pointing that out.

> can you please explain

I'd love to explain it, but what is your question about it? It says in so many words that you can use any element (xs:any + lax + ##any, the latter being the default).

that I wrote the same line a tad different doesn't matter, Gertone's syntax will validate just as well.
0
abelCommented:
that's three times, Maverick. Please don't use caps (don't shout), we will answer your questions or follow up with new questions.

lax means: validate if you can, otherwise don't.
xs:any means: any content allowed.
0
Geert BormansInformation ArchitectCommented:
The D part says

- that the content is a complex type that can be mixed, meaning both text or elements or mixed
- that the potential elements in there can be any... but that you don't validate the content (lax)... only wellformedness is checked
            <xs:element name="D">
                <xs:complexType mixed="true">
                    <xs:choice  minOccurs="0" maxOccurs="unbounded">
                        <xs:any processContents="lax"/>
                    </xs:choice>
                </xs:complexType>
            </xs:element>

Open in new window

0
Geert BormansInformation ArchitectCommented:
@maverick,
you don't have to read this, if you fear it might confuse you more than it adds

@abel

> that is the most interesting of your remarks ;-).
> I was thinking about xs:any, as then you do not need validation (if specified).
> Can it be that certain validators are more lenient with xs:anyType?
> I noticed Xerces not reporting any errors with xs:anyType, but others I tested did.

Well, I always assumed all needed to be declared for xs:anyType, there is no processConten="lax" mode for it in my mind
The recommendation is not explicit about it.
I checked three of the books I have on schema (not really a normative reference) Two only mention the risk of using anyType with substitution groups, the third one (neil bradley, the least normative of the three in my mind) explicitely says all elements used in an anyType need to be declared in the schema or in an imported/included schema. Neil can still be wrong of course.
But, since most validators go from the same assumption, it is safer to not use anyType in this setting... so much for standardisation :)
0
abelCommented:
Since we're trying to be very precise in this thread, I beg to disagree on your latter point on "lax" ;-)

Wellformedness is mandatory for any XML document. Whether processContents is lax, strict or skip.

lax means: if an element is declared or has an xsi:type attribute, then validate
strict means: any elements must be valid and must be declared (or have an xsi:type attr)
skip means: any element is allowed, no validation will be performed, ever.

Resource: page 414 of XML In a Nutshell (or the XSD spec at W3C of course)
0
Geert BormansInformation ArchitectCommented:
of course you are right on your definition of "lax", I was not very precise in that, sorry about that
0
abelCommented:
@maverick: to explain the difference between lax and skip, consider this XML snippet. It will not validate with lax (the C-element is missing the required date-attribute), but it will validate when the D-type would be set to skip.

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="schema.xsd">
    <A>hello A</A>
    <B>hello B</B>
    <C date="2008-12-12">hello C</C>
    <D>
        <C xsi:type="CType"></C>
        <hello>
            <other xmlns="urn:other" />
        </hello>
        Any content
    </D>    
</root>

Open in new window

0
Maverick_CoolAuthor Commented:
thanks guys!!!
0
Geert BormansInformation ArchitectCommented:
welcome
0
abelCommented:
you're welcome, glad to be of help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.