Monday, 9 September 2013

xslt 1.0 - How to format 2 column table from multiple nodes

xslt 1.0 - How to format 2 column table from multiple nodes

I've got an XML file that has multiple sections that are being displayed
and for one of the sections, the layout needs to be a two column table
(with alternating background colors per row of the table). I think I have
the template and everything set up right, but for some reason, nothing is
getting returned when I know there's data.
Here 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>
This is my XSL:
<xsl:if
test="//section[templateId/@root='2.16.840.1.113883.10.20.22.2.3.1']!=''">
<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="entry[position() mod 2 = 1]">
<tr>
<xsl:apply-templates
select=".|following-sibling::section[templateId/@root='2.16.840.1.113883.10.20.22.2.3.1']/entry[position()
&lt; 2]" />
</tr>
</xsl:for-each>
</table>
</div>
</xsl:if>
<xsl:template
match="section[templateId/@root='2.16.840.1.113883.10.20.22.2.3.1']/entry">
<td style="width:85px; border-bottom:1px solid #CCCCCC; border-right:1px
solid #CCCCCC; vertical-align:top;">
<xsl:call-template name="StripTime">
<xsl:with-param name="DateTime"
select="organizer/component/observation/effectiveTime/@value"
/>
</xsl:call-template>
</td>
<td>
<xsl:value-of
select="organizer/component/observation/code/@displayName"/>
&#160;
</td>
<td>
<xsl:value-of select="organizer/component/observation/value/@value"/>
&#160;
</td>
<xsl:choose>
<xsl:when test="position() != last()">
<td>
<xsl:value-of
select="organizer/component/observation/referenceRange/observationRange/text"/>
&#160;
</td>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="(position() &lt; 2)">
<td colspan="5">
<xsl:value-of
select="organizer/component/observation/referenceRange/observationRange/text"/>
&#160;
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:value-of
select="organizer/component/observation/referenceRange/observationRange/text"/>
&#160;
</td>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="StripTime">
<xsl:param name="DateTime" />
<xsl:value-of select="substring-before($DateTime, ' ')" />
</xsl:template>
I'm not sure if I've got the match wrong in one or many places to test the
"if" and/or apply the template. I have a similar apply-template in other
areas and they work fine when I do apply-template="//..." and then
match="..." removing the "//" in the template when it's defined.
I've jumped into the deep end on XSL and have picked up a lot, but this
one has me stumped so any help would be very greatly appreciated.

No comments:

Post a Comment