Re: XSL and XML schema

Hi Mani,

> Right now i am putting the list of values as an attribute of that
> node separated by | symbol.
>
> <icecream domain="vanilla|chocolate|strawberry">chocolate</icecream>
>
> Then i use xsl substring to parse the attribute value and show it in 
> the select box.
> Can you suggest me a better alternative which avoids
> overhead/cumbersome coding. (How about inline xml schema ?)

What I was thinking was that you had a schema that looked like:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="icecream">
  <xs:simpleType>
    <xs:restriction base="xs:token">
      <xs:enumeration value="vanilla" />
      <xs:enumeration value="chocolate" />
      <xs:enumeration value="strawberry" />
    </xs:restriction>
  </xs:simpleType>
</xs:element>

</xs:schema>

And then an instance like:

<icecream xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="icecream.xsd">
  chocolate
</icecream>

With that, you could get hold of the list with something like:

<xsl:variable name="schema"
  select="document(/*/@xsi:noNamespaceSchemaLocation)/xs:schema" />

<xsl:template match="icecream">
  <xsl:variable name="declaration"
                select="$schema/xs:element[@name = 'icecream']" />
  <select>
    <xsl:for-each select="$declaration/xs:simpleType/xs:restriction
                            /xs:enumeration">
      <option><xsl:value-of select="@value" /></option>
    </xsl:for-each>
  </select>
</xsl:template>

Note, though, that this depends on you knowing what the structure of
the schema looks like. In XML Schema terms, a schema that looks like:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="icecream" type="icecreamType" />

<xs:simpleType name="icecreamType">
  <xs:restriction base="xs:token">
    <xs:enumeration value="vanilla" />
    <xs:enumeration value="chocolate" />
    <xs:enumeration value="strawberry" />
  </xs:restriction>
</xs:simpleType>

</xs:schema>

is basically equivalent, but the XSLT to get hold of the enumerated
values would look quite different. In the general case, the XSLT can
get very complicated - you need to take into account includes and
imports and redefines as well. But if you have control over the XML
Schema it's viable.

Naturally, you could have the schema inline if you wanted - it would
just mean changing the way you set the $schema variable.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Monday, 4 February 2002 09:12:14 UTC