Search code examples
xsltxslt-2.0

how to count the value of multiple elements in xslt


I have this xslt code to count the abstract length of type main:

<xsl:variable name="mainAbstract">
    <xsl:value-of select="string-length(normalize-space(abstract[@type = 'main']))"/>
</xsl:variable>

and I had an issue where I had multiple xml tags that matches the same pattern and it's not working.

How should I count the number of characters in multiple elements from this select statement?


Solution

  • If you have multiple abstract elements of type main, then you need to select which one you want to process.

    If - as it seems - you want the sum of their individual string-lenghts, then do:

    <xsl:value-of select="sum(abstract[@type = 'main']/string-length(normalize-space()))"/>