xslt 1.0 - display only one distinct value in a table cell as part of a multi-column table and alternate colors - xslt

I've got an xml file that has some data that is being output in a table split into two columns (effectively). This is the XML
<structuredBody>
<component>
<section>
<templateId root="2.16.840.1.113883.10.20.22.2.3.1" />
<entry>
<organizer>
<component>
<observation>
<code displayName="TIBC" />
<effectiveTime value="8/29/2013 12:00:00 AM" />
<value value="39" />
<referenceRange>
<observationRange>
<text />
</observationRange>
</referenceRange>
</observation>
</component>
</organizer>
</entry>
<entry>
<organizer>
<component>
<observation>
<code displayName="TSAT" />
<effectiveTime value="8/29/2013 12:00:00 AM" />
<value value="25" />
<referenceRange>
<observationRange>
<text />
</observationRange>
</referenceRange>
</observation>
</component>
</organizer>
</entry>
<entry>
<organizer>
<component>
<observation>
<code displayName="Albumin" />
<effectiveTime value="9/5/2013 12:00:00 AM" />
<value value="46" />
<referenceRange>
<observationRange>
<text />
</observationRange>
</referenceRange>
</observation>
</component>
</organizer>
</entry>
<entry>
<organizer>
<component>
<observation>
<code displayName="ALT" />
<effectiveTime value="9/5/2013 12:00:00 AM" />
<value value="48" />
<referenceRange>
<observationRange>
<text>21-72</text>
</observationRange>
</referenceRange>
</observation>
</component>
</organizer>
</entry>
<entry>
<organizer>
<component>
<observation>
<code displayName="Bicarbonate" />
<effectiveTime value="9/5/2013 12:00:00 AM" />
<value value="69" />
<referenceRange>
<observationRange>
<text />
</observationRange>
</referenceRange>
</observation>
</component>
</organizer>
</entry>
</section>
</component>
<component>
<section>
<...>
</section>
</component>
<component>
<section>
<...>
</section>
</component>
</structuredBody>
I've got the output formatted using xslt:
<xsl:template match="/">
<xsl:if test="//section[templateId/#root='2.16.840.1.113883.10.20.22.2.3.1']!=''">
<xsl:variable name="rowLabs" select="ceiling(count(//section[templateId/#root='2.16.840.1.113883.10.20.22.2.3.1']/entry) div $colLabs)" />
<div style="margin-bottom: 5px; padding: 5px; border-bottom: 1px solid #000000;">
<span style="font-weight: bold;">Lab Results:</span>
<table border="0" cellspacing="0" cellpadding="1" width="99%" style="font-size: 11px;">
<xsl:for-each select="//section[templateId/#root='2.16.840.1.113883.10.20.22.2.3.1']/entry[position() <= $rowLabs]">
<tr>
<xsl:variable name="otherEntries" select=".|following-sibling::entry[position() mod $rowLabs = 0]" />
<xsl:apply-templates select="self::*|$otherEntries" />
<xsl:call-template name="blankentries">
<xsl:with-param name="entries" select="$colLabs - count($otherEntries) - 1" />
</xsl:call-template>
<!--<xsl:apply-templates select=".|following-sibling::entry[position() < 2]" />-->
</tr>
</xsl:for-each>
</table>
</div>
</xsl:if>
</xsl:template>
<xsl:template match="section[templateId/#root='2.16.840.1.113883.10.20.22.2.3.1']/entry">
<td width="75">
<xsl:call-template name="StripTime">
<xsl:with-param name="DateTime" select="organizer/component/observation/effectiveTime/#value" />
</xsl:call-template>
</td>
<td width="198">
<xsl:value-of select="organizer/component/observation/code/#displayName"/>
</td>
<td width="50">
<xsl:value-of select="organizer/component/observation/value/#value"/>
</td>
<td width="75">
<xsl:value-of select="organizer/component/observation/referenceRange/observationRange/text"/>
</td>
</xsl:template>
<xsl:template name="blankentries">
<xsl:param name="entries" />
<xsl:if test="$entries > 0">
<td></td>
<xsl:call-template name="blankentries">
<xsl:with-param name="entries" select="$entries - 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
so that the resulting entry nodes run down and then over so that the output is:
<table>
<tr>
<td>8/29/2013</td>
<td>TIBC</td>
<td>39</td>
<td></td>
<td>9/5/2013</td>
<td>ALT</td>
<td>48</td>
<td>21-72</td>
</tr>
<tr>
<td>8/29/2013</td>
<td>TSAT</td>
<td>25</td>
<td></td>
<td>9/5/2013</td>
<td>Bicarbonate</td>
<td>69</td>
<td></td>
</tr>
<tr>
<td>9/5/2013</td>
<td>Albumin</td>
<td>46</td>
<td></td>
</tr>
</table>
This gives me:
[entry 1] [entry 4]
[entry 2] [entry 5]
[entry 3]
which is what I'm looking for and that's great.
What I can't figure out is how to get the different 4-cell entry sets to alternate colors as it goes through. I can't use position() because I'm manipulating it to get the desired output order in the table. If I output the position to figure out the math, on the left column, it's always "1" and on the right column it's always "2" so I can't do a position() mod 2 = 1 to set a style attribute.
Secondly, I only want the date value to appear one time and then not appear until it changes. That would make it such that the output ideally should look like:
<table>
<tr>
<td>8/29/2013</td>
<td>TIBC</td>
<td>39</td>
<td></td>
<td bgcolor="dcdcdc"></td>
<td bgcolor="dcdcdc">ALT</td>
<td bgcolor="dcdcdc">48</td>
<td bgcolor="dcdcdc">21-72</td>
</tr>
<tr>
<td bgcolor="dcdcdc"></td>
<td bgcolor="dcdcdc">TSAT</td>
<td bgcolor="dcdcdc">25</td>
<td bgcolor="dcdcdc"></td>
<td></td>
<td>Bicarbonate</td>
<td>69</td>
<td></td>
</tr>
<tr>
<td>9/5/2013</td>
<td>Albumin</td>
<td>46</td>
<td></td>
</tr>
</table>
I can't put the bgcolor attribute in the "tr" tag because it should alternate even across the "columns" and not just the entire row.
Thanks for any help. This site has brought my xslt knowledge a long way. I've only started delving into it in the last month.

For the colours you essentially have two cases depending on whether $rowLabs is odd or even.
If it's even then odd rows will be entirely "white" and even rows will be entirely "black"
If it's odd then
for odd rows, odd columns will be white and even ones black
for even rows, odd columns will be black and even ones white
("odd" and "even" counting from 1 as XPath position() does, so the first row/column is odd, the second is even, etc.).
You can encode that logic in XSLT by adding some parameters to the templates. Replace the
<xsl:apply-templates select="self::*|$otherEntries" />
with
<xsl:apply-templates select="self::*|$otherEntries">
<xsl:with-param name="rowNum" select="position()" />
<xsl:with-param name="totalRows" select="$rowLabs" />
</xsl:apply-templates>
and add the parameters to the section template
<xsl:template match="section[templateId/#root='2.16.840.1.113883.10.20.22.2.3.1']/entry">
<xsl:param name="rowNum" select="1" />
<xsl:param name="totalRows" select="2" />
Now we need a named template we can call to implement the logic I described above:
<xsl:template name="bgcolor">
<xsl:param name="rowNum" select="1" />
<xsl:param name="totalRows" select="2" />
<xsl:if test="($totalRows mod 2 = 0 and $rowNum mod 2 = 0) or
($totalRows mod 2 = 1 and $rowNum mod 2 != position() mod 2)">
<xsl:attribute name="bgcolor">dcdcdc</xsl:attribute>
</xsl:if>
</xsl:template>
This adds the bgcolor attribute if either there is an even number of rows and the current rownum is even, or if there is an odd number of rows and the current column number within the row has a "different oddness" from the row number.
Finally we call this template within the <td> elements, e.g.
<td width="50">
<xsl:call-template name="bgcolor">
<xsl:with-param name="rowNum" select="$rowNum" />
<xsl:with-param name="totalRows" select="$totalRows" />
</xsl:call-template>
<xsl:value-of select="organizer/component/observation/value/#value"/>
</td>
The crucial thing that makes all this work is the fact that you're applying templates to self::*|$otherEntries, so a call to position() within the applied template gives the column number (the position within this node list), not the node's original position in its parent.
To make the date appear only once the first time it is encountered, you can define a key and use a trick related to the "Muenchian grouping" technique. Declare
<xsl:key name="effectiveTimeByDate" match="effectiveTime"
use="substring-before(#value, ' ')" />
and then you can check whether this is the first occurrence of a particular date in the document using
<td width="75">
<xsl:call-template name="bgcolor">
<xsl:with-param name="rowNum" select="$rowNum" />
<xsl:with-param name="totalRows" select="$totalRows" />
</xsl:call-template>
<xsl:if test="
generate-id(organizer/component/observation/effectiveTime)
= generate-id(key('effectiveTimeByDate', substring-before(
organizer/component/observation/effectiveTime/#value, ' '))[1])">
<xsl:call-template name="StripTime">
<xsl:with-param name="DateTime" select="organizer/component/observation/effectiveTime/#value" />
</xsl:call-template>
</xsl:if>
</td>
(you may be able to scrap the StripTime template entirely and just use the substring-before call the same as I'm using in the key)

Related

XSL, SUM & Multiply with Condition

Im doing an assignment for University (so im new to XSL coding) in making a quasi ecommerce site, and will provide as much detail as i can so it makes sense.
Sample XML Data:
<Items>
<Item>
<ItemID>50001</ItemID>
<ItemName>Samsung Galaxy S4</ItemName>
<ItemPrice>629</ItemPrice>
<ItemQty>14</ItemQty>
<ItemDesc>4G Mobile</ItemDesc>
<QtyHold>0</QtyHold>
<QtySold>1</QtySold>
</Item>
<Item>
<ItemID>50002</ItemID>
<ItemName>Samsung Galaxy S5</ItemName>
<ItemPrice>779</ItemPrice>
<ItemQty>21</ItemQty>
<ItemDesc>4G Mobile</ItemDesc>
<QtyHold>0</QtyHold>
<QtySold>1</QtySold>
</Item>
</Items>
Website
So the process is, when a person clicks 'Add to Cart' in the top Table, the ItemQty is decreased by 1 on the ItemQty in the XML, while it increases by 1 in the QtyHold in the XML. (QtyHold represents what has been added to the shopping Cart. Thus if QtyHold is >0 then its been added to the Cart)
My problem refers to the 2nd Table (code below), where the Total figure works - only if dealing with 1 Item. Thus, if Item Number '50001' is added a 2nd time, the Total wont change.
<xsl:template match="/">
<fieldset>
<legend>Shopping Cart</legend>
<BR />
<table border="1" id="CartTable" align="center">
<tr><th>Item Number</th>
<th>Price</th>
<th>Quantity</th>
<th>Remove</th></tr>
<xsl:for-each select="/Items/Item[QtyHold > 0]">
<tr><td><xsl:value-of select="ItemID"/></td>
<td>$<xsl:value-of select="ItemPrice"/></td>
<td><xsl:value-of select="QtyHold"/></td>
<td><button onclick="addtoCart({ItemID}, 'Remove')">Remove from Cart</button></td> </tr>
</xsl:for-each>
<tr><td ALIGN="center" COLSPAN="3">Total:</td><td>$<xsl:value-of select="sum(//Item[QtyHold >0]/ItemPrice)"/></td></tr>
</table>
<BR />
<button onclick="Purchase()" class="submit_btn float_l">Confirm Purchase</button>
<button onclick="CancelOrder()" class="submit_btn float_r">Cancel Order</button>
</fieldset>
</xsl:template>
</xsl:stylesheet>
So what needs to happen is within the following code, while it checks if the QtyHold is greater than 0 (which would mean its in the shopping Cart) & to sum these values, it also needs to multiply QtyHold & ItemPrice.
<xsl:value-of select="sum(//Item[QtyHold >0]/ItemPrice)"/>
I tried many variations of Code like this below... but can't seem to make anything work.
select="sum(//Item[QtyHold >0]/ItemPrice)/(QtyHold*ItemPrice"/>
If you are using XSLT 2.0, the expression you could use would be this:
<xsl:value-of select="sum(//Item[QtyHold >0]/(ItemPrice * QtyHold))"/>
However, in XSLT 1.0 that is not allowed. Instead, you could achieve the result you need with an extension function. In particular the "node-set" function. First you would create a variable like this, in which you construct new nodes holding each item total
<xsl:variable name="itemTotals">
<xsl:for-each select="//Item[QtyHold >0]">
<total>
<xsl:value-of select="ItemPrice * QtyHold" />
</total>
</xsl:for-each>
</xsl:variable>
Ideally, you would like to do sum($itemTotals/total), but this won't work, because itemTotals is a "Result Tree Fragment" and the sum function only accepts a node-set. So you use the node-set extension function to convert it. First declare this namespace in your XSLT...
xmlns:exsl="http://exslt.org/common"
Then, your sum function would look like this:
<xsl:value-of select="sum(exsl:node-set($itemTotals)/total)"/>
Alternatively, if you couldn't even use an extension function, you could use the "following-sibling" approach, to select each Item at a time, and keep a running total. So, you would have a template like this:
<xsl:template match="Item" mode="sum">
<xsl:param name="runningTotal" select="0" />
<xsl:variable name="newTotal" select="$runningTotal + ItemPrice * QtyHold" />
<xsl:variable name="nextItem" select="following-sibling::Item[1]" />
<xsl:choose>
<xsl:when test="$nextItem">
<xsl:apply-templates select="$nextItem" mode="sum">
<xsl:with-param name="runningTotal" select="$newTotal" />
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$newTotal" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
And to call it, to get the sum, you just start off by selecting the first node
<xsl:apply-templates select="(//Item)[1]" mode="sum" />
Try this XSLT which demonstrates the various approaches
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="exsl">
<xsl:output method="html" indent="yes" />
<xsl:template match="/">
<table border="1" id="CartTable" align="center">
<tr><th>Item Number</th>
<th>Price</th>
<th>Quantity</th>
</tr>
<xsl:for-each select="/Items/Item[QtyHold > 0]">
<tr>
<td><xsl:value-of select="ItemID"/></td>
<td>$<xsl:value-of select="ItemPrice"/></td>
<td><xsl:value-of select="QtyHold"/></td>
</tr>
</xsl:for-each>
<tr>
<td ALIGN="center" COLSPAN="2">Total:</td>
<xsl:variable name="itemTotals">
<xsl:for-each select="//Item[QtyHold >0]">
<total>
<xsl:value-of select="ItemPrice * QtyHold" />
</total>
</xsl:for-each>
</xsl:variable>
<td>
<!-- XSLT 2.0 only: $<xsl:value-of select="sum(//Item[QtyHold >0]/(ItemPrice * QtyHold))"/>-->
$<xsl:value-of select="sum(exsl:node-set($itemTotals)/total)"/>
$<xsl:apply-templates select="(//Item)[1]" mode="sum" />
</td>
</tr>
</table>
</xsl:template>
<xsl:template match="Item" mode="sum">
<xsl:param name="runningTotal" select="0" />
<xsl:variable name="newTotal" select="$runningTotal + ItemPrice * QtyHold" />
<xsl:variable name="nextItem" select="following-sibling::Item[1]" />
<xsl:choose>
<xsl:when test="$nextItem">
<xsl:apply-templates select="$nextItem" mode="sum">
<xsl:with-param name="runningTotal" select="$newTotal" />
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$newTotal" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
As a final thought, why don't you just a new Total element to each Item element in your XML. Initially, it would be set to 0, like QtyHold. Then, when you increment QtyHold by 1, by what ever process you do, you can also increment Total by the amount held in ItemPrice. That way, you can just sum this Total node to get the overall total, without the need for extension functions or recursive templates.

xsl 1.0 Why won't a node match return data

I'm using the following xml information:
<section>
<...>
</section>
<section>
<templateId root="2.16.840.1.113883.10.20.22.2.10" />
<text>
<table id="Appointments">
<tr>
<td id="heading">Appointments</td>
</tr>
<tr>
<td id="content">No future appointments scheduled.</td>
</tr>
</table>
<br />
<table id="Referrals">
<tr>
<td id="heading">Referrals</td>
</tr>
<tr>
<td id="content">No referrals available.</td>
</tr>
</table>
<br />
</text>
<section>
<section>
<...>
</section>
There are multiple section nodes (with their own child nodes, including templateId) within the document. I'm having trouble with this one so I wanted to be specific in the xml information.
and in my xslt file I want to get one particular table out. I'm referencing it the following way (I'm trying to use templates and I'm new to XSL so please bear with me)
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//section[templateId/#root='2.16.840.1.113883.10.20.22.2.17']/text/table[#id='Appointments']" />
</xsl: template>
<xsl:template match="section[templateId/#root='2.16.840.1.113883.10.20.22.2.17']/text/table[#id='Appointments']">
<div style="float: left; width: 50%;">
<span style="font-weight: bold;">
<xsl:value-of select="tr/td[#id='heading']"/>:
</span>
<br />
<xsl:call-template name="replace">
<xsl:with-param name="string" select="tr/td[#id='content']"/>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($string,'
')">
<xsl:value-of select="substring-before($string,'
')"/>
<br/>
<xsl:call-template name="replace">
<xsl:with-param name="string" select="substring-after($string,'
')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
In this particular xml example, the output should be:
Appointments:
No future appointments scheduled.
I'm thinking the match and select need some tweaking but not sure what part.
Also, if the template can be tweaked so that I could pass a parameter with the table/#id value so that I could reuse this one template for a couple of items,that would be even more beneficial (the output for referrals and appointments that are in this example would be the same).
Thanks for any help
This is your XML section root attribute (cut and paste from your XML):
root="2.16.840.1.113883.10.20.22.2.10"
This is your test XSL:
root='2.16.840.1.113883.10.20.22.2.17'
Of course they do not match, one ends with "10", the other with "17"
Changing the data to "17" and correcting the other errors in my comments yields:
<div style="float: left; width: 50%;"><span style="font-weight: bold;">Appointments:
</span><br>No future appointments scheduled.
</div

How can I produce columnar output from 2-level grouping in XML?

I have the following xml:
input
<page>
<group category="cat1">
<item fileunder="#">.45 colt</item>
<item fileunder="#">8 queens</item>
<item fileunder="#">9 lives</item>
<item fileunder="#">99 bottles of beer</item>
<item fileunder="A">An innocent man</item>
<item fileunder="A">Academy awards</item>
<item fileunder="B">Before the dawn</item>
</group>
<group category="cat2">
<item fileunder="R">Rows of houses</item>
</group>
</page>
The input items are already sorted.
desired output
I want to produce a 3-column HTML table for every group, with a subheading (a 3-column spanning cell) for each distinct fileunder, optimally presented in a top-down, then-next-column (the items are already sorted):
<table>
<tr><td colspan="3">#</td></tr>
<tr><td>.45 colt</td><td>9 lives</td><td>99 bottles of beer</td></tr>
<tr><td>8 queens</td></tr>
<tr><td colspan="3">A</td></tr>
<tr><td>An innocent man</td><td>Academy awards</td></tr>
<tr><td colspan="3">B</td></tr>
<tr><td>Before the dawn</td></tr>
</table>
<table>
<tr><td colspan="3">R</td></tr>
<tr><td>Rows of houses</td></tr>
</table>
I can live if the items are presented as left-to-right, then-next-row.
What I have so far is:
current xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="itm_grp" match="/page/group/item" use="concat(../#category,':',#fileunder)"/>
<xsl:template match="page/group">
<table>
<xsl:for-each select="item[.=key('itm_grp',concat(../#category,':',#fileunder))[1]]">
<tr><td colspan="3"><xsl:value-of select="#fileunder"/></td></tr>
<xsl:variable name="nodeset" select="key('itm_grp',concat(../#category,':',#fileunder))"/>
<xsl:for-each select="$nodeset[position() mod 3=1]">
<tr>
<td><xsl:value-of select="."/></td>
<td><xsl:value-of select="following-sibling::item[1]"/></td>
<td><xsl:value-of select="following-sibling::item[2]"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
which produces a left-to-right, then-next-row output (non-optimal); however, the following-sibling selects produce a “bleed-through” effect:
#
.45 colt 8 queens 9 lives
99 bottles of beer An innocent man Academy awards
A
An innocent man Academy awards Before the dawn
B
Before the dawn
R
Rows of houses
As you can see, fileunder # has two A items, and fileunder A has one B item.
So, my question is:
How can I produce the desired output (column-wise)?
If I can't do that, how can I have the row-wise output avoiding the “bleeding”?
Please note that I have very little experience with XSLT, so if my code is blatantly inefficient/idiotic/whatever, please feel free to educate me by replacing all of it!
NB: XSLT version 1, so apparently no index-of function is available.
There is a slight contradiction between your narrative and your listed expected output. You have asked for top-down, then left-right column fill order, which you have so in the listing for the non-empty values, but not for the empties. This spatial order implies that a whole column must be filled out before the next column can begin. I have assumed that your listing was a mistake and what your really want in output is ...
<table>
<tr>
<td colspan="3">#</td>
</tr>
<tr>
<td>.45 colt</td>
<td>9 lives</td>
<td>&npsp;</td>
</tr>
<tr>
<td>8 queens</td>
<td>99 bottles of beer</td>
<td>&npsp;</td>
</tr>
<tr>
<td colspan="3">A</td>
</tr>
<tr>
<td>An innocent man</td>
<td>Academy awards</td>
<td>&npsp;</td>
</tr>
<tr>
<td colspan="3">B</td>
</tr>
<tr>
<td>Before the dawn</td>
<td>&npsp;</td>
<td>&npsp;</td>
</tr>
</table>
<table>
<tr>
<td colspan="3">R</td>
</tr>
<tr>
<td>Rows of houses</td>
<td>&npsp;</td>
<td>&npsp;</td>
</tr>
</table>
... which is consistent top-down, then left-right column fill order.
This XSLT 1.0 style-sheet...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="about:legacy-compat" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="kItemByFile" match="item" use="concat(../#category,':',#fileunder)"/>
<xsl:template match="/">
<html lang="en">
<head><title>Songs</title></head>
<body>
<xsl:apply-templates select="*/group" />
</body>
</html>
</xsl:template>
<xsl:template match="group">
<xsl:variable name="cat" select="concat(#category,':')" />
<table>
<xsl:apply-templates select="item[
generate-id() = generate-id(key('kItemByFile',concat($cat,#fileunder))[1])]"
mode="group-head" />
</table>
</xsl:template>
<xsl:template match="item" mode="group-head">
<xsl:variable name="items"
select="key('kItemByFile',concat(../#category,':',#fileunder))" />
<xsl:variable name="row-count" select="ceiling( count($items) div 3)" />
<tr><td colspan="3"><xsl:value-of select="#fileunder" /></td></tr>
<xsl:for-each select="$items[position() <= $row-count]">
<xsl:variable name="pos" select="position()" />
<xsl:apply-templates select="." mode="row">
<xsl:with-param name="items" select="$items" />
<xsl:with-param name="row" select="$pos" />
<xsl:with-param name="row-count" select="$row-count" />
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="item" mode="row">
<xsl:param name="items" select="/.." />
<xsl:param name="row" select="1" />
<xsl:param name="row-count" select="1" />
<tr>
<xsl:apply-templates select="
$items[(position() mod $row-count) = ($row mod $row-count)]" mode="td" />
<xsl:variable name="full-cols" select="floor((count($items) div $row-count))" />
<xsl:variable name="part-col" select="number($row <
((count($items) mod $row-count) + 1))" />
<xsl:variable name="empties" select="3 - ($full-cols + $part-col)" />
<xsl:for-each select="(document('')/*/*)[position() <= $empties]">
<xsl:call-template name="empty-cell" />
</xsl:for-each>
</tr>
</xsl:template>
<xsl:template match="item" mode="td">
<td><xsl:value-of select="." /></td>
</xsl:template>
<xsl:template name="empty-cell">
<td> </td>
</xsl:template>
</xsl:stylesheet>
...when applied to this input...
<page>
<group category="cat1">
<item fileunder="#">.45 colt</item>
<item fileunder="#">8 queens</item>
<item fileunder="#">9 lives</item>
<item fileunder="#">99 bottles of beer</item>
<item fileunder="A">An innocent man</item>
<item fileunder="A">Academy awards</item>
<item fileunder="B">Before the dawn</item>
</group>
<group category="cat2">
<item fileunder="R">Rows of houses</item>
</group>
</page>
...yields...
<!DOCTYPE html SYSTEM "about:legacy-compat">
<html lang="en">
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Songs</title>
</head>
<body>
<table>
<tr>
<td colspan="3">#</td>
</tr>
<tr>
<td>.45 colt</td>
<td>9 lives</td>
<td> </td>
</tr>
<tr>
<td>8 queens</td>
<td>99 bottles of beer</td>
<td> </td>
</tr>
<tr>
<td colspan="3">A</td>
</tr>
<tr>
<td>An innocent man</td>
<td>Academy awards</td>
<td> </td>
</tr>
<tr>
<td colspan="3">B</td>
</tr>
<tr>
<td>Before the dawn</td>
<td> </td>
<td> </td>
</tr>
</table>
<table>
<tr>
<td colspan="3">R</td>
</tr>
<tr>
<td>Rows of houses</td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
</html>
Note
For the empty cells in the output, when viewing the lexical HTML, you will get either or the literal white space equivalent. It is XSLT processor implementation dependant, but should not cause you any concern because it is model-equivalent.
Easiest way to fix that:
<xsl:variable name="header" select="#fileunder"/>
...
<xsl:value-of select="following-sibling::item[#fileunder=$header][1]"/>
<xsl:value-of select="following-sibling::item[#fileunder=$header][2]"/>

Getting an attribute via another attribute in XSL

I stink at XSLT, so I'm not sure how to approach this... I'm getting a feed from the EQ2 database. The XML looks like this:
<guilds limit="1" returned="1">
<guild accounts="3" alignment="0" dateformed="1127855265" guildid="111" guildstatus="0" id="1111111111" last_update="1326986410" level="11" name="MyGuild" version="1" world="Permafrost" worldid="202">
<ranks>
<rank id="0" name="Leader"/>
<rank id="1" name="Senior Officer"/>
<rank id="2" name="Officer"/>
<rank id="3" name="Senior Member"/>
<rank id="4" name="Member"/>
<rank id="5" name="Junior Member"/>
<rank id="6" name="Initiate"/>
<rank id="7" name="Recruit"/>
</ranks>
<members>
<member dbid="123456" rank="0" name="Dude1"/>
<member dbid="123457" rank="1" name="Dude2"/>
<member dbid="123458" rank="2" name="Dude3"/>
<member dbid="123459" rank="4" name="Dude4"/>
<member dbid="123460" rank="4" name="Dude5"/>
<member dbid="123461" rank="4" name="Dude6"/>
</members>
<events/>
</guild>
</guilds>
I'm trying to add the ranks into a table. The XSL (version 1) snippet for it is as follows, but adding the name from the rank isn't working properly - I know this part is wrong:
<xsl:value-of select="rank/rank[#id=1]/#name"/>
So, can I get some help making it work and maybe an idea of how to shorten it?
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0" indent="yes"/>
<xsl:template match="/">
<table width="100%" cellspacing="0" cellpadding="0" id="eq2roster" align="center">
<thead>
<tr>
<td colspan="4" class="eq2CharacterHeader">Character</td>
<td colspan="3" class="eq2TradeskillsHeader">Tradeskills</td>
</tr>
<tr class="ForumCategoryHeader">
<th class="eq2NameHeader">Name</th>
<th class="eq2RankHeader">Rank</th>
<th class="eq2ClassHeader">Class</th>
<th class="eq2LevelHeader">Level</th>
<th class="eq2ArtisanHeader">Artisan</th>
<th class="eq2ArtisanLevelHeader">Level</th>
<th class="eq2SecondaryHeader">Secondary</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="guilds/guild/members/member">
<xsl:if test="#id > 1">
<tr>
<td class="eq2Name">
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:text>http://eq2players.station.sony.com/</xsl:text>
<xsl:value-of select="concat(normalize-space(/guilds/guild/#world), '/')"/>
<xsl:value-of select="concat(normalize-space(#name), '/')"/>
</xsl:attribute>
<xsl:attribute name="target">
<xsl:text>_blank</xsl:text>
</xsl:attribute>
<xsl:value-of select="normalize-space(#name)"/>
</xsl:element>
</td>
<xsl:element name="td">
<xsl:attribute name="class">
<xsl:text>eq2Rank eq2rank-</xsl:text>
<xsl:value-of select="translate(normalize-space(guild/#rank),' ','')"/>
</xsl:attribute>
<xsl:choose>
<xsl:when test="member/#rank = 1"><span class = "rank1"><xsl:value-of select="ranks/rank[#id=1]/#name"/></span></xsl:when>
<xsl:when test="member/#rank = 2"><span class = "rank2"><xsl:value-of select="ranks/rank[#id=2]/#name"/></span></xsl:when>
<xsl:when test="member/#rank = 3"><span class = "rank3"><xsl:value-of select="ranks/rank[#id=3]/#name"/></span></xsl:when>
<xsl:when test="member/#rank = 4"><span class = "rank4"><xsl:value-of select="ranks/rank[#id=4]/#name"/></span></xsl:when>
<xsl:when test="member/#rank = 5"><span class = "rank5"><xsl:value-of select="ranks/rank[#id=5]/#name"/></span></xsl:when>
<xsl:when test="member/#rank = 6"><span class = "rank6"><xsl:value-of select="ranks/rank[#id=6]/#name"/></span></xsl:when>
<xsl:when test="member/#rank = 7"><span class = "rank7"><xsl:value-of select="ranks/rank[#id=7]/#name"/></span></xsl:when>
</xsl:choose>
</xsl:element>
<xsl:element name="td">
<xsl:attribute name="class">
<xsl:text>eq2Class eq2</xsl:text>
<xsl:value-of select="translate(normalize-space(type/#class),' ','')"/>
</xsl:attribute>
<xsl:value-of select="type/#class"/>
</xsl:element>
<td class="eq2level"><xsl:value-of select="type/#level"/></td>
<xsl:element name="td">
<xsl:attribute name="class">
<xsl:text>eq2ArtisanClass eq2</xsl:text>
<xsl:value-of select="translate(normalize-space(tradeskills/tradeskill/#class),' ','')"/>
</xsl:attribute>
<xsl:value-of select="tradeskills/tradeskill/#class"/>
</xsl:element>
<td class="eq2ArtisanLevel"><xsl:value-of select="tradeskills/tradeskill/#level"/></td>
<xsl:element name="td">
<xsl:attribute name="class">
<xsl:text>eq2Secondary eq2</xsl:text>
<xsl:value-of select="translate(normalize-space(secondarytradeskills/secondarytradeskill/#name),' ','')"/>
</xsl:attribute>
<xsl:value-of select="secondarytradeskills/secondarytradeskill/#name"/>
</xsl:element>
</tr>
</xsl:if>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
The following expression associates a member with its corresponding rank using current() (assuming that the context node is the member in question):
../../ranks/rank[#id=current()/#rank]/#name
Full example:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="member">
<td class="eq2Rank eq2rank-{guild/#rank}">
<span class="rank{#rank}">
<xsl:value-of
select="../../ranks/rank[#id=current()/#rank]/#name"/>
</span>
</td>
</xsl:template>
</xsl:stylesheet>
Output (based on the originally posted sample XML):
<td class="eq2Rank eq2rank-">
<span class="rank0">Leader</span>
</td>
<td class="eq2Rank eq2rank-">
<span class="rank1">Senior Officer</span>
</td>
<td class="eq2Rank eq2rank-">
<span class="rank2">Officer</span>
</td>
<td class="eq2Rank eq2rank-">
<span class="rank4">Member</span>
</td>
<td class="eq2Rank eq2rank-">
<span class="rank4">Member</span>
</td>
<td class="eq2Rank eq2rank-">
<span class="rank4">Member</span>
</td>
Not sure if you can do something like this but it might be worth a shot. In the context of a single member
<xsl:value = "//ranks/rank[#id = ./#rank]/#name"/>
It uses X-Path to jump to the top and look for the ranks node, find the rank element whose ID attribute equals the rank attribute of the member, and grabs the name attribute
It's been a few years since I had to XSL like that, but should allow you to get rid of the XSL:choose block
I'm not really sure if I get the question.
Asume that you have the right rankid you probably want to use variable
<xsl:variable name="rank" select="translate(normalize-space(guild/#rank),' ','')"/>
<span class = "rank{$rank}"><xsl:value-of select="ranks/rank[#id = $rank]/#name"/></span>

How to XPath sum() all previous nodes in a XSL for-each loop?

The source XML (this is just foobar data, in reality it is thousands of rows wich can be both positive and negative):
<accounting>
<entry id="1">
<accounting_date>2010-10-29</accounting_date>
<transfer_date>2010-10-29</transfer_date>
<description>Start balance</description>
<vat>0</vat>
<sum>87287</sum>
</entry>
<entry id="2">
<accounting_date>2011-01-24</accounting_date>
<transfer_date>2011-02-17</transfer_date>
<description>Bill 1</description>
<vat>175</vat>
<sum>875</sum>
</entry>
<entry id="3">
<accounting_date>2011-01-31</accounting_date>
<transfer_date>2011-01-18</transfer_date>
<description>Bill 2</description>
<vat>350</vat>
<sum>1750</sum>
</entry>
</accounting>
I want to transform this XML to an HTML table to display to the user. Most of the transformation is just putting values in the right places, but the balance-field is giving me headache.
My XSLT (that does not work):
<table>
<tr>
<th>Accounting date</th>
<th>Description</th>
<th>Sum</th>
<th>Balanche</th>
</tr>
<xsl:for-each select="/accounting/entry">
<tr>
<td><xsl:value-of select="accounting_date" /></td>
<td><xsl:value-of select="description" /></td>
<td><xsl:value-of select="sum" /></td>
<td><xsl:value-of select="sum(../entry[position() < current()/position()]/sum)" /></td><!-- This XPath is the problem! -->
</tr>
</xsl:for-each>
</table>
Expected result:
<table>
<tr>
<th>Accounting date</th>
<th>Description</th>
<th>Sum</th>
<th>Balanche</th>
</tr>
<tr>
<td>2010-10-29</td>
<td>Start balance</td>
<td>87287</td>
<td>87287</td>
</tr>
<tr>
<td>2011-01-24</td>
<td>Bill 1</td>
<td>875</td>
<td>88162</td>
</tr>
<tr>
<td>2011-01-31</td>
<td>Bill 2</td>
<td>1750</td>
<td>89912</td>
</tr>
</table>
Chrome is blank, and Firefox gives me:
Error loading stylesheet: XPath parse failure: Name or Nodetype test expected:
I'm stuck, please help. :)
The best solution might depend a bit on whether you are using XSLT 1.0 or XSLT 2.0. You really need to say, since at present there's a roughly even mix of both in use in the field. (It seems you're running it in the browser, which suggests you want a 1.0 solution, so that's what I've given you).
But either way, recursion is your friend. In this case, "sibling recursion" where you write a template to process an entry, and it does apply-templates to process the next entry, passing the total so far as a parameter: something like this
<xsl:template match="entry">
<xsl:param name="total-so-far" select="0"/>
<tr>
<td><xsl:value-of select="accounting_date" /></td>
<td><xsl:value-of select="description" /></td>
<td><xsl:value-of select="sum" /></td>
<td><xsl:value-of select="$total-so-far + sum"/></td><
</tr>
<xsl:apply-templates select="following-sibling::entry[1]">
<xsl:with-param name="total-so-far" select="$total-so-far + sum"/>
</xsl:apply-templates>
</xsl:template>
Then you need to start the process off with
<xsl:template match="accounting">
<table>
<xsl:apply-templates select="entry[1]"/>
</table>
</xsl:template>
If there are thousands of rows then this could cause stack overflow in an XSLT processor that doesn't do tail call optimisation. I've no idea whether the XSLT processors in today's browsers implement this optimisation or not.
Alternatively you can use the preceding-sibling axes
<table>
<tr>
<th>Accounting date</th>
<th>Description</th>
<th>Sum</th>
<th>Balanche</th>
</tr>
<xsl:for-each select="/accounting/entry">
<tr>
<td>
<xsl:value-of select="accounting_date" />
</td>
<td>
<xsl:value-of select="description" />
</td>
<td>
<xsl:value-of select="sum" />
</td>
<td>
<xsl:value-of select="sum(preceding-sibling::*/sum)+sum" />
</td>
</tr>
</xsl:for-each>
</table>
In addition to the correct answer by #Michael Kay, here is a general template/function from FXSL to use for computing running totals. Its DVC variant will never (for practical purposes) crash due to stack overflow. With DVC (Divide and Conquer) recursion, processing a sequence of 1000000 (1M) items requires maximum stack depth of only 19.
Here is an example of using the scanl template:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/"
xmlns:myAdd="f:myAdd"
xmlns:myParam="f:myParam"
>
<xsl:import href="scanlDVC.xsl"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<myAdd:myAdd/>
<myParam:myParam>0</myParam:myParam>
<xsl:template match="/">
<xsl:variable name="vFun" select="document('')/*/myAdd:*[1]"/>
<xsl:variable name="vZero" select="document('')/*/myParam:*[1]"/>
<xsl:call-template name="scanl">
<xsl:with-param name="pFun" select="$vFun"/>
<xsl:with-param name="pQ0" select="$vZero" />
<xsl:with-param name="pList" select="/*/num"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="myAdd:*" mode="f:FXSL">
<xsl:param name="pArg1" select="0"/>
<xsl:param name="pArg2" select="0"/>
<xsl:value-of select="$pArg1 + $pArg2"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the following XML file:
<nums>
<num>01</num>
<num>02</num>
<num>03</num>
<num>04</num>
<num>05</num>
<num>06</num>
<num>07</num>
<num>08</num>
<num>09</num>
<num>10</num>
</nums>
the correct result (running totals) is produced:
<el>0</el>
<el>1</el>
<el>3</el>
<el>6</el>
<el>10</el>
<el>15</el>
<el>21</el>
<el>28</el>
<el>36</el>
<el>45</el>
<el>55</el>
Using it for the provided XML document:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/"
xmlns:myAdd="f:myAdd"
xmlns:myParam="f:myParam"
>
<xsl:import href="scanlDVC.xsl"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<myAdd:myAdd/>
<myParam:myParam>0</myParam:myParam>
<xsl:template match="/">
<xsl:variable name="vFun" select="document('')/*/myAdd:*[1]"/>
<xsl:variable name="vZero" select="document('')/*/myParam:*[1]"/>
<xsl:call-template name="scanl">
<xsl:with-param name="pFun" select="$vFun"/>
<xsl:with-param name="pQ0" select="$vZero" />
<xsl:with-param name="pList" select="/*/*/sum"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="myAdd:*" mode="f:FXSL">
<xsl:param name="pArg1" select="0"/>
<xsl:param name="pArg2" select="0"/>
<xsl:value-of select="$pArg1 + $pArg2"/>
</xsl:template>
</xsl:stylesheet>
and the correct result is produced::
<el>0</el>
<el>87287</el>
<el>88162</el>
<el>89912</el>