XSLT count with double criteria

Posted on 2009-04-27
Last Modified: 2013-11-18
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.
Question by:sasapopovic
    LVL 18

    Accepted 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

    LVL 9

    Author Closing Comment

    Hello zc2,

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
    Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now