XML schema question, possibly related to xs:any

C

Clarence

Hi, here is an XML schema excerpt for an employees details.

<xs:element name="ADDRESS">
<xs:complexType>
<xs:sequence>
<xs:element name="ADDRESSLINE" type="xs:string" maxOccurs="6"/>
<xs:element name="HOUSENUMBER" type="xs:int" minOccurs="1"/>
<xs:element name="POSTCODE" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>


What I want is to write an XML schema to validate my XML against and
guarantee that my XML has an <ADDRESS> somewhere in it. Here is an example
of a schema I have written and that works.


<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="EMPLOYEE">
<xs:complexType>
<xs:sequence>
<--STUFF FROM ADDRESS COPIED ABOVE -->
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


This schema could be used to validate the following XML


<EMPLOYEE>
<ADDRESS>
<ADDRESSLINE>Downing Street</ADDRESSLINE>
<ADDRESSLINE>London</ADDRESSLINE>
<HOUSENUMBER>10</HOUSENUMBER>
<POSTCODE>SW1A 2AA</POSTCODE>
</ADDRESS>
</EMPLOYEE>


Any although it has taken a while to get this far, it isn't what I really
want. What I want is a schema to confirm there is an ADDRESS portion in the
XML, somewhere. For example I want the following XML to be schema valid
using the same XML schema too.


<EMPLOYEE>
<UK>
<ENGLAND>
<ADDRESS>
<ADDRESSLINE>Downing Street</ADDRESSLINE>
<ADDRESSLINE>London</ADDRESSLINE>
<HOUSENUMBER>10</HOUSENUMBER>
<POSTCODE>SW1A 2AA</POSTCODE>
</ADDRESS>
</ENGLAND>
</UK>
<EMPLOYEE>


And also the following


<EMPLOYEE>
<LEVEL1>
<LEVEL2>
<LEVEL3>
<ADDRESS>
<ADDRESSLINE>somewhere</ADDRESSLINE>
<ADDRESSLINE>somwhere</ADDRESSLINE>
<HOUSENUMBER/>
<POSTCODE>something</POSTCODE>
</ADDRESS>
</LEVEL3>
</LEVEL2>
</LEVEL1>
<EMPLOYEE>


The employee address details could be anywhere in the XML, not just the
examples I have given above. I want my XML schema to confirm that the XML
has an <ADDRESS> node SOMEWHERE in the XML. Does anyone have an idea how
this can be done? According to "XML in a nutshell", I think I should be
looking at xs:any but unfortunately don't (currently) have the skills to put
it all together.

I am also thinking that there might be an XPATH expression like

..//ADDRESS[ADDRESSLINE and HOUSENUMBER and POSTCODE]

somewhere in my XML schema but again I am lacking the required knowledge of
where to put it in.

Any help much appreciated.

Thank you
Clarence
 
M

Martin Honnen

Clarence said:
The employee address details could be anywhere in the XML, not just the
examples I have given above. I want my XML schema to confirm that the XML
has an <ADDRESS> node SOMEWHERE in the XML. Does anyone have an idea how
this can be done? According to "XML in a nutshell", I think I should be
looking at xs:any but unfortunately don't (currently) have the skills to put
it all together.

I am also thinking that there might be an XPATH expression like

.//ADDRESS[ADDRESSLINE and HOUSENUMBER and POSTCODE]

somewhere in my XML schema but again I am lacking the required knowledge of
where to put it in.

I don't think you can use a W3C XML schema for what you want to express.
Schematron however
http://xml.ascc.net/resource/schematron/schematron.html
lets you specify such restrictions.
 
C

Clarence

I did not know about "Schematron" Martin. Thank you. I will look at this
later tonight.

For the purposes of the NG and people reading this thread, I note that (from
http://www.oracle.com/technology/tech/xml/index.html) "Schematron" does not
(currently) appear to be a common way of validating XML documents, at least
for people that visit and bother to record their vote on Oracles website.
For example the stats I pulled a few moments ago are:


Which language do you use to validate XML documents?
XML Schema 1435 Votes 65.9%
DTD 500 Votes 23.0%
RelaxNG 37 Votes 1.7%
Schematron 22 Votes 1.0%
Other 23 Votes 1.1%
No comments 160 Votes 7.3%
Total: 2177 votes


Thank you again
Clarence



Martin Honnen said:
The employee address details could be anywhere in the XML, not just the
examples I have given above. I want my XML schema to confirm that the XML
has an <ADDRESS> node SOMEWHERE in the XML. Does anyone have an idea how
this can be done? According to "XML in a nutshell", I think I should be
looking at xs:any but unfortunately don't (currently) have the skills to
put it all together.

I am also thinking that there might be an XPATH expression like

.//ADDRESS[ADDRESSLINE and HOUSENUMBER and POSTCODE]

somewhere in my XML schema but again I am lacking the required knowledge
of where to put it in.

I don't think you can use a W3C XML schema for what you want to express.
Schematron however
http://xml.ascc.net/resource/schematron/schematron.html
lets you specify such restrictions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top