B
Bostonasian
I have following original xml :
<HealthHistory>
<BloodPressure low="80" high="120" TransactionDate="1/1/2007"/>
<BloodPressure low="90" high="140" TransactionDate="1/1/2007"/>
<BloodPressure low="70" high="110" TransactionDate="1/1/2007"/>
<BloodPressure low="80" high="120" TransactionDate="1/3/2007"/>
<BloodPressure low="80" high="120" TransactionDate="1/4/2007"/>
<BodyFat value="22" TransactionDate="1/2/2007"/>
<BodyFat value="22" TransactionDate="1/3/2007"/>
<BodyFat value="22" TransactionDate="1/4/2007"/>
<HeartRate value="87" TransactionDate="1/1/2007"/>
<HeartRate value="87" TransactionDate="1/2/2007"/>
</HealthHistory>
Then I wrote following xslt :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xslutput omit-xml-declaration="yes"/>
<xsl:key name="DateKey" match="*" use="@TransactionDate"/>
<xsl:key name="NameKey" match="*" use="name()"/>
<xsl:template match="/">
<EventCalendar>
<xsl:apply-templates select="//*"/>
</EventCalendar>
</xsl:template>
<xsl:template match="*">
<xsl:for-each
select="*[generate-id()=generate-id(key('DateKey',@TransactionDate)[1])]">
<DateKey>
<xsl:attribute name="Date">
<xsl:value-of select="@TransactionDate"/>
</xsl:attribute>
<xsl:for-each select="key('DateKey',@TransactionDate)">
<Criteria>
<xsl:attribute name="Name">
<xsl:value-of select="name()"/>
</xsl:attribute>
</Criteria>
</xsl:for-each>
</DateKey>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
To return following :
<EventCalendar>
<DateKey date="1/1/2007">
<criteria name="BloodPressure"/>
<criteria name="BloodPressure"/>
<criteria name="BloodPressure"/>
<criteria name="HeartRate"/>
</DateKey>
<DateKey date="1/2/2007">
<criteria name="BodyFat"/>
<criteria name="HeartRate"/>
</DateKey>
<DateKey date="1/3/2007">
<criteria name="BloodPressure"/>
<criteria name="BodyFat"/>
</DateKey>
<DateKey date="1/4/2007">
<criteria name="BloodPressure"/>
<criteria name="BodyFat"/>
</DateKey>
</EventCalendar>
But the problem is that I need to return unique value of "name"
attribute in "criteria" element. Thefore, for <DateKey
date="1/1/2007">, I want to return <criteria name="BloodPressure"/>
once, instead of three times.
What am I missing?
Thanks bunch
<HealthHistory>
<BloodPressure low="80" high="120" TransactionDate="1/1/2007"/>
<BloodPressure low="90" high="140" TransactionDate="1/1/2007"/>
<BloodPressure low="70" high="110" TransactionDate="1/1/2007"/>
<BloodPressure low="80" high="120" TransactionDate="1/3/2007"/>
<BloodPressure low="80" high="120" TransactionDate="1/4/2007"/>
<BodyFat value="22" TransactionDate="1/2/2007"/>
<BodyFat value="22" TransactionDate="1/3/2007"/>
<BodyFat value="22" TransactionDate="1/4/2007"/>
<HeartRate value="87" TransactionDate="1/1/2007"/>
<HeartRate value="87" TransactionDate="1/2/2007"/>
</HealthHistory>
Then I wrote following xslt :
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xslutput omit-xml-declaration="yes"/>
<xsl:key name="DateKey" match="*" use="@TransactionDate"/>
<xsl:key name="NameKey" match="*" use="name()"/>
<xsl:template match="/">
<EventCalendar>
<xsl:apply-templates select="//*"/>
</EventCalendar>
</xsl:template>
<xsl:template match="*">
<xsl:for-each
select="*[generate-id()=generate-id(key('DateKey',@TransactionDate)[1])]">
<DateKey>
<xsl:attribute name="Date">
<xsl:value-of select="@TransactionDate"/>
</xsl:attribute>
<xsl:for-each select="key('DateKey',@TransactionDate)">
<Criteria>
<xsl:attribute name="Name">
<xsl:value-of select="name()"/>
</xsl:attribute>
</Criteria>
</xsl:for-each>
</DateKey>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
To return following :
<EventCalendar>
<DateKey date="1/1/2007">
<criteria name="BloodPressure"/>
<criteria name="BloodPressure"/>
<criteria name="BloodPressure"/>
<criteria name="HeartRate"/>
</DateKey>
<DateKey date="1/2/2007">
<criteria name="BodyFat"/>
<criteria name="HeartRate"/>
</DateKey>
<DateKey date="1/3/2007">
<criteria name="BloodPressure"/>
<criteria name="BodyFat"/>
</DateKey>
<DateKey date="1/4/2007">
<criteria name="BloodPressure"/>
<criteria name="BodyFat"/>
</DateKey>
</EventCalendar>
But the problem is that I need to return unique value of "name"
attribute in "criteria" element. Thefore, for <DateKey
date="1/1/2007">, I want to return <criteria name="BloodPressure"/>
once, instead of three times.
What am I missing?
Thanks bunch