·  XSLT – Elements – <xsl:template>

 

- Info :

  - XSL file is set of <xsl:template> elements each one defining what should be done with elements matching it's criteria.

  - Use XPath rules and function to define match criteria. Some of these are shown below.

 

- Parameters:

  match = "/"                       Apply this template to root element.

        = "name"                    Apply this template to all <name> elements.

        = "student/name"            Apply this template to all <name> elements which have   <student> as parent.

        = "/people/student/name"    Apply this template to all elements whose full path is   as defined.

        = "text()"                  Apply this template to all text elements

        = "name/text()"             Apply this template to all text elements which have <name> as   parent.

 

- Example:

  - We have defined 3 templates.

  - First two tell what to do with <name> elements depending if parent element is <student> or <teacher>.

  - Last one is used to override default template for text elements so that text of text elements is not displayed.

 

    test.xml

    <?xml version="1.0"?>

    <?xml-stylesheet type="text/xsl" href="test.xsl"?>

    

    <people>

    

      List of students

      <student>

        <name>Jack</name>

        <age> 23  </age>

      </student>

      

      List of teachers

      <teacher>

        <name>Mandle</name>

        <age> 55    </age>

      </teacher>

    

    </people>

    

    test.xsl

    <?xml version="1.0" ?>

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

    

      <xsl:template match="student/name">

        Found student's name

        <xsl:value-of select="."/> <br/>

      </xsl:template>

      

      <xsl:template match="teacher/name">

        Found teacher's name

        <xsl:value-of select="."/> <br/>

      </xsl:template>

      

      <xsl:template match="text()"/> 

    

    </xsl:stylesheet>

    

    result

    Found student's name Jack
    Found teacher's name Mandle