• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

XSLT count with double criteria

Hello Experts,

I need some help with XSLT and COUNT function. The XML looks like this (it is just a sample similar to actual XML which is quite complex):

            <data alias="subitemCategory">Category 1</data>
            <data alias="subitemCategory">Category 1</data>
            <data alias="subitemCategory">Category 2</data>
                  <data alias="category">Category 1</data>
                  <data alias="category">Category 2</data>

I need to write an XSLT to count nodes with alias=subitemCategory and with data equal to "Category 1".

I wrote something like:
<xsl:value-of select="count(nodes/node/data [@alias = 'subitemCategory'])"/>

and I can write something like:
<xsl:value-of select="count(nodes/node[data  = 'Category 1'])"/>

but I don't know how to write a single "count" function and to specify both criteria:
 - @alias = 'subitemCategory'
 - data = 'Category 1'.

I would appriciate any kind of help.
1 Solution
You said nothing about the nodes inside the "categories" element. Should the be count?
If not use the first expression of the following, if yes - the second one
<xsl:value-of select="count(nodes/node[data/@alias = 'subitemCategory' and data = 'Category 1'])"/>
<xsl:value-of select="count(nodes//node[data/@alias = 'subitemCategory' and data = 'Category 1'])"/>

Open in new window

sasapopovicAuthor Commented:
Hello zc2,

The first one was what I needed. Thank you very much!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now