xslt位置计数器理解

时间:2016-11-08 18:11:53

标签: xml xslt xpath

嗨,我是xslt的新手,我很难理解我做错了什么。我想我错过了一个变量来存储计数以便能够显示它或类似的东西。我不知道如果我做得好,任何建议/提示也会很棒!

试图让它看起来像这样

<title>Vital Signs</title>
          <text mediaType="text/x-hl7-text+xml">
            <table width="100%" border="1">
              <thead>
                <tr>
                  <th>Date</th>
                  <th>BP-Sys(mm[Hg])</th>
                  <th>BP-Dia(mm[Hg])</th>
                  <th>HR(bpm)</th>
                  <th>RR(rpm)</th>
                  <th>Temp</th>
                  <th>O2 Sat(%)</th>
                  <th>Head Size</th>
                  <th>Waist(in)</th>
                  <th>Height(in)</th>
                  <th>Weight(lbs)</th>
                  <th>BMI(%)</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>9/15/2016</td>
                  <td ID="VitalReading.1.1">123</td>
                  <td ID="VitalReading.1.2">85</td>
                  <td>-</td>
                  <td>-</td>
                  <td>-</td>
                  <td>-</td>
                  <td>-</td>
                  <td>-</td>
                  <td ID="VitalReading.1.9">52.00</td>
                  <td ID="VitalReading.1.10">12.00000</td>
                  <td ID="VitalReading.1.11">3.12</td>
                </tr>
                <tr>
                  <td>8/3/2016</td>
                  <td>-</td>
                  <td>-</td>
                  <td>-</td>
                  <td>-</td>
                  <td ID="VitalReading.2.5">98.6</td>
                  <td>-</td>
                  <td>-</td>
                  <td>-</td>
                  <td ID="VitalReading.2.9">52.00</td>
                  <td ID="VitalReading.2.10">150.00000</td>
                  <td ID="VitalReading.2.11">39.00</td>
                </tr>
              </tbody>
            </table>
          </text>

从这个xml,btw方式来看,它们是多个clv根将出现的实例,因此在顶部输出示例中有多个重要读数。

<clv>
  <DOCEXTENSION>E7906752-8845-4526-B30A-1B1D27E0FB72</DOCEXTENSION>
  <WEIGHT>0.00</WEIGHT>
  <HEIGHT>-</HEIGHT>
  <BP>120/80</BP>
  <BPSYSTOLIC>120</BPSYSTOLIC>
  <BPDIASTOLIC>80</BPDIASTOLIC>
  <BPDESC></BPDESC>
  <BMI>-</BMI>
  <EFFECTIVEDATE>20140306</EFFECTIVEDATE>
  <NUMOFREADING>1</NUMOFREADING>
  <COMPANY>MAIN</COMPANY>
  <SSNO>10018</SSNO>
  <PROV>MC</PROV>
  <WEIGHT>0.00</WEIGHT>
  <HEIGHT></HEIGHT>
  <TEMPR></TEMPR>
  <PULSE></PULSE>
  <BP>120/80</BP>
  <BPS>120</BPS>
  <BPD>80</BPD>
  <RESP></RESP>
  <GLUCOSE></GLUCOSE>
  <HEADSIZE></HEADSIZE>
  <DATETIME>03/06/14 14:50:25</DATETIME>
  <VDATE>2014-03-06T00:00:00</VDATE>
  <RESULT> </RESULT>
  <BPDESC></BPDESC>
  <PULSEDESC></PULSEDESC>
  <RESPDESC></RESPDESC>
  <TEMPRDESC></TEMPRDESC>
  <O2SAT></O2SAT>
  <WAIST></WAIST>
  <BMI></BMI>
  <MILESTONE>0</MILESTONE>
  <MUNIT>1</MUNIT>
  <RECORDEDFLAG>O</RECORDEDFLAG>
</clv>

我现在拥有的xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method='xml'/>
    <xsl:template match="/">
        <component>
            <section classCode="DOCSECT" moodCode="EVN">
                <templateId root="2.16.840.1.113883.10.20.22.2.4" />
                <templateId root="2.16.840.1.113883.10.20.22.2.4.1" />
                <code code="8716-3" codeSystem="2.16.840.1.113883.6.1" />
                <title>Vital Signs</title>
                <text mediaType="text/x-hl7-text+xml">
                    <table width="100%" border="1">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>BP-Sys(mm[Hg])</th>
                                <th>BP-Dia(mm[Hg])</th>
                                <th>HR(bpm)</th>
                                <th>RR(rpm)</th>
                                <th>Temp</th>
                                <th>O2 Sat(%)</th>
                                <th>Head Size</th>
                                <th>Waist(in)</th>
                                <th>Height(in)</th>
                                <th>Weight(lbs)</th>
                                <th>BMI(%)</th>
                            </tr>
                        </thead>
                        <tbody>
                            <xsl:for-each select="clv">
                                <tr>
                                    <td>
                                        <xsl:value-of select="DATETIME"/>
                                    </td>
                                    <xsl:choose>
                                        <xsl:when test="BPSYSTOLIC != ''">
                                            <td ID="VitalReading.position()">
                                                <xsl:value-of select="BPSYSTOLIC"/>
                                            </td>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <td>-</td>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                    <xsl:choose>
                                        <xsl:when test="BPDIASTOLIC != ''">
                                            <td ID="VitalReading.position()">
                                                <xsl:value-of select="BPDIASTOLIC"/>
                                            </td>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <td>-</td>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                    <td>-</td>
                                    <td>-</td>
                                    <xsl:choose>
                                        <xsl:when test="TEMPR != ''">
                                            <td ID="VitalReading.position()">
                                                <xsl:value-of select="TEMPR"/>
                                            </td>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <td>-</td>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                    <xsl:choose>
                                        <xsl:when test="O2SAT != ''">
                                            <td ID="VitalReading.position()">
                                                <xsl:value-of select="O2SAT"/>
                                            </td>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <td>-</td>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                    <xsl:choose>
                                        <xsl:when test="HEADSIZE != ''">
                                            <td ID="VitalReading.position()">
                                                <xsl:value-of select="HEADSIZE"/>
                                            </td>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <td>-</td>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                    <xsl:choose>
                                        <xsl:when test="WAIST != ''">
                                            <td ID="VitalReading.position()">
                                                <xsl:value-of select="WAIST"/>
                                            </td>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <td>-</td>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                    <xsl:choose>
                                        <xsl:when test="HEIGHT != ''">
                                            <td ID="VitalReading.position()">
                                                <xsl:value-of select="HEIGHT"/>
                                            </td>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <td>-</td>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                    <xsl:choose>
                                        <xsl:when test="WEIGHT != ''">
                                            <td ID="VitalReading.position()">
                                                <xsl:value-of select="WEIGHT"/>
                                            </td>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <td>-</td>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                    <xsl:choose>
                                        <xsl:when test="BMI != ''">
                                            <td ID="VitalReading.position()">
                                                <xsl:value-of select="BMI"/>
                                            </td>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <td>-</td>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </tr>
                            </xsl:for-each>
                        </tbody>
                    </table>
                </text>
                <entry>
                </entry>
            </section>
        </component>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

如果您已显示当前输出,则会显示类似

的内容
<td ID="VitalReading.position()">-</td>

它实际输出文本&#34; position()&#34;而不是当前所选节点的位置。

您需要使用的是Attribute Value Templates,所以它看起来应该是这样的(对于您要输出的第一个td

<td ID="VitalReading.{position()}.1">-</td>

花括号表示要计算的表达式,而不是字面输出,因此它应该输出(对于第一个clv,即)

<td ID="VitalReading.1.1">-</td>

您可能还想了解有助于减少代码重复的命名模板。

试试这个(删节)XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method='html' indent="yes"/>
    <xsl:template match="/">
      <text mediaType="text/x-hl7-text+xml">
        <table width="100%" border="1">
          <thead>
            <tr>
              <th>Date</th>
              <th>BP-Sys(mm[Hg])</th>
              <th>BP-Dia(mm[Hg])</th>
              <th>HR(bpm)</th>
            </tr>
          </thead>
          <tbody>
            <xsl:for-each select="clv">
              <tr>
                <td>
                  <xsl:value-of select="DATETIME"/>
                </td>
                <xsl:call-template name="cell">
                  <xsl:with-param name="element" select="BPSYSTOLIC" />
                  <xsl:with-param name="number" select="1" />
                </xsl:call-template>
                <xsl:call-template name="cell">
                  <xsl:with-param name="element" select="BPDIASTOLIC" />
                  <xsl:with-param name="number" select="2" />
                </xsl:call-template>
                <xsl:call-template name="cell">
                  <xsl:with-param name="element" select="HRBPM" />
                  <xsl:with-param name="number" select="3" />
                </xsl:call-template>
              </tr>
            </xsl:for-each>
          </tbody>
        </table>
      </text>
    </xsl:template>

    <xsl:template name="cell">
      <xsl:param name="element" />
      <xsl:param name="number" />
      <xsl:choose>
        <xsl:when test="$element != ''">
          <td ID="VitalReading.{position()}.{$number}">
            <xsl:value-of select="$element"/>
          </td>
        </xsl:when>
        <xsl:otherwise>
          <td>-</td>
        </xsl:otherwise>
      </xsl:choose>
  </xsl:template>
</xsl:stylesheet>