Re: XML schema supports conditional structure???

Hi Eric,


> > Unfortunately you can't do these conditional relationships with the current
> > version of W3C XML Schema but it probably will be supported in the next
> > version.
>
> For the record, there is a workaround using substritution groups,
> though, but it requires adding xsi:type attributes in the instance
> document to "help" W3C XML Schema to choose the right datatype and is
> not as clean as the solution you mention below.

Sure, that's another solution that will work in this case. However, many times you
want conditional relationships that can't be expressed using substitutionGroups so
the Schematron solution is a more general solution. On the other hand, when you
can you substitutionGroups and don't mind the extra work of putting in the correct
value for the xsi:type attribute you might be better of using this method since it
doesn't require the extra validation step with embedded Schematron.

Cheers,
/Eddie

> Eric
>
> > However, in the mean time we have to use other means to achieve
> > these kinds of constraints and one way to do it is to combine Schematron
> > with W3C XML Schema. What you do is to include a Schematron rule in the
> > xs:appinfo element of your XML Schema and for your constraint above it could
> > look like this:
> >
> > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> > elementFormDefault="qualified" attributeFormDefault="unqualified">
> >  <xs:element name="Root" type="BindType"/>
> >  <xs:complexType name="BindType">
> >   <xs:annotation>
> >    <xs:appinfo>
> >     <pattern name="Bridged Option"
> > xmlns="http://www.ascc.net/xml/schematron">
> >      <rule context="*[@type = 'BridgedEthernet']">
> >       <assert test="@BrEthernetIP">If attribute @type = 'BridgedEthernet'
> > the attribute @BrEthernetIP must be present.</assert>
> >      </rule>
> >     </pattern>
> >    </xs:appinfo>
> >   </xs:annotation>
> >   <xs:attribute name="type" use="required">
> >    <xs:simpleType>
> >     <xs:restriction base="xs:NMTOKEN">
> >      <xs:enumeration value="BridgedEthernet"/>
> >      <xs:enumeration value="PPPOE"/>
> >     </xs:restriction>
> >    </xs:simpleType>
> >   </xs:attribute>
> >   <xs:attribute name="BrEthernetIP" use="optional">
> >    <xs:simpleType>
> >     <xs:restriction base="xs:string">
> >      <xs:minLength value="7"/>
> >      <xs:maxLength value="16"/>
> >     </xs:restriction>
> >    </xs:simpleType>
> >   </xs:attribute>
> >  </xs:complexType>
> > </xs:schema>
> >
> > Note that this doesn't really give you what you want since the XPath
> > expression *[@type = 'BridgedEthernet'] will select all element nodes in
> > your XML document that has an attribute type with the value
> > 'BridgedEthernet'. What you really want is to get all elements that are of
> > type 'BindType' AND has an attribute type with the value 'BridgedEthernet'.
> > Unfortunately, you can't select nodes based on XML Schema datatypes in the
> > current version of XPath but I think this is an upcoming feature for the
> > next version. The XPath would then probably look something like
> > *[Datatype='BindType'][@type='BridgedEthernet']
> > The Schematron Validator from Topologi [1] can validate XML Schemas and
> > Schematron as well as provide validation results from embedded Schematron
> > rules like the above example. For more information about Schematron see [2]
> > and more information about how to extend XML Schemas can be found at [3].
> >
> > Cheers,
> > /Eddie
> >
> > [1] www.topologi.com
> > [2] http://www.ascc.net/xml/resource/schematron/
> > [3] http://www.xfront.com/BestPracticesHomepage.html (see Extending XML
> > Schemas)
> >
> >
> >
> >
>
> --
> See you in Scottsdale, Arizona.
>       http://xmlconnections.com/xml/xmlfall2001/speakers.asp#evandervlist
> ------------------------------------------------------------------------
> Eric van der Vlist       http://xmlfr.org            http://dyomedea.com
> http://xsltunit.org      http://4xt.org           http://examplotron.org
> ------------------------------------------------------------------------

Received on Wednesday, 26 September 2001 19:27:17 UTC