Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Aggregate Functions on XML Attributes

Status
Not open for further replies.

Detron

Programmer
Apr 23, 2001
2
US
First, my XML sample:

<docroot>
<datepoint rawtime="1199346540000" objid="4">
<issue severity="1">Random Issue #1</issue>
<issue severity="3">Random Issue #2</issue>
<issue severity="2">Random Issue #3</issue>
</datepoint>

<datepoint rawtime="1199346980500" objid="4">
<issue severity="3">Random Issue #1</issue>
<issue severity="3">Random Issue #2</issue>
<issue severity="2">Random Issue #3</issue>
</datepoint>

.
.
.
</docroot>

And here's my problem: how do I output each distinct "objid" (in my example, I only show one), its most recent "rawtime" (i.e. the max value), and the highest "severity" (i.e. the min value).

In the above XML sample, since I list only one objid, my desired output would be as follows:

OBJID = 4
RAWTIME = 1199346980500
SEVERITY = 2

Assuming I'm certain of OBJID 4's existence (in other words, I can directly access that set of nodes via "/docroot/datepoint[@objid=4]"), is there a way for me to apply aggregate functions such as "min" and "max" to complete the "select" below to derive the values I seek?

RAWTIME = <xsl:value-of select=""/>
SEVERITY = <xsl:value-of select=""/>

If that's not possible, which other way might I go about tackling this?

Thanks.
 
You don't say what XSL processor you are using, but assuming that you do not have Xpath 2.0, you use recursion to do max and min. You may find examples here.

The basic technique is to 'compute' the maximum value into a variable (e.g. maxtime), then in your <xsl:value-of select="$maxtime"/>.

If your input document has a large number of nodes to process, you might want to use fancier methods of recursion to reduce processing time, but the example provided probably will be quite suitable.

Tom Morrison
 
Thanks for your help. It looks like I may have to tweek it some in order to sort on the attribute, but that shouldn't be too big of a problem.

BTW, I'm using Saxon.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top