Question

I want to iterate ArrayList <String> and put all strings to output tree, but have no any idea how to do it.

java method:

public ArrayList<String> getErrorList(String name) {
    if (errorMap.containsKey(name)) {
        return errorMap.get(name);
    }
    return new ArrayList<>();
}

xsl document:

<xsl:variable name="list">
    <xsl:value-of select="validator:getErrorList($validator, 'model')"/>
</xsl:variable>

<tr>
    <td style="color: red;">
        <ul>
            <li> first string from ArrayList </li>
            . . .
            <li> last string from ArrayList </li>
        </ul>
    </td>
</tr>

I'm pretty new to xsl, so I'm asking you for help.

Was it helpful?

Solution

Your mistake was to initialize variable such as

<xsl:variable name="list">
    <xsl:value-of select="validator:getErrorList($validator, 'model')"/>
</xsl:variable>

because xslt thinks, that value of this variable is #STRING, so you'll got error

For extension function, could not find method java.util.ArrayList.size([ExpressionContext,] #STRING).

You have to use next declaration, instead of previous:

<xsl:variable name="list" select="validator:getErrorList($validator, 'model')"/>

so, method getErrorList would return ArrayList object. Next code will show you how to iterate ArrayList collection, using XSL functional:

<xsl:variable name="list" select="validator:getErrorList($validator, 'model')"/>
<xsl:variable name="length" select="list:size($list)"/>
<xsl:if test="$length > 0">
    <xsl:call-template name="looper">
        <xsl:with-param name="iterations" select="$length - 1"/>
        <xsl:with-param name="list" select="$list"/>
    </xsl:call-template>
</xsl:if>
. . .
<xsl:template name="looper">
    <xsl:param name="iterations"/>
    <xsl:param name="list"/>
    <xsl:if test="$iterations > -1">
        <xsl:value-of select="list:get($list, $iterations)"></xsl:value-of>
        <xsl:call-template name="looper">
             <xsl:with-param name="iterations" select="$iterations - 1"/>
               <xsl:with-param name="list" select="$list"/>
          </xsl:call-template>
     </xsl:if>
</xsl:template>

So, you have to use recursion, 'cause it's not possible to use loops in functional language, such as XSLT. You can read about it here

OTHER TIPS

you have to define a namespace for your Java extension function in your stylesheet. It should look like xmlns:yourchoice = "javapackage.classname. Assuming the method getErrorList is in the class ErrorListClass it could look like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:validator="mypackage.ErrorListClass"
exclude-result-prefixes="filecounter" version="1.0">

And then you call it in your XSLT

<xsl:variable name="list">
<xsl:value-of select="validator:getErrorList($validator, 'model')"/>
</xsl:variable>

I hope that helps.

Best regards, Peter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top