Go Back   Velocity Reviews > Newsgroups > XML
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

XML - Schema help needed

 
Thread Tools Search this Thread
Old 09-12-2006, 11:15 AM   #1
Default Schema help needed


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 xmlnss="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



olivier.scalbert@algosyn.com
  Reply With Quote
Old 09-12-2006, 01:20 PM   #2
George Bina
 
Posts: n/a
Default Re: Schema help needed

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
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

wrote:
> 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 xmlnss="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


  Reply With Quote
Old 09-12-2006, 02:35 PM   #3
olivier.scalbert@algosyn.com
 
Posts: n/a
Default Re: Schema help needed

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 xmlnss="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

  Reply With Quote
Old 09-12-2006, 05:17 PM   #4
George Bina
 
Posts: n/a
Default Re: Schema help needed

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 xmlnss="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 xmlnssi="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
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

wrote:
> 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 xmlnss="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


  Reply With Quote
Old 09-13-2006, 08:29 AM   #5
olivier.scalbert@algosyn.com
 
Posts: n/a
Default Re: Schema help needed

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

George Bina wrote:
> 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 xmlnss="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>
>


  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump