Link to home
Start Free TrialLog in
Avatar of gadkins
gadkins

asked on

XSLT: Evaluate two XML parent nodes and return a count of how many matches

I've got a XML file that contains two AAA elements:

<Root>
    <AAA>
        <BBB number="1">
            <CCC value="1" myAttribute="N" />
            <CCC value="2" myAttribute="Y" />
            <CCC value="3" myAttribute="N" />
        </BBB>
        <BBB number="2">
            <CCC value="1" myAttribute="N" />
            <CCC value="2" myAttribute="N" />
            <CCC value="3" myAttribute="Y" />
        </BBB>
    </AAA>
    <AAA>
        <BBB number="1">
            <CCC value="1" myAttribute="N" />
            <CCC value="2" myAttribute="Y" />
            <CCC value="3" myAttribute="N" />
        </BBB>
        <BBB number="2">
            <CCC value="1" myAttribute="Y" />
            <CCC value="2" myAttribute="N" />
            <CCC value="3" myAttribute="N" />
        </BBB>
    </AAA>
</Root>

Basically, the process should return "1" because <BBB number="1"> in AAA[1] and AAA[2] has the same CCC element with a myAttribute="Y".  Each BBB node will only have one CCC node that has myAttribute="Y".

Could someone provide me with the best way to do this in XSLT.  Keep in mind that the files I'm processing are fairly small (Perhaps up to 15 BBB nodes each with up to 10 CCC nodes) and thus the XSLT method used should be suited to small rather than large files (if relevant).

Also, I'll be using Oracle 9.2's XSLT processor so please keep the solution non-processor specific.  I don't need the PL/SQL code, just the XLS stylesheet.

Thanks very much!
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Will there always only be two AAA nodes?
Avatar of gadkins
gadkins

ASKER

Yes.
Avatar of gadkins

ASKER

Just want to clarify that I want a count of how many BBB have the matching CCC node set to Y.

I've been having a crack here myself and have got something that returns either 0 or 1 for each matching CCC that has a myAttribute of "Y" in both AAA nodes, but I don't have a clue how to add em up.  So right now my output it 10000001000  (I'm testing with more data than I specified here).  It's so frustrating!  XSLT just makes me want to take up a less stressful job... like defusing bombs.  :)
To clarify things :
Do you want to count CCC nodes that have the @myAttribute to 'Y' have a defined @value under different BBB that have the same @number as only one or as as many as there is of BBB ?
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Of course you can decide what you output here:
           <xsl:otherwise>
                <xsl:value-of select="$Ccount"/><xsl:text> nodes found</xsl:text>
            </xsl:otherwise>
you might be only interested in the <xsl:value-of select="$Ccount"/> :-)
add
<xsl:output method="text"/>
as the first child of <xsl:stylesheet>
to get rid of the XML declaration
Avatar of gadkins

ASKER

Thanks mate.  It's midnight here, so I'm too tired to bother looking just now.  I'll have a decent look in the morning.
Avatar of gadkins

ASKER

After minor changes to fit my real XML layout, it worked perfectly.  Thanks very much.  Great work!