Rendering XML Documents Using XSL
by Sean McGrath

Example 1:

C>msxsl -i cfs.xml -s cfs4.xsl -o cfs4.htm
Error in style sheet 'cfs4.xsl'
ParseException: Expecting name instead of '1'
Location: file:///C:/DOBBS/cfs4.xsl (9,17)
Context: <xsl><rule><HTML><BODY><TABLE>

Figure 2:

C>type cars.xml
<?xml version = "1.0"?>
<CarsForSale>
        <Car Price = "10000" Units = "Dollars">
 <Maker>Toyota</Maker>
 <Condition Type = "Good"/>
 <Color>Red</Color>
 </Car>
 <Car Price = "20000" Units = "Irish Punts">
 <Maker>Ford</Maker>
 <Condition Type = "Good"/>
 <Color>White</Color>
 </Car>
</CarsForSale>

Listing One
(a)
C>type cfs1.xsl
<!-- Ultra simple XSL stylesheet -->
<xsl>
 <rule>
 <!-- Pattern -->
 <root/>
 <!-- Action -->
 <HTML>
 <HEAD>
 <TITLE>Cars for sale - Example 1</TITLE>
 </HEAD>
 <BODY>
 <children/>
 </BODY>
 </HTML>
 </rule>
</xsl>

(b)
C>msxsl -i cfs.xml -s cfs1.xsl -o cfs1.htm
C>type cfs1.htm
<HTML>
<HEAD>
<TITLE>Cars for sale - Example 1</TITLE>
</HEAD>
<BODY>
ToyotaRedFordWhite
</BODY>
</HTML>

Listing Two 
(a)
<!-- Process Car elements by processing all children and then adding 
a horizontal rule -->
<rule>
 <!-- Pattern -->
 <target-element type = "Car"/>
 <!-- Action -->
 <children/>
 <HR/>
</rule>
<!-- Process Maker elements by prefixing some literal text and then
processing all children -->
<rule>
 <!-- Pattern -->
 <target-element type = "Maker"/>
 <!-- Action -->
 <P>
 Make of Car: <children/>
 </P>
</rule>
<!-- Process both Condition and Color elements in the same way--simply 
create HTML paragraphs -->
<rule>
 <!-- Pattern -->
 <target-element type = "Condition"/>
 <target-element type = "Color"/>
 <!-- Action -->
 <P>
 <children/>
 </P>
</rule>

(b)
C>msxsl -i cfs.xml -s cfs2.xsl -o cfs2.htm
C>type cfs2.htm
<HTML>
<HEAD>
<TITLE>Cars for sale - Example 2</TITLE>
</HEAD>
<BODY>
<P> Make of Car: Toyota
</P><P>
</P><P>
Red
</P><HR><P> Make of Car: Ford
</P><P>
</P><P>
White
</P><HR>
</BODY>
</HTML>

Listing Three
(a)
<rule>
 <target-element type = "Car"/>
 <P>
 Price = <eval><![CDATA[
 getAttribute("Price") + " " + getAttribute("Units")
 ]]></eval>
 </P>
 <children/>
 <HR/>
</rule>

(b)
C>msxsl -i cfs.xml -s cfs3.xsl -o cfs3.htm
C>type cfs3.htm
<HTML>
<HEAD>
<TITLE>Cars for sale - Example 3</TITLE>
</HEAD>
<BODY>
<P> Price = 10000 Dollars
</P><P> Make of Car: Toyota
</P><P>
</P><P>
Red
</P><HR><P> Price = 20000 Irish Punts
</P><P> Make of Car: Ford
</P><P>
</P><P>
White
</P><HR>
</BODY>
</HTML>

Listing Four
(a)
C>type cfs4.xsl
<xsl>
 <rule>
    <!-- Pattern -->
 <root/>
 <!-- Action -->
 <HTML>
 <HEAD>
 <TITLE>Cars for sale - Example 4</TITLE>
 </HEAD>
 <BODY>
 <TABLE BORDER="1">
 <TR>
 <TD>Number</TD>
 <TD>Price</TD>
 <TD>Maker</TD>
 <TD>Condition</TD>
 <TD>Color</TD>
 </TR>
 <children/>
 </TABLE>
 </BODY>
 </HTML>
 </rule>
<rule>
<!-- Pattern -->
 <target-element type = "Car"/>
 <!-- Action -->
 <TR>
 <!-- Automatically number the table rows -->
 <TD><eval>childNumber(this)</eval></TD>
 <TD>
 <eval>getAttribute("Price") + " " + getAttribute("Units")</eval>
 </TD>
 <children/>
 </TR>
</rule>
<rule>
 <!-- Pattern -->
 <target-element type = "Maker"/>
 <target-element type = "Color"/>
 <!-- Action -->
 <TD>
 <children/>
 </TD>
</rule>
<rule>
 <!-- Pattern -->
 <target-element type = "Condition"/>
 <TD>
 <eval><![CDATA[
 getAttribute("Type")
          ]]></eval>
 </TD>
</rule>
</xsl>

(b)
C>msxsl -i cfs.xml -s cfs4.xsl -o cfs4.htm
C>type cfs4.htm
<HTML>
<HEAD>
<TITLE>Cars for sale - Example 4</TITLE>
</HEAD>
<BODY>
<TABLE BORDER="1">
<TR>
<TD>Number</TD><TD>Price</TD><TD>Maker</TD><TD>Condition</TD>
                                                        <TD>Color</TD></TR>
<TR><TD>1</TD><TD>10000 Dollars</TD><TD>Toyota</TD><TD>Good</TD><TD>Red</TD>
</TR>
<TR><TD>2</TD><TD>20000 Irish Punts</TD><TD>Ford</TD><TD>Good</TD><TD>
White</TD></TR>
</TABLE>
</BODY>
</HTML>

Listing Five
(a)
C>type cfs5.xsl
<xsl>
 <rule>
 <root/>
 <HTML>
 <HEAD>
 <TITLE>Cars for sale - Example 5</TITLE>
 </HEAD>
 <BODY>
 <TABLE BORDER="1">
 <children/>
 </TABLE>
 </BODY>
 </HTML>
 </rule>
<rule>
 <target-element type = "Car"/>
 <TR>
 <select-elements>
 <target-element type = "Maker"/>
 </select-elements>
 </TR>
</rule>
<rule>
 <target-element type = "Maker"/>
 <TD>
 <children/>
 </TD>
</rule>
</xsl>

(b)
C>msxsl -i cfs.xml -s cfs5.xsl -o cfs5.htm
C>type cfs5.htm
<HTML>
<HEAD>
<TITLE>Cars for sale - Example 5</TITLE>
</HEAD>
<BODY>
<TABLE BORDER="1">
<TR>
<TD>
Toyota
</TD>
</TR><TR>
<TD>
Ford
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

Listing Six
(a)
C>type cfs8.xsl
<xsl>
<define-script><![CDATA[
// 1.5 Dollars to every Irish Pound
var ExchangeRate = 1.5;
// Convert price into Irish Pounds based on the ExchangeRate variable
// if units is Dollars
function getPriceInIrishPunts(price,units)
{
 if (units == "Dollars")
 return price * ExchangeRate + " Irish Pounds";
 else
 return price + " Irish Pounds";
}
]]></define-script>
<rule>
 <!-- Pattern -->
 <root/>
 <!-- Action -->
 <HTML>
 <HEAD>
 <TITLE>Cars for sale - Example 8</TITLE>
 </HEAD>
 <BODY>
 <P><B>
Note: Exchange Rate Used <eval>ExchangeRate+" Dollars per Irish Pound"</eval>
 </B></P>
 <children/>
 </BODY>
 </HTML>
</rule>
<rule>
 <!-- Pattern -->
 <target-element type = "Car"/>
 <!-- Action -->
 <P>
 Price in Irish Punts= <eval><![CDATA[
 getPriceInIrishPunts(getAttribute("Price"),getAttribute("Units"))
 ]]></eval>
 </P>
 <children/>
</rule>
<rule>
 <!-- Pattern -->
 <target-element type = "Make"/>
 <target-element type = "Color"/>
 <!-- Action -->
 <P>
 <children/>
 </P>
</rule>
</xsl>

(b)
C>msxml -i cfs.xml -s cfs8.xsl -o cfs8.htm
C>type cfs8.htm
<HTML>
<HEAD>
<TITLE>Cars for sale - Example 8</TITLE>
</HEAD>
<BODY>
<P><B> Note : Exchange Rate Used 1.5 Dollars per Irish Pound
</B></P>
<P> Price in Irish Punts= 15000 Irish Pounds
</P>Toyota<P>
Red
</P><P> Price in Irish Punts= 20000 Irish Pounds
</P>Ford<P>
White
</P>
</BODY>
</HTML>


8


