Thursday, April 25, 2013

XSL - Global Variable Change

Global Variable in XSL

http://stackoverflow.com/questions/9608432/incrementing-and-checking-the-counter-variable-in-xslt

Cannot increment a variable in XSLT because all XSLT variables are constant. There are, however, several methods available for accomplishing this task:

1. Use a recursive <xsl:call-template> element passing the value of $noOfElements+1 as a param to the template.

2. Use an XSLT extension such as Java or JavaScript and set an xsl:variable equal to an instance of an object. Even though XSL variable's are constant, the data contained within an instance variable such as a Java Integer or a Java StringBuffer can be modified. In other words, you can not change the object referenced by the XSL variable (set the variable to a different Java object), but you can change the data members of the object referenced by that variable.


Sample Input File
=============
<Foo>
    <Bar>Line 1</Bar>
    <Bar>Line 2</Bar>
    <Bar>Line 3</Bar>
    <Bar>Line 4</Bar>
    <Bar>Line 5</Bar>
    <Bar>Line 6</Bar>
</Foo>

Sample XSLT
==========
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
        xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
        xmlns:xalan='http://xml.apache.org/xslt'
        xmlns:java="http://xml.apache.org/xslt/java"
        extension-element-prefixes="" exclude-result-prefixes="xsl xalan java"
        version="1.0">

    <!-- define the output parameters -->
    <xsl:output method='xml'  version='1.0' encoding='UTF-8'
      omit-xml-declaration='no' indent='yes' xalan:indent-amount='4' />

       <!-- Declare my counter variable here -->
    <xsl:variable name="javaCounter" select="java:java.util.ArrayList.new()"/>

    <xsl:template match="/Foo">
        <Foo>
            <xsl:for-each select="./Bar">
            <!-- increment the Counter , ie. add item to the ArrayList
object -->
            <xsl:variable name="Foo" select="java:add($javaCounter, '1')"/>
            <Bar><xsl:value-of select="."/></Bar>

            <!-- ask for the size of the ArrayList  -->
            <Counter><xsl:value-of select="java:size($myCounter)"/></Counter>
            </xsl:for-each>
        </Foo>
    </xsl:template>
</xsl:stylesheet>


Output File
=========
<?xml version="1.0" encoding="UTF-8"?>
<Foo>
    <Bar>Line 1</Bar><Counter>1</Counter>
    <Bar>Line 2</Bar><Counter>2</Counter>
    <Bar>Line 3</Bar><Counter>3</Counter>
    <Bar>Line 4</Bar><Counter>4</Counter>
    <Bar>Line 5</Bar><Counter>5</Counter>
    <Bar>Line 6</Bar><Counter>6</Counter>
</Foo>

Namespace can be defined as following

xmlns:date="java:java.util.Date"
xmlns:array="java:java.util.ArrayList" 

Clear Array

java:clear($javaCounter)


Java with XSL
http://cafeconleche.org/books/xmljava/chapters/ch17s03.html

Reference

http://xml.apache.org/xalan-j/extensions.html 
http://cafeconleche.org/books/xmljava/chapters/ch17s03.html
http://mail-archives.apache.org/mod_mbox/xml-xalan-j-users/200605.mbox/%3C53cb81ac0605031612s37b76138q397144a782586170@mail.gmail.com%3E

0 comments:

Post a Comment