Hi,
Using my computer example, I want to use XSLT to figure out when to take my computer for servicing. My example doc is show below
Code:
<computer name="DELL XPS" service-year="2005">
<component number="1" name="chassis">
<expected-life years="6"/>
<description>some chasis description</description>
</component>
<component number="2" name="hard-disk">
<expected-life years="4"/>
<description>some hard-disk description</description>
</component>
<component number="5" name="screen">
<expected-life years="3"/>
<description>some screen description</description>
</component>
<component number="4" name="keyboard">
<expected-life years="3"/>
<description>some keyboard description</description>
</component>
<component number="5" name="usb">
<expected-life years="5"/>
<description>some usb description</description>
</component>
<component number="6" name="mic">
<expected-life years="2"/>
<description>some keyboard description</description>
</component>
<component number="5" name="mouse">
<expected-life years="1"/>
<description>some usb description</description>
</component>
</computer>
With my XSLT stylesheet, I want to ensure that I take the computer for repair if two or more parts have surpassed their expected life span(given by expected-life attribute). My document node has a service-year attribute to indicate when the computer was last service.
Therefore, I want to somehow go through the whole XML document and retrieve all the @years attributes(from the expected-life element) into a node set.
Then I want to be able to say when to take the PC for servicing once I find the two components that will fail first.
To this effect I am trying to write the stylesheet below but I am stuck. I am wondering if I am on the correct path.
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:variable name="maxFailed">2</xsl:variable>
<xsl:template match="computer">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="component">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="expected-life">
part will fail after: <xsl:value-of select="@years"/> years
<br/>
</xsl:template>
<xsl:template match="@* | node()"/>
</xsl:stylesheet>
Evidently, my stylesheet is incomplete as I am still working on it. I was wondering if I am on the right path. I think if I can get all the @years into a node set and then sort it, I can then just get the third entry in the node set and that the number of years after the last service date when my computer will fail.
Kind Regards,
Shumba