W3C home > Mailing lists > Public > xmlschema-dev@w3.org > May 2007

Re: Defining recursive elements?

From: Todd Moon <tmrfcm@gmail.com>
Date: Thu, 17 May 2007 15:08:23 -0400
Message-ID: <2dc6e4c80705171208u3ebd29f8m8341a931d9bcca94@mail.gmail.com>
To: xmlschema-dev@w3.org

(Forgive me if this is a duplicate, Gmail hiccuped for 5 minutes after
I hit send. I don't see my message in the Sent folder though)

I've been fleshing out my schema. I renamed "part" to "component", and
added some unique and pattern validation. ("type" and "serial"
together must be unique across all components.)

I would appreciate some criticism of my schema's overall construction.
Here it is:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="header"/>
  <xsd:element name="components">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="component" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:unique name="componentTypeAndSerial">
      <xsd:selector xpath=".//component"/>
      <xsd:field xpath="@type"/>
      <xsd:field xpath="@serial"/>
    </xsd:unique>
  </xsd:element>
  <xsd:element name="component">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="component" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
      <xsd:attribute ref="type"/>
      <xsd:attribute ref="serial"/>
    </xsd:complexType>
  </xsd:element>
  <xsd:attribute name="serial">
    <xsd:simpleType>
      <xsd:restriction base="xsd:string">
        <xsd:pattern value="\d{10}"/>
      </xsd:restriction>
    </xsd:simpleType>
  </xsd:attribute>
  <xsd:attribute name="type">
    <xsd:simpleType>
      <xsd:restriction base="xsd:string">
        <xsd:pattern value="\d{3}"/>
      </xsd:restriction>
    </xsd:simpleType>
  </xsd:attribute>
  <xsd:element name="adapter">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="header"/>
        <xsd:element ref="components"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Here is an example of a valid document:

<adapter>
  <header>
  </header>
  <components>
    <component type="735" serial="0000000001">
      <component type="736" serial="0000000001"/>
      <component type="736" serial="0000000002"/>
      <component type="740" serial="0000000003"/>
    </component>
    <component type="735" serial="0000000002">
    </component>
    <component type="735" serial="0000000003"/>
  </components>
</adapter>

I may have gone a little overboard with the "Salami Slice" design.
Would it be better (ie: easier to understand) if I made a hybrid of
"Salami Slice" and "Russian Doll"? It seems overkill to define the
attributes like I did just to be referenced only once. The only node
that really needs to be referenced is "component" to support the
recursion.

In some ways I like the usage of the salami slice style. If you are
visually inspecting the schema of "component" you can see that it has
the "type" and "serial" attributes, but you may not care about any
validation I have added them. That validation won't get in the way of
your understanding of the structure of "component". That being said,
trying to get an overall picture of what a valid document would look
like is more difficult because you have to search for the schemas of
every element and attribute separately.

Any thoughts?

- Todd

On 5/17/07, Pete Cordell <petexmldev@tech-know-ware.com> wrote:
> ----- Original Message From: "Andrew Welch" <...>
> To: "Michael Kay" <...>
> Cc: "Todd Moon" <...>; <...>
>
>
> >
> > On 5/17/07, Michael Kay <mike@saxonica.com> wrote:
> >> > Well it's a matter of taste, but if you use the venetian
> >> > blind style of schema then you wouldn't use element ref=""
> >> > much, but @type instead, eg:
> >> >
> >> > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
> >> >
> >> >   <xs:element name="part" type="part"/>
> >> >
> >> >   <xs:complexType name="part">
> >> >     <xs:sequence>
> >> >       <xs:element name="part" type="part" minOccurs="0"
> >> > maxOccurs="unbounded"/>
> >> >     </xs:sequence>
> >> >     <xs:attribute name="serial" type="xs:string"/>
> >> >   </xs:complexType>
> >> >
> >>
> >> I'm not sure it's just a matter of taste. It feels wrong to me to have
> >> two
> >> element declarations for element part, one global and one local, when all
> >> the part elements are the same. It gives the same answers as far as
> >> validation of instances is concerned, but it seems a messier component
> >> model, which could give you a messier translation into classes when you
> >> do
> >> data binding, for example (I don't know if that's actually the case).
>
>
> It may also depend on whether the global element 'part' is the only element
> to have type 'part'.  And also depends on whether you might want to add
> another element of type 'part' later (although it's not impossible to tweak
> that when the need arises!).
>
> Pete.
> --
> =============================================
> Pete Cordell
> Tech-Know-Ware Ltd
> for XML Schema to C++ data binding visit
>  http://www.tech-know-ware.com/lmx/
>  http://www.codalogic.com/lmx/
> =============================================
>
>
>
Received on Thursday, 17 May 2007 19:08:29 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:14:59 GMT