Creating an XML Configuration File
You need to create a configuration file format that can contain all the parameterized information that will be needed at runtime to produce the proper result. Since this file is an integral part of the application and is designed to be updated, it needs to be validated and thus should have a DTD or XML schema. For this application, we will use an XML schema. The following is the XML schema derived from the requirements, which is divided into two parts for ease of discussion.
Defining the XSLT Stylesheets
The configuration XML schema file is called XSLConfig.xsd and initially defines the stylesheets used in your application:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsd:element name="Styles">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Stylesheets" type="Stylesheets"/>
<xsd:element name="Profiles" type="Profiles" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!–– Define Stylesheets Type made up of Books, CDs, and DVDs ––>
<xsd:complexType name="Stylesheets">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="Books">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bookxsl" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="dtd" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="CDs">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="cdxsl" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="dtd" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="DVDs">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="dvdxsl" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="dtd" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
This first section defines the root element to be <Styles> and its child elements to be <Stylesheets> and <Profiles> elements of the corresponding types. It then defines the Stylesheets type to contain <Books>, <CDs>, and <DVDs> elements, which contain their respective <bookxsl>, <cdxsl>, and <dvdxsl> elements. These *xsl elements specify the stylesheets to apply when the corresponding DTDs are retrieved from the DOCTYPE of the input XML document. Note that more than one *xsl element can be specified for each media type and that more media types can be added because of the presence of the maxOccurs="unbounded" attribute on the <xsd:sequence> elements. The following is a conformant and valid Stylesheets instance XML fragment:
<Stylesheets>
<Books>
<bookxsl dtd="bookcatalog.dtd">booklist.xsl</bookxsl>
</Books>
<CDs>
<cdxsl dtd="cdcatalog.dtd">cdlist.xsl</cdxsl>
</CDs>
<DVDs>
<dvdxsl dtd="dvdcatalog.dtd">dvdlist.xsl</dvdxsl>
</DVDs>
</Stylesheets>
The application will retrieve the DOCTYPE DTD declaration and execute the matching XSL stylesheet.
Defining the Profiles
The second section of XSLConfig.xsd defines the profiles for each device’s parameter set as of type Profileset. In this case, we are defining one for PC, PDA, and PHONE.
<xsd:element name="Profileset" type="Profileset"/>
<xsd:complexType name="Profiles">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="PC" type="Profileset"/>
<xsd:element name="PDA" type="Profileset"/>
<xsd:element name="PHONE" type="Profileset"/>
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="Profileset">
<xsd:sequence>
<xsd:element name="CharacterSet" type="CharacterSetType"/>
<xsd:element name="OutputFileType" type="OutputFileTypeType"/>
<xsd:element name="Graphics" type="OnOffType"/>
<xsd:element name="ColorDepth" type="ColorDepthType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="ColorDepthType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="16"/>
<xsd:enumeration value="256"/>
<xsd:enumeration value="64K"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="OnOffType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="ON"/>
<xsd:enumeration value="OFF"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="CharacterSetType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="UTF-8"/>
<xsd:enumeration value="8859-1"/>
<xsd:enumeration value="US-ASCII"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="OutputFileTypeType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="WML"/>
<xsd:enumeration value="HTML"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Note that instead of using <xsd:sequence> to specify the named profiles, as we did with the different media types, we used <xsd:choice>. The distinction is that a valid configuration file must be able to transform all of the specified types; however, it needs to optimize the output only for one or more devices. Obviously, this is an option that you can eliminate by using <xsd:sequence> instead.Also note that the Profileset type is made up of element types that use enumerations to further restrict their content. This means that only those values specified are legal.Finally, there are two other options of note in the second section of XSLConfig.xsd. The first is the definition of OnOffType as a restriction of xsd:string. As we are using “ON” and “OFF” as the values for better semantic understanding, we chose to restrict to a string enumeration. You could also select a Boolean, should you wish to instead have TRUE/FALSE or 1/0 as the values. A similar option holds true for LinesPerPage type; however, in this case we are enumerating over actual numerical values. In the case of XSLT 1.0, these will be implicitly cast anyway.
The following is a conformant and valid Profiles XML instance section from an XSLConfig.xml file:
<Profiles>
<PC>
<CharacterSet>UTF-8</CharacterSet>
<OutputFileType>HTML</OutputFileType>
<Graphics>ON</Graphics>
<ColorDepth>64K</ColorDepth>
</PC>
<PDA>
<CharacterSet>US-ASCII</CharacterSet>
<OutputFileType>HTML</OutputFileType>
<Graphics>ON</Graphics>
<ColorDepth>256</ColorDepth>
</PDA>
<PHONE>
<CharacterSet>8859-1</CharacterSet>
<OutputFileType>WML</OutputFileType>
<Graphics>OFF</Graphics>
<ColorDepth>16</ColorDepth>
</PHONE>
</Profiles>
The application will retrieve these parameters along with the stylesheets using the SAX parser because there is no need to build a DOM of the configuration document, as you are not altering it.The following is an example dvdlist.xml instance document:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE DVDcatalog SYSTEM "dvdcatalog.dtd">
<DVDcatalog>
<dvd>
<ISBN>2222</ISBN>
<title>Titanic</title>
<director_lastname>Cameron</director_lastname>
<studio>MGM</studio>
<year>2003</year>
<graphics>
<color64K URL="http:/pictures.foo.com/64K/2222.jpg">
Don Quixote Cover</color64K>
<color256 URL="http:/pictures.foo.com/256/2222.jpg">
Don Quixote Cover</color256>
<color16 URL="http:/pictures.foo.com/16/2222.jpg">
Don Quixote Cover</color16>
</graphics>
<price>9.99</price>
</dvd>
</DVDcatalog>