Reputation: 884
New to XSLT.
Here is the XML.
<Resource resource_id="603">
<Computer full_name="AJAY-2064" remarks="computer key expired."/>
</Resource>
<Resource resource_id="604">
<Computer full_name="AJAY-2070" remarks="computer key expired."/>
</Resource>
<Resource resource_id="602">
<Computer full_name="AJAY-1662" remarks="Not a valid key. "/>
</Resource>
My Requirement is to Get the unique remarks count and group the name based on remarks.
(i.e)
Computer key expired Count (2)
AJAY-2064
AJAY-2070
Not a valid key (1)
AJAY-1662
Here is the xsl.
<xsl:variable name="license.computer" select="Resource/ManagedComputer"/>
<xsl:key name="keyComputer" match="$license.computer" use="@remarks"/>
<xsl:for-each select="$license.computer[generate-id() =
generate-id(key('keyComputer', @remarks)[1])]">
<xsl:variable name="license.remarks" select="@remarks"/>
<xsl:for-each select="$license.computer[contains(@remarks,$license.remarks)]">
<xsl:choose>
<xsl:when test="position() mod 2 = 0" >
<tr>
<td bgcolor="#F9F9F9"><xsl:value-of select="@full_name"/></td>
<td bgcolor="#F9F9F9"><xsl:value-of select="@remarks"/></td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr>
<td bgcolor="#FFFFFF"><xsl:value-of select="@full_name"/></td>
<td bgcolor="#FFFFFF"><xsl:value-of select="@remarks"/></td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
But it doesn't seem to be working. Provide any workaround if you have. Any debugger to debug xslt ?
Upvotes: 0
Views: 1667
Reputation: 70648
There are quite a lot of reasons why it is "not working", although some of them may be due the question not being fully representative of your actual code.
Still, the first issue is with your license.computer
variable. Assuming this is a global variable, and not within a template that matches root
, the xpath expression will be relative to the document node, and so is looking for a root element of Resource
. But I am guessing you have a different root element, so it probably needs to look like this....
<xsl:variable name="license.computer" select="//Resource/Computer"/>
(Also note your XSLT refers to ManagedComputer
but your XML sample only show Computer
)
Next up, is that you are not allowed to use variables in the match condition of a key, so your key should actually look like this
<xsl:key name="keyComputer" match="//Resource/Computer" use="@remarks"/>
Also worth noting, although it is not actually a problem in your XSLT, but to iterate over the elements in the group, you can replace your inner xsl:for-each
with this
<xsl:for-each select="key('keyComputer', @remarks)">
Try this XSLT
Actually, you could also define it just like this, as you don't need the full xpath expression, unless you want to restrict which Computer
elements it matches
<xsl:key name="keyComputer" match="Computer" use="@remarks"/>
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:variable name="license.computer" select="//Resource/Computer"/>
<xsl:key name="keyComputer" match="Computer" use="@remarks"/>
<xsl:template match="/">
<xsl:for-each select="$license.computer[generate-id() = generate-id(key('keyComputer', @remarks)[1])]">
<xsl:for-each select="key('keyComputer', @remarks)">
<xsl:variable name="bgcolor">
<xsl:choose>
<xsl:when test="position() mod 2 = 0" >#F9F9F9</xsl:when>
<xsl:otherwise>#FFFFFF</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<tr>
<td bgcolor="{$bgcolor}"><xsl:value-of select="@full_name"/></td>
<td bgcolor="{$bgcolor}"><xsl:value-of select="@remarks"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Note also I have re-jigged how it does the background colour of the cells to avoid some code repetition.
Upvotes: 2