ConditionalTypeAssignment-l10nI18n-UsageScenario

From W3C Wiki

Table of Contents:


Usage of Conditional Type Assignment for XML Schema Internationalization and Localization Purposes

Motivation

Schemas may need to be modified to encompass declarations that are only necessary for specific schema usage scenarios:

  1. the adaptation of the schema to a specific language, culture or region, a process called "localization", or
  2. the preparation for specific schema users, again specific to language, culture or region. This process is called "internationalization".

An example of localization is the addition of a 'translate' attribute, which can be used for translation preparation to identify (non) translatable content. An example of internationalization is ruby markup, which is useful e.g. for a Japanese audience (an example is given below).

Here it will be demonstrated how conditional type assignment using xsi:type or other attributes can be used to modify a general schema adequately.

Schema with Localization and Internationalization specific Subtyping

The schema below contains a general type 'textBaseType'. That type is used in three extension subtypes:

  1. The 'l10n' subtype contains attributes to describe translatability of content, to add terminology references or to provide notes for localizers.
  2. The 'i18n-ruby' subtype contains ruby markup used to express additional information about a base text, e.g. pronounciation or translation.
  3. The 'i18n-dir' type encompasses a 'dir' attribute which provides directionality information necessary for mixed script text.

The schema defines among others an element 'span' which uses the type 'textBaseType'.


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:loc="http://example.com/schemalocalization"
    targetNamespace="http://example.com/schemalocalization" attributeFormDefault="unqualified"
    elementFormDefault="qualified">
    <xs:complexType name="textBaseType" mixed="true"/>
    <xs:complexType name="l10n">
        <xs:complexContent>
            <xs:extension base="loc:textBaseType">
                <xs:attribute name="termRef" type="xs:anyURI" use="optional"/>
                <xs:attribute name="translate" use="optional">
                    <xs:simpleType>
                        <xs:restriction base="xs:NMTOKEN">
                            <xs:enumeration value="yes"/>
                            <xs:enumeration value="no"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
                <xs:attribute name="locNote" use="optional"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="i18n-ruby" mixed="true">
        <xs:complexContent>
            <xs:extension base="loc:textBaseType">
                <xs:choice>
                    <xs:element name="ruby">
                        <xs:complexType mixed="false">
                            <xs:sequence>
                                <xs:element name="rb" type="xs:string"/>
                                <xs:element name="rt" type="xs:string"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:choice>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="l10n-dir">
        <xs:complexContent mixed="true">
            <xs:extension base="loc:textBaseType">
                <xs:attribute name="dir">
                    <xs:simpleType>
                        <xs:restriction base="xs:NMTOKEN">
                            <xs:enumeration value="ltr"/>
                            <xs:enumeration value="rtl"/>
                            <xs:enumeration value="lro"/>
                            <xs:enumeration value="rlo"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:element name="text">
        <xs:complexType>
            <xs:sequence minOccurs="1" maxOccurs="unbounded">
                <xs:element name="p">
                    <xs:complexType mixed="true">
                        <xs:sequence minOccurs="0" maxOccurs="unbounded">
                            <xs:element name="span" type="loc:textBaseType"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>


Instance using xsi:type for Conditional Type Assignment

The following instance uses the 'xsi:type attribute' to choose one of the subtypes available for the element 'span'.


<loc:text xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:loc="http://example.com/schemalocalization">
    <loc:p>Normal paragraph</loc:p>
    <loc:p>Paragraph with localization information: <loc:span xsi:type="loc:l10n" translate="no">don't translate me!</loc:span></loc:p>
    <loc:p>Paragraph with ruby information: <loc:span xsi:type="loc:i18n-ruby"><loc:ruby>
            <loc:rb>Keiodaigaku</loc:rb>
            <loc:rt>Keio University</loc:rt>
        </loc:ruby></loc:span></loc:p>
    <loc:p>Paragraph with directionality information for e.g. <loc:span xsi:type="loc:l10n-dir" dir="rtl">ARABIC</loc:span> text.</loc:p>
</loc:text>


Relying on other attributes than xsi:type for conditional type assignment

To choose the appropriate subtype in an instance, other attributes than 'xsi:type' are more useful. 'xml:lang' is one of these. Below is an example of using xml:lang and a 'localize' attribute. The example is based on the Atom Example for xsi:type Alias.


<xs:element name="span" type='xs:TextBaseType'>
   <xs:when test="@localize='yes'" type='loc:l10n'/>
   <xs:when test="@xml:lang='ja'" type='loc:i18n-ruby'/>
   <xs:when test="@xml:lang='ar'" type='loc:i18n-dir'/>
...
</xs:element>


The 'xs:when' elements can be read as: if the condition in the 'test' attribute holds, i.e. the attribute with the described value is given in the instance document, the appropriate type can be choosen.

Wish List

From the localization and internationalization scenario described above, the following wish list for conditional type assignment is derived:

  1. have the ability to choose types in an instance based on other attributes than xsi:type
  2. have the ability to choose types in an instance based on a combination of attributes, e.g. the combination of 'xml:lang' and the 'localize' attribute
  3. have the ability to mix derived types in an instance, e.g. to mix the 'l10n' type with either 'i18n-ruby' or 'i18n-dir'.
  4. take attributes which are inherited into account. E.g. in the case of Ruby, users might set the language to Japanese via "xml:lang='ja'" at the root of a document. It would be useful to have this information inherited and available for type assignment.