Freigeben über


Example of <xsl:apply-templates> 

The style sheet in this example formats customer data in XML into an HTML <TABLE> element. In the output table, each row represents a customer and the columns represent the customer's name, address, and phone number. The <xsl:sort> element sorts the customers by state, with all customers from a single state sorted by name.

XML File (customers.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="applyt.xsl" ?>
<customers>
   <customer>
      <name>John Smith</name>
      <address>123 Oak St.</address>
      <state>WA</state>
      <phone>(206) 123-4567</phone>
   </customer>
   <customer>
      <name>Zack Zwyker</name>
      <address>368 Elm St.</address>
      <state>WA</state>
      <phone>(206) 423-4537</phone>
   </customer>
   <customer>
      <name>Albert Aikens</name>
      <address>368 Elm St.</address>
      <state>WA</state>
      <phone>(206) 423-4537</phone>
   </customer>
   <customer>
      <name>Albert Gandy</name>
      <address>6984 4th St.</address>
      <state>WA</state>
      <phone>(206) 433-4547</phone>
   </customer>
   <customer>
      <name>Peter Furst</name>
      <address>456 Pine Av.</address>
      <state>CA</state>
      <phone>(209) 765-4321</phone>
   </customer>
   <customer>
      <name>Dan Russell</name>
      <address>9876 Main St.</address>
      <state>PA</state>
      <phone>(323) 321-7654</phone>
   </customer>
</customers>

XSLT File (applyt.xsl)

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="/">
   <HTML>
      <BODY>
         <TABLE border="1" cellspacing="0" cellpadding="2">
            <xsl:apply-templates select="customers/customer">
               <xsl:sort select="state"/>
               <xsl:sort select="name"/> 
            </xsl:apply-templates>
         </TABLE>

      </BODY>
   </HTML>
</xsl:template>

<xsl:template match="customer">
   <TR>
      <xsl:apply-templates select="name" />
      <xsl:apply-templates select="address" />
      <xsl:apply-templates select="state" />
      <xsl:apply-templates select="phone" />
      <xsl:apply-templates select="phone" mode="accountNumber"/>
   </TR>
</xsl:template>

<xsl:template match="name">
   <TD STYLE="font-size:14pt font-family:serif">
      <xsl:apply-templates />
   </TD>
</xsl:template>

<xsl:template match="address">
   <TD> <xsl:apply-templates /> </TD>
</xsl:template>

<xsl:template match="state">
   <TD> <xsl:apply-templates /> </TD>
</xsl:template>

<xsl:template match="phone">
   <TD> <xsl:apply-templates /> </TD>
</xsl:template>

<xsl:template match="phone" mode="accountNumber">
   <TD STYLE="font-style:italic">
      1-<xsl:value-of select="."/>-001
   </TD>
</xsl:template>

</xsl:stylesheet>

Output

This is the formatted output:

Formatted output

This is the processor output:

<HTML>
<BODY>
<TABLE border="1" cellspacing="0" cellpadding="2">
<TR>
<TD STYLE="font-size:14pt font-family:serif">Peter Furst</TD>
<TD>456 Pine Av.</TD>
<TD>CA</TD>
<TD>(209) 765-4321</TD>
<TD STYLE="font-style:italic">
      1-(209) 765-4321-001
   </TD>
</TR>
<TR>
<TD STYLE="font-size:14pt font-family:serif">Dan Russell</TD>
<TD>9876 Main St.</TD>
...
</TR>
</TABLE>
</BODY>
</HTML>