jjasper0729
jjasper0729

Reputation: 295

XSLT 1.0 - getting two separate nodes/@values to display in table

I'm trying to get two different id values in xml nodes to show up in a table format. The xml is as follows:

<root>
<section>
  <templateId root="2.16.840.1.113883.10.20.22.2.10" />
  <text>
    <table id="ScheduledTests">
      <tr>
        <td id="heading">Scheduled Tests</td>
        <td>Order Date</td>
      </tr>
      <tr>
        <td id="content">Lab for JJ</td>
        <td id="testdate">9/20/2013 12:00:00 AM</td>
      </tr>
      <tr>
        <td id="content">Rad for JJ</td>
        <td id="testdate">9/20/2013 12:00:00 AM</td>
      </tr>
      <tr>
        <td id="content">Lab for JJ</td>
        <td id="testdate">9/11/2013 12:00:00 AM</td>
      </tr>
      <tr>
        <td id="content">Rad for JJ</td>
        <td id="testdate">9/11/2013 12:00:00 AM</td>
      </tr>
      <tr>
        <td id="content">Referral for JJ</td>
        <td id="testdate">9/11/2013 12:00:00 AM</td>
      </tr>
      <tr>
        <td id="content">Lab for JJ</td>
        <td id="testdate">9/6/2013 12:00:00 AM</td>
      </tr>
      <tr>
        <td id="content">Rad for JJ</td>
        <td id="testdate">9/6/2013 12:00:00 AM</td>
      </tr>
    </table>
  </text>
</section>
<section>
  <...>
</section>
<section>
  <...>
</section>
</root>

This is just the xml snippet. There are other blocks under the root so is not the actual root node.

It is set up in a table format, but unfortunately, I can't just read that in and be done with it. I have the following in my xslt:

<xsl:template match="/">
  <!-- Scheduled Tests -->
  <xsl:apply-templates select="//section[templateId/@root='2.16.840.1.113883.10.20.22.2.10']/text/table[@id='ScheduledTests']"/>
</xsl:template>

<!-- template for scheduled tests -->
<xsl:template match="section[templateId/@root='2.16.840.1.113883.10.20.22.2.10']/text/table[@id='ScheduledTests']">
  <div style="padding: 5px; border-top: 1px solid #000000;">
    <span style="font-weight: bold;">
      <xsl:value-of select="tr/td[@id='heading']"/>:
    </span>
    <br />
    <xsl:for-each select="tr/td[@id='content']">
            <div>
                <xsl:choose>
                    <xsl:when test="position() mod 2 = 0">
                        <xsl:attribute name="style">
                            <xsl:text>padding: 2px 0px 2px 0px; background-color: #dcdcdc;</xsl:text>
                        </xsl:attribute>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:attribute name="style">
                            <xsl:text>padding: 2px 0px 2px 0px;</xsl:text>
                        </xsl:attribute>
                    </xsl:otherwise>
                </xsl:choose>
                <table border="0" cellspacing="0" cellpadding="0" width="100%">
                    <tr>
                        <td style="font-size: 11px;  text-align: left;"><xsl:value-of select="."/></td>
                        <td style="font-size: 11px;  text-align: right;"><xsl:value-of select="substring-before(../../tr/td[@id='testdate'], ' ')"/></td>
                    </tr>
                </table>
            </div>
    </xsl:for-each>
  </div>
</xsl:template>

This is giving me the correct number of rows, but the @id='testdate' is not correct after the first row that comes back:

<div style="padding: 5px; border-top: 1px solid #000000;" xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns="http://www.w3.org/1999/xhtml">
  <span style="font-weight: bold;">Scheduled Tests:</span><br />
  <div style="padding: 2px 0px 2px 0px;">
    <table border="0" cellspacing="0" cellpadding="0" width="100%">
      <tr><td style="font-size: 11px;  text-align: left;">Lab for JJ</td>
      <td style="font-size: 11px;  text-align: right;">9/20/2013</td></tr>
    </table>
  </div>
  <div style="padding: 2px 0px 2px 0px; background-color: #dcdcdc;">
    <table border="0" cellspacing="0" cellpadding="0" width="100%">
      <tr><td style="font-size: 11px;  text-align: left;">Rad for JJ</td>
      <td style="font-size: 11px;  text-align: right;">9/20/2013</td></tr>
    </table>
  </div>
  <div style="padding: 2px 0px 2px 0px;">
    <table border="0" cellspacing="0" cellpadding="0" width="100%">
      <tr><td style="font-size: 11px;  text-align: left;">Lab for JJ</td>
      <td style="font-size: 11px;  text-align: right;">9/20/2013</td></tr>
    </table>
  </div>
  <div style="padding: 2px 0px 2px 0px; background-color: #dcdcdc;">
    <table border="0" cellspacing="0" cellpadding="0" width="100%">
      <tr><td style="font-size: 11px;  text-align: left;">Rad for JJ</td>
      <td style="font-size: 11px;  text-align: right;">9/20/2013</td></tr>
    </table>
  </div>
  <div style="padding: 2px 0px 2px 0px;">
    <table border="0" cellspacing="0" cellpadding="0" width="100%">
      <tr><td style="font-size: 11px;  text-align: left;">Referral for JJ</td>
      <td style="font-size: 11px;  text-align: right;">9/20/2013</td></tr>
    </table>
  </div>
  <div style="padding: 2px 0px 2px 0px; background-color: #dcdcdc;">
    <table border="0" cellspacing="0" cellpadding="0" width="100%">
      <tr><td style="font-size: 11px;  text-align: left;">Lab for JJ</td>
      <td style="font-size: 11px;  text-align: right;">9/20/2013</td></tr>
    </table>
  </div>
  <div style="padding: 2px 0px 2px 0px;">
    <table border="0" cellspacing="0" cellpadding="0" width="100%">
      <tr><td style="font-size: 11px;  text-align: left;">Rad for JJ</td>
      <td style="font-size: 11px;  text-align: right;">9/20/2013</td></tr>
    </table>
  </div>
</div>

as displayed, there are dates for every pair, but the value corresponds to the first @id='testdate' and doesn't sequence through. Can't figure out what I'm doing wrong so that they go through. Again, I wish I could just display the table as is, but I can't for the output I need.

Thanks in advance for any assistance.

Upvotes: 2

Views: 222

Answers (1)

jjasper0729
jjasper0729

Reputation: 295

I found out what I needed to do. The following line:

<td style="font-size: 11px;  text-align: right;">
  <xsl:value-of select="substring-before(../../tr/td[@id='testdate'], ' ')"/>
</td>

I need to change to:

<td style="font-size: 11px;  text-align: right;">
  <xsl:value-of select="substring-before(../td[@id='testdate'], ' ')"/>
</td>

I was backing out too far and coming back in. I just needed to back out to the tr tag and get the td with the @id=testdate

Upvotes: 2

Related Questions