iswinky
iswinky

Reputation: 1969

XSLT - Get maximum child nodes for group of nodes

Is it possible to get the maximum grandchild nodes in all the child nodes, without looping through them? In the example below there are four document nodes, with 4, 5, 1 and 2 value nodes respectively. So ideally I'd want to return 5 as the answer of the expression / function.

Is this possible without looping in xslt 2.0 or by using xpath?

XML:

<File>
    <Document>
        <Value>..</Value>
        <Value>..</Value>
        <Value>..</Value>
        <Value>..</Value>
    </Document>
    <Document>
        <Value>..</Value>
        <Value>..</Value>
        <Value>..</Value>
        <Value>..</Value>
        <Value>..</Value>
    </Document>
    <Document>
        <Value>..</Value>
    </Document>
    <Document>
        <Value>..</Value>
        <Value>..</Value>
    </Document>
</File>

Upvotes: 2

Views: 1025

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167516

You can use max(/File/Document/count(*)).

Upvotes: 2

Ian Roberts
Ian Roberts

Reputation: 122364

Assuming the File element is the current context item, you could use

max(Document/count(Value))

Document/count(Value) gives you a sequence of integers (the number of Value children of each Document), max picks the largest value from that sequence.

Upvotes: 4

Related Questions