I have a set of XML documents that contain some large lists of values in a single XML element. I need to determine how large each list is and only output the count when they are too large. I am required to use xsltproc that only supports 1.0 and have tried using the count() function but that does not seem to produce any value other than 1. An example style-sheet is:
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<!-- NOTE: US-ASCII encoding is not compatible with Java HTML text -->
<xsl:output method="html" indent="yes" encoding="ASCII"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><xsl:value-of select="'Test Case for count()'"/></title>
</head>
<body>
<xsl:element name="table">
<xsl:attribute name="border">1</xsl:attribute>
<xsl:attribute name="align">center</xsl:attribute>
<xsl:call-template name="DblColTableDataRow">
<xsl:with-param name="DataLabel" select="'count'"/>
<xsl:with-param name="DataValue" select="function-available('count')"/>
</xsl:call-template>
<xsl:call-template name="DblColTableDataRow">
<xsl:with-param name="DataLabel" select="'normalize-space'"/>
<xsl:with-param name="DataValue" select="function-available('normalize-space')"/>
</xsl:call-template>
<xsl:call-template name="DblColTableDataRow">
<xsl:with-param name="DataLabel" select="'string-length'"/>
<xsl:with-param name="DataValue" select="function-available('string-length')"/>
</xsl:call-template>
<xsl:call-template name="DblColTableDataRow">
<xsl:with-param name="DataLabel" select="'replace'"/>
<xsl:with-param name="DataValue" select="function-available('replace')"/>
</xsl:call-template>
<xsl:call-template name="DblColTableDataRow">
<xsl:with-param name="DataLabel" select="'tokenize'"/>
<xsl:with-param name="DataValue" select="function-available('tokenize')"/>
</xsl:call-template>
<xsl:call-template name="DblColTableDataRow">
<xsl:with-param name="DataLabel" select="'contains'"/>
<xsl:with-param name="DataValue" select="function-available('contains')"/>
</xsl:call-template>
<xsl:variable name="DataIn" select="' A B C '"/>
<xsl:variable name="DataList">
<xsl:call-template name="Tokenize-Str">
<xsl:with-param name="Data" select="$DataIn"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="DblColTableDataRow">
<xsl:with-param name="DataLabel"
select="concat('tokenize(',$DataIn,')')"/>
<xsl:with-param name="DataValue">
<xsl:call-template name="Tokenize-Str">
<xsl:with-param name="Data" select="$DataIn"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="DblColTableDataRow">
<xsl:with-param name="DataLabel"
select="concat('count(',$DataIn,')')"/>
<xsl:with-param name="DataValue">
<xsl:copy-of select="$DataList"/>
<xsl:text>: </xsl:text>
<xsl:value-of select="count(($DataList))"/>
</xsl:with-param>
</xsl:call-template>
</xsl:element>
</body>
</html>
</xsl:template>
<xsl:template name="DblColTableDataRow">
<xsl:param name="DataLabel" select="'?:'"/>
<xsl:param name="DataValue" select="'???'"/>
<xsl:element name="tr">
<xsl:element name="td">
<xsl:attribute name="style">text-align:right</xsl:attribute>
<xsl:copy-of select="$DataLabel"/>
</xsl:element>
<xsl:element name="td">
<xsl:copy-of select="$DataValue"/>
</xsl:element>
</xsl:element>
</xsl:template>
<!-- template needed because tokenize function not supported -->
<xsl:template name="Tokenize-Str">
<xsl:param name="Data"/>
<xsl:variable name="DataStr">
<xsl:value-of select="normalize-space($Data)"/>
</xsl:variable>
<xsl:if test="0 != string-length($DataStr)">
<!--xsl:value-of select="concat('Tkn-Str(',$Data,')')"/-->
<xsl:choose>
<xsl:when test="contains($DataStr,' ')">
<xsl:element name="tkn">
<xsl:value-of select="substring-before($DataStr, ' ')"/>
</xsl:element>
<xsl:call-template name="Tokenize-Str">
<xsl:with-param name="Data"
select="substring-after($DataStr, ' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:element name="tkn">
<xsl:value-of select="$DataStr"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
This is set up so that the XML document content does not matter. The command:
xsltproc -o tst.html test_case.xsl whatever.xml
Produces:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=ASCII">
<title>Test Case for count()</title></head><body><table border="1" align="center"><tr xmlns="">
<td style="text-align:right">count</td>
<td>true</td>
</tr>
<tr xmlns="">
<td style="text-align:right">normalize-space</td>
<td>true</td>
</tr>
<tr xmlns="">
<td style="text-align:right">string-length</td>
<td>true</td>
</tr>
<tr xmlns="">
<td style="text-align:right">replace</td>
<td>false</td>
</tr>
<tr xmlns="">
<td style="text-align:right">tokenize</td>
<td>false</td>
</tr>
<tr xmlns="">
<td style="text-align:right">contains</td>
<td>true</td>
</tr>
<tr xmlns="">
<td style="text-align:right">tokenize( A B C )</td>
<td>
<tkn>A</tkn><tkn>B</tkn><tkn>C</tkn>
</td>
</tr>
<tr xmlns="">
<td style="text-align:right">count( A B C )</td>
<td>
<tkn>A</tkn><tkn>B</tkn><tkn>C</tkn>: 1</td>
</tr></table></body></html>
I am not sure why I am getting a count of 1 since my template is clearly returning 3 element nodes.