![]() |
|
|
|
#1 |
|
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: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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 xmlns > > <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 |
|
|
|
#3 |
|
Posts: n/a
|
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: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 |
|
|
|
#4 |
|
Posts: n/a
|
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: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 <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 xmlns > > <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 |
|
|
|
#5 |
|
Posts: n/a
|
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 xmlns > > <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> > |
|