·  XSLT – Elements – <xsl:variable>

 

- Info :

  - Variable is global if it's declared as a top-level element, and local if it's declared within a template.

  - Value is defined by:

    - the content of the <xsl:variable> element, and then select attribute must not be defined

    - the select attribute, and then content must be empty: <xsl:variable name="prefix" select="'This is '"/>

    - default as empty string if both content and select attribute are not defined: <xsl:variable name="j"/>

 

- Example:

 

    test.xml

    <?xml version="1.0"?>

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

    

    <people>

    

      <student>

        <name>Jack</name>

        <name>Anton</name>

      </student>

    

    </people>

    

    test.xsl

    <?xml version="1.0" ?>

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

    

      <xsl:variable name="prefix" select="'This is '"/>

      <xsl:variable name="scope"  select="."/>

      <xsl:variable name="suffix">

        <br/>

      </xsl:variable>

    

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

        <xsl:copy-of  select="$prefix"/>

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

        <xsl:copy-of  select="$suffix"/>

      </xsl:template>

      

    </xsl:stylesheet>

    

    result

    This is Jack

    This is Anton

 

- Other:

   <xsl:for-each select="/foo/bar">

     <xsl:variable name="some-bar" select="."/>

     <xsl:value-of select="$some-bar"/>

   </xsl:for-each>