[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Using <choice> to enforce rules
Hi Sean, > I try to use W3C schema to enforce a business rule so that only one of the > two attributes can have blank value. > > My schema structure is one attribute group with two attributes which > requires value, one attribute group with two attributes which only first > attribute requires value and one attribute group with same two attributes > which only second attribute requires value. Then I have two groups (element > groups), first group have the first and second attribute groups and second > group have first and third attribute group. Last I create the element which > uses a <choice> with reference to the two element groups. The schema and an > instance document are below. > > I created it in XML Spy 4 and it works as expected. But when I use Java > with Xerces 1.4 deployed on Weblogic, it always validates against the first > item in the <choice>. > Can anyone see any problems in my schema design? Yes. I think XML Spy is wrong here because you the model you have created is ambigous. The parser won't know which of the elementA elements it's parsing. I think this also violates the constraint the in the same model group you can't have two element declarations with the same name but different types. Unfortunately W3C XML Schema isn't very good at defining co-occurence constraints like this but RELAX-NG and Schematron can do this or you can choose to embedd some Schematron rules within your XML Schema to enforce the extra constraints. See [1] for an example of how a Schematron rule can be embedded within a W3C XML Schema. Cheers, /Eddie [1] http://lists.w3.org/Archives/Public/xmlschema-dev/2001Nov/0049.html > > > Thanks a lot, > > Sean > > <?xml version="1.0" encoding="UTF-8"?> > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" > elementFormDefault="qualified" attributeFormDefault="unqualified"> > <xs:attributeGroup name="AttGroupA"> > <xs:attribute name="AA"> > <xs:simpleType> > <xs:restriction base="xs:string"> > <xs:minLength value="1"/> > </xs:restriction> > </xs:simpleType> > </xs:attribute> > <xs:attribute name="BB"> > <xs:simpleType> > <xs:restriction base="xs:string"> > <xs:minLength value="1"/> > </xs:restriction> > </xs:simpleType> > </xs:attribute> > </xs:attributeGroup> > <xs:attributeGroup name="AttGroupB"> > <xs:attribute name="CC"> > <xs:simpleType name=""> > <xs:restriction base="xs:string"> > <xs:maxLength value="40"/> > </xs:restriction> > </xs:simpleType> > </xs:attribute> > <xs:attribute name="DD"> > <xs:simpleType name=""> > <xs:restriction base="xs:string"> > <xs:minLength value="1"/> > <xs:maxLength value="40"/> > </xs:restriction> > </xs:simpleType> > </xs:attribute> > </xs:attributeGroup> > <xs:attributeGroup name="AttGroupC"> > <xs:attribute name="CC"> > <xs:simpleType name=""> > <xs:restriction base="xs:string"> > <xs:minLength value="1"/> > <xs:maxLength value="40"/> > </xs:restriction> > </xs:simpleType> > </xs:attribute> > <xs:attribute name="DD"> > <xs:simpleType name=""> > <xs:restriction base="xs:string"> > > <xs:maxLength value="40"/> > </xs:restriction> > </xs:simpleType> > </xs:attribute> > </xs:attributeGroup> > <xs:group name="ElementGroupA"> > <xs:sequence> > <xs:element name="ElementA"> > <xs:complexType name=""> > > <xs:attributeGroup ref="AttGroupA"/> > <xs:attributeGroup ref="AttGroupB"/> > </xs:complexType> > </xs:element> > </xs:sequence> > </xs:group> > <xs:group name="ElementGroupB"> > <xs:sequence> > <xs:element name="ElementA"> > <xs:complexType name=""> > > <xs:attributeGroup ref="AttGroupA"/> > <xs:attributeGroup ref="AttGroupC"/> > </xs:complexType> > </xs:element> > </xs:sequence> > </xs:group> > <xs:element name="TEST"> > <xs:complexType> > <xs:choice> > <xs:group ref="ElementGroupA"/> > <xs:group ref="ElementGroupB"/> > </xs:choice> > </xs:complexType> > </xs:element> > </xs:schema> > > <?xml version="1.0" encoding="UTF-8"?> > <TEST xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xsi:noNamespaceSchemaLocation="D:\Test.xsd"> > <ElementA AA="A" BB="B" CC="" DD=""/> > </TEST> > > ----------------------------------------------------------------- > The xml-dev list is sponsored by XML.org <http://www.xml.org>, an > initiative of OASIS <http://www.oasis-open.org> > > The list archives are at http://lists.xml.org/archives/xml-dev/ > > To subscribe or unsubscribe from this list use the subscription > manager: <http://lists.xml.org/ob/adm.pl>
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|