xsl key not passing through variables

2019-09-05 22:17发布

I've got the following xml:

<section>
  <templateId root="2.16.840.1.113883.10.20.22.2.4" />
  <text>
    <list listType="ordered">
      <item>9/18/2013 - Weight - 125 lbs</item>
      <item>9/18/2013 - Blood Pressure - 120/80 mm Hg</item>
      <item>9/18/2013 - BMI - 19 98</item>
    </list>
  </text>
</section>

I need to have the html output appear as the following:

<table>
  <tr>
    <td>9/18/2013</td>
    <td>Blood Pressure - 120/80</td>
    <td>Weight - 125lbs</td>
    <td>BMI - 19</td>
  </tr>
</table>

I have the following key set up in my xsl file:

<xsl:key name="vs_times" match="//hl7:section[hl7:templateId/@root='2.16.840.1.113883.10.20.22.2.4']/hl7:text/hl7:list/hl7:item" use="substring-before(., ' - ')"/>

This is to get the date. In my xml, there COULD be more than one set of dates (but at most two) and the second date option would be another row in the table example above.

This is my xsl template information to output:

<xsl:template match="hl7:section[hl7:templateId/@root='2.16.840.1.113883.10.20.22.2.4']">
  <div style="padding: 5px; border-top: 1px solid #000000;">
    <table border="0" cellspacing="0" cellpadding="1" width="100%">
      <xsl:for-each select="hl7:text/hl7:list/hl7:item[generate-id(.)=generate-id(key('vs_times', substring-before(., ' - ')))]">
        <tr>
          <td style="width: 76px;">
            <xsl:value-of select="substring-before(., ' - ')" />
          </td>
          <xsl:variable name="values" select="key('vs_times', substring-after(., ' - '))"/>
          <td style="width: 180px; padding-left: 3px;">
            <xsl:choose>
              <xsl:when test="substring-before($values, ' - ') = 'Blood Pressure'">
                <span style="font-weight: bold;">Blood Pressure: </span>
                <xsl:value-of select="substring-after($values, ' - ')"/>
              </xsl:when>
              <xsl:otherwise>
                &#xa0;
              </xsl:otherwise>
            </xsl:choose>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </div>
</xsl:template>

(this is just a snippet. if I can get the one table cell to show up for the blood pressure, then I can duplicate that for the other list items). The issue I'm having is that the date is showing up without issue. However, the next table cell is not showing up. I don't know if I'm referencing my key wrong or if I'm referencing the value to show wrong.

Thanks

1条回答
迷人小祖宗
2楼-- · 2019-09-05 22:59

It looks like you have substring-after where you should have substring-before:

<xsl:variable name="values" select="key('vs_times', substring-after(., ' - '))"/>

should be:

<xsl:variable name="values" select="key('vs_times', substring-before(., ' - '))"/>

And this should have substring-after:

<xsl:when test="substring-before($values, ' - ') = 'Blood Pressure'">

Also, you'll need to iterate through the items in $values (preferably with templates rather than for-each), but it seems like you might already know that. Something like this:

  <xsl:template match="hl7:section
                         [hl7:templateId/@root='2.16.840.1.113883.10.20.22.2.4']">
    <div style="padding: 5px; border-top: 1px solid #000000;">
      <table border="0" cellspacing="0" cellpadding="1" width="100%">
        <xsl:apply-templates
          select="hl7:text/hl7:list/hl7:item
                       [generate-id(.)=
                        generate-id(key('vs_times', 
                                        substring-before(., ' - ')))]" 
          mode="group" />
      </table>
    </div>
  </xsl:template>

  <xsl:template match="hl7:item" mode="group">
    <tr>
      <td style="width: 76px;">
        <xsl:value-of select="substring-before(., ' - ')" />
      </td>
      <xsl:variable name="values" select="key('vs_times', 
                                              substring-before(., ' - '))"/>
      <xsl:apply-templates select="$values" />
    </tr>
  </xsl:template>

  <xsl:template match="hl7:item">
    <td style="width: 180px; padding-left: 3px;">
      <xsl:apply-templates select="." mode="content" />
    </td>
  </xsl:template>

  <xsl:template match="hl7:item[contains(., 'Blood Pressure')]" mode="content">
    <span style="font-weight: bold;">Blood Pressure: </span>
    <xsl:value-of select="substring-after(., 'Blood Pressure - ')"/>
  </xsl:template>

  <xsl:template match="hl7:item" mode="content">
    &#xa0;
  </xsl:template>
查看更多
登录 后发表回答