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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding the last node in list 1

Status
Not open for further replies.

jhartridge

Programmer
Feb 20, 2004
4
GB
Hello,

I have a program that stores the results of various tests on a piece of eqiptment in XML format.

A basic test result is of the form :-
Code:
<PumpLogItem>
   <Date>05/02/04</Date>
   <Time>09:42:30</Time>
   <Operator>jh</Operator>
   <Result>
      <Description>Dry Side Pressure Calibration</Description>
      <Detail>Pass</Detail>
      <Data>08256,45760,99,589,585,1220,1216</Data>
   </Result>
</PumpLogItem>
A PumpLog consists of many such entries.

The <Description> can be ony one of about a dozen different test descriptions. Several <PumpLogItems> can have the same <Description> for repeated tests.

The PumpLogItems are all in Date and Time order (as they are added to the end when the test is done).

I need to be able to display just the LAST <PumpLogItem> for each <Description> in the list of <PumpLogItem>s to show the latest test results for all the tests done.

I can display the FIRST <PumpLogItem> for each <Description> by using :-
Code:
<xsl:for-each select=&quot;PumpLogItem&quot;>
   <xsl:if test=&quot;not(Result/Description=preceding:Result/Description)&quot;>
      output PumpLog item elements
   </xsl:if>
</xsl:for-each>
but how do I get the LAST <PumpLogItem> ??

Sorry for such a long question. Thanks for any pointers.

John
 
Try this:

<xsl:for-each select=&quot;PumpLogItem&quot;>
<xsl:if test=&quot;position() = last()&quot;>
output PumpLog item elements
</xsl:if>
</xsl:for-each>


Hope this helps.
 
Thank you for the reply, but your suggestion only gives me the last of all the <PumpLogItem> entries.

What I need is the last <PumpLogItem> with a particular <Description>.

If I was doing it in another language I would have done something like :-

Code:
Store first <PumpLogItem> with required <Description>
For each <PumpLogItem>
{
   If Current.<Description> == Stored.<Description>
   {
      Stored = Current
   }
}
Output Stored

But this won't work in xsl will it. There is no way to store the current value in a <xsl:variable> as they can only be set once.

Is there another mechanism in xsl I could use to do this ?

Thanks

John
 
OK, try this:

&lt;xsl:for-each select=&quot;PumpLogItem[Result/Description = 'xxx']&quot;&gt;
&lt;xsl:if test=&quot;position() = last()&quot;&gt;
output PumpLog item elements
&lt;/xsl:if&gt;
&lt;/xsl:for-each&gt;
 
Excellent !!
[thumbsup]
Thank you for your help. That was the result I was looking for. I just couldn't get all the right bits of xsl together to get it to work neatly, so was trying to find a more &quot;programmatic&quot; way of doing it, but your help has given me what I originally wanted to do.

Thanks again.

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top