XSL Example

In this XSL example, I create two lists in XML and use the XSL style sheet to set the style display the way I want to set it. Also HTML is embedded in the style sheet which allows an actual ordered list to be created.

The XML File

<?xml version="1.0"?>
<!DOCTYPE LANGLIST SYSTEM "langlist.dtd">
<?xml-stylesheet type="text/xsl" href="xmlstyle.xsl"?>
<LANGLIST>
   <TITLE>List of Items Important to Markup Languages</TITLE>
   <TITLE1>Languages</TITLE1>
      <LIST1>
      <LANGUAGES>SGML</LANGUAGES>
      <LANGUAGES>XML</LANGUAGES>
      <LANGUAGES>HTML</LANGUAGES>
      </LIST1>
   <TITLE2>Other Support</TITLE2>
      <LIST2>
      <OTHER>DTD</OTHER>
      <OTHER>DSSSL</OTHER>
      <OTHER>Style Sheets</OTHER>
     </LIST2>
</LANGLIST>

The DTD File

<!ELEMENT LANGLIST ANY>
<!ENTITY % Shape "(rect|circle|poly|default)">
<!ELEMENT TITLE  (#PCDATA)>
<!ELEMENT TITLE1  (#PCDATA)>
<!ELEMENT TITLE2  (#PCDATA)>
<!ELEMENT LIST1 (LANGUAGES)+>
<!ELEMENT LIST2 (OTHER)+>
<!ELEMENT LANGUAGES  (#PCDATA)>
<!ATTLIST LANGUAGES
   type %Shape; #IMPLIED>
<!ELEMENT OTHER  (#PCDATA)>
<!ATTLIST OTHER
   type (disc|square|circle) #IMPLIED>


This DTD file employs an ENTITY, just for demonstration purposes. An entity, in this case is similar to a variable with a value that is set to a string value. In this case the entity name is "Shape" and the string value is "(rect|circle|poly|default)". This value is used to set the attribute list (ATTLIST) for the LANGUAGES and OTHER elements, although these attributes are not used in this example.

The XML Style File

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

	<xsl:template match="/">
		<xsl:apply-templates select="LANGLIST/TITLE" />
		<xsl:apply-templates select="LANGLIST/TITLE1" />
		<xsl:apply-templates select="LANGLIST/LIST1" />
		<xsl:apply-templates select="LANGLIST/TITLE2" />
		<xsl:apply-templates select="LANGLIST/LIST2" />
	</xsl:template>

	<xsl:template match="TITLE">
		<SPAN STYLE="display: 'block'; font-family: 'arial'; color: '#008000'; font-weight: '600'; font-size: '22'; margin-top: '12pt'; text-align: 'center'">
		<xsl:value-of />
		</SPAN>
		<BR/>
	</xsl:template>

	<xsl:template match="TITLE1">
		<SPAN STYLE="display: 'block'; font-family: 'arial'; color: '#000080'; font-weight: '400'; font-size: '20'; margin-top: '12pt'">
		<xsl:value-of />
		</SPAN>
		<BR/>
	</xsl:template>

	<xsl:template match="LIST1">
		<UL style="display: 'list-item'; list-style-image: url('bullet8.gif'); font-family: 'arial'; color: '#000000'; font-weight: '400';  margin-left: '15pt'; margin-top: '12pt'; font-size: '18'">
		<xsl:for-each select="LANGUAGES">
			<LI style="display: 'list-item'; list-style-type: 'square'; list-style-image: url('bullet8.gif'); font-family: 'arial'; color: '#ff0000'; font-weight: '300';  margin-left: '15pt'; margin-top: '12pt'; font-size: '16'">
			<xsl:value-of />
			</LI>
		</xsl:for-each>
		</UL>
	</xsl:template>

	<xsl:template match="TITLE2">
		<SPAN STYLE="display: 'block'; font-family: 'arial'; color: '#000080'; font-weight: '400'; font-size: '20'; margin-top: '12pt'">
		<xsl:value-of />
		</SPAN>
		<BR/>
	</xsl:template>

	<xsl:template match="LIST2">
		<UL style="display: 'list-item'; list-style-image: url('bullet8.gif'); font-family: 'arial'; color: '#000000'; font-weight: '400';  margin-left: '15pt'; margin-top: '12pt'; font-size: '18'">
		<xsl:for-each select="OTHER">
			<LI style="display: 'list-item'; list-style-type: 'square'; list-style-image: url('bullet8.gif'); font-family: 'arial'; color: '#0000ff'; font-weight: '200';  margin-left: '15pt'; margin-top: '12pt'; font-size: '14'">
			<xsl:value-of "."/>
			</LI>
		</xsl:for-each>
		</UL>
	</xsl:template>

</xsl:stylesheet>

In the section above between the <xsl:template match="/"> tag and </xsl:template> tag, the order of the "apply-templates" statements is very important since this determines the order they are displayed in. Also note that the text <xsl:value-of "."/> is used to refer to the XML object currently being processed in order to display the value of that object.

Click Here to see the LANGLIST.XML file (USE your browser's BACK button to return).