Schema help needed

O

olivier.scalbert

Hello,

I need to create an xsd to validate the following type of xml:
<?xml version="1.0" encoding="UTF-8"?>
<root
<exception1 xsi:type="ExceptionAType">
<Type>1</Type>
<Info>Bla bla</Info>
</exception1>

<exception2 xsi:type="ExceptionAType">
<Type>1</Type>
<Info>Bla bla</Info>
</exception2>

<exception3 xsi:type="ExceptionBType">
<Type>2</Type>
<Info>Bla bla</Info>
</exception3>

</root>

My constraints are:
- I have a hierarchy of ExceptionType
- For all ExceptionAType, <Type> must be "1"
- For all ExceptionBType, <Type> must be "2"

With the following schema, I can validate the xml:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:complexType abstract="true" name="ExceptionType">
<xs:sequence>
<xs:element name="Type" type="xs:string"></xs:element>
<xs:element name="Info" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>

<xs:complexType name="ExceptionAType">
<xs:complexContent>
<xs:restriction base="ExceptionType">
<xs:sequence>
<xs:element name="Type" type="xs:string" fixed="1" ></xs:element>
<xs:element name="Info" type="xs:string"></xs:element>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="ExceptionBType">
<xs:complexContent>
<xs:restriction base="ExceptionType">
<xs:sequence>
<xs:element name="Type" type="xs:string" fixed="2"></xs:element>
<xs:element name="Info" type="xs:string"></xs:element>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="RootType">
<xs:sequence>
<xs:element name="exception1" type="ExceptionType"></xs:element>
<xs:element name="exception2" type="ExceptionType"></xs:element>
<xs:element name="exception3" type="ExceptionType"></xs:element>
</xs:sequence>
</xs:complexType>

<xs:element name="root" type="RootType"></xs:element>
</xs:schema>

But I am not happy with that, because, I need to repeat the element
<Info> in each exception definitions.
Is there an other way of doing that, by using extension or other tricks
?

Thanks

Olivier
 
G

George Bina

Hi Olivier,

The content model that results after an extension is a sequence of the
base particles followed by the new added particles so you can work with
extension only if you have the Type as the last element. Have a look at
the IPO example in the schema spec for a sample, look at the Address
and USAddress/UKAddress types.
In your case if the content following the Type element is mode complex
and you do not want to repeat that then you can put that in a group and
make a reference to that group after the Type element.

Best Regards,
George
 
O

olivier.scalbert

Thanks for your help George,

If I use extension, which I prefer because it implements a kind of OO
inheritance, how can I constraint <Type>1</Type> in ExceptionAType and
<Type>2</Type> in ExceptionBType ?


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:complexType abstract="true" name="ExceptionType">
<xs:sequence>
<xs:element name="Type" type="xs:string"></xs:element>
<xs:element name="Info" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>

<xs:complexType name="ExceptionAType">
<xs:complexContent>
<xs:extension base="ExceptionType">
<xs:sequence>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="ExceptionBType">
<xs:complexContent>
<xs:extension base="ExceptionType">
<xs:sequence>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="RootType">
<xs:sequence>
<xs:element name="exception1" type="ExceptionType"/></xs:element>
<xs:element name="exception2" type="ExceptionType"/></xs:element>
<xs:element name="exception3" type="ExceptionType"/></xs:element>
</xs:sequence>
</xs:complexType>

<xs:element name="root" type="RootType"></xs:element>
</xs:schema>


Thanks

Olivier
 
G

George Bina

Hi Olivier,

As I said in my initial reply, you can use extension but in that case
you need to have first Info and then Type, like below:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:complexType abstract="true" name="ExceptionType">
<xs:sequence>
<xs:element name="Info" type="xs:string"></xs:element>
</xs:sequence>
</xs:complexType>

<xs:complexType name="ExceptionAType">
<xs:complexContent>
<xs:extension base="ExceptionType">
<xs:sequence>
<xs:element name="Type" type="xs:string" fixed="1"/>

</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="ExceptionBType">
<xs:complexContent>
<xs:extension base="ExceptionType">
<xs:sequence>
<xs:element name="Type" type="xs:string" fixed="2"/>

</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="RootType">
<xs:sequence>
<xs:element name="exception1" type="ExceptionType"/>
<xs:element name="exception2" type="ExceptionType"/>
<xs:element name="exception3" type="ExceptionType"/>
</xs:sequence>
</xs:complexType>

<xs:element name="root" type="RootType"></xs:element>
</xs:schema>

and the instance document:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<exception1 xsi:type="ExceptionAType">
<Info>Bla bla</Info>
<Type>1</Type>
</exception1>

<exception2 xsi:type="ExceptionAType">
<Info>Bla bla</Info>
<Type>1</Type>
</exception2>

<exception3 xsi:type="ExceptionBType">
<Info>Bla bla</Info>
<Type>2</Type>
</exception3>

</root>

Best Regards,
George
 
O

olivier.scalbert

Thank again George,

What I forget to say is that the xsd will be used to generate java code
for a web service (wsdl2java)
As we need to be able to access the <Type> value polymorphically (?), I
need to put <Type> in the base class ExceptionType.
ExceptionType ex = getException(); // can be any exception subclasses
System.out.println(ex.getType());

And then, I have no idea of how to constraint the <Type> value
depending the subclass ...

Kind regards,

Olivier
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top