·  XSLT – Elements – <xsl:apply-templates>

 

- Info :

  - If select attribute is not defined, XSLT will use appropriate <xsl:template> for ALL   children of the current element.

  - If select attribute is    defined, XSLT will use appropriate <xsl:template> ONLY for   children matching select criteria

    - When <xsl:template> finds matching element, it will not go through children unless you use <xsl:apply-templates>.

    - This element serves as function call to make XSL file more readable as demonstrated with 2 examples below.

 

- Example:

 

    test.xml

    <?xml version="1.0"?>

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

    

    <people>

    

      <student>

        <name>Jack</name>

        <age> 23  </age>

      </student>

    

      <student>

        <name>Jill</name>

        <age> 43  </age>

      </student>

    

    </people>

    

    test.xsl

    <?xml version="1.0" ?>

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

    

      <xsl:template match="people">

        start of student's list <br/>

        <xsl:apply-templates select="student"/> 

        end of student's list

      </xsl:template>

      

      <xsl:template match="student">

        Name is:<xsl:value-of select="name"/> <br/>

        Age  is:<xsl:value-of select="age" /> <br/>

      </xsl:template>

        

    </xsl:stylesheet>

    

    test.xsl    (same result withouth using <xsl:apply-templates>)

    <?xml version="1.0" ?>

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

    

      <xsl:template match="people">

        start of student's list <br/>

        <xsl:for-each select="student"> 

          Name is:<xsl:value-of select="name"/> <br/>

          Age  is:<xsl:value-of select="age" /> <br/>

        </xsl:for-each> 

        end of student's list

      </xsl:template>

        

    </xsl:stylesheet>

    

    result

    start of student's list
    Name is:Jack
    Age is: 23
    Name is:Jill
    Age is: 43
    end of student's list