output a table without loop : template match « XSLT stylesheet « XML






output a table without loop


File: Data.xml
<?xml version="1.0" ?>

<customer-list>
  <customer>
    <name>
      <first>A</first>
      <last>B</last>
    </name>
    <order>C</order>
    <order>D</order>
  </customer>

</customer-list>

File: Transform.xslt

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

  <xsl:template match="/">
    <HTML>
      <HEAD>
        <TITLE>Customers</TITLE>
      </HEAD>
      <BODY>
        <TABLE BORDER="1" >
          <xsl:apply-templates select="customer-list/customer" />
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>

  <xsl:template match="customer">
    <TR><TH ALIGN="left">
      <xsl:apply-templates select="name"/>
    </TH></TR>
    <xsl:apply-templates select="order"/>
  </xsl:template>

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

  <xsl:template match="name">
    <xsl:value-of select="last" />, 
    <xsl:value-of select="first" />
  </xsl:template>

</xsl:stylesheet>

Output:

<HTML>
   <HEAD>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <TITLE>Customers</TITLE>
   </HEAD>
   <BODY>
      <TABLE BORDER="1">
         <TR>
            <TH ALIGN="left">B, 
                   A
            </TH>
         </TR>
         <TR>
            <TD>C</TD>
         </TR>
         <TR>
            <TD>D</TD>
         </TR>
      </TABLE>
   </BODY>
</HTML>

 








Related examples in the same category

1.Define and use template
2.Locate parent tags and get value from children tags
3.Select value from an element with value-of
4.Get two values in one template
5.match an element
6.output in template
7.set match mode to fulltext
8.match and get value operations with namespace
9.Call a template with parameter
10.template mode="index"
11.template with parameters