2013-10-04 47 views
0

我有以下XML:XSL关键不通过变量

<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> 

我需要有HTML输出显示如下:

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

我有以下关键设置在我的xsl文件中:

<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(., ' - ')"/> 

这是获取日期。在我的xml中,可能会有多个日期集合(但最多两个),第二个日期选项将是上面表格示例中的另一行。

这是我的XSL模板信息输出:

<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> 

(这只是一个片段,如果我能得到一个表格单元格,以示对血压的话,我可以复制,对于。其他列表项目)。我遇到的问题是日期没有问题。但是,下一个表单元格不显示。我不知道我是否引用了我的密钥错误,或者我是否引用了显示错误的值。

感谢

回答

1

看起来你有substring-after,你应该有substring-before

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

应该是:

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

,这应该有substring-after

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

此外,您需要遍历$values中的项目(最好使用模板而不是for-each),但似乎您可能已经知道这一点。类似这样的:

<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> 
+0

第二个模板的唯一问题是对于列表中的每个项目,格式需要不同。我曾研究过这个问题,但它会在第二个模板中选择/更多,并且在第一个模板中更简单。尽管如此,我可以尝试一下。 – jjasper0729

+0

@ jjasper0729几乎没有什么好的理由使用'choose/when'而不是模板,特别是在这种情况下。如果您想就如何以优雅的方式获得格式化权利另外提出一个问题,我很乐意提供帮助。 – JLRishe

+0

我想我可能会打开第二个。这很好,但我需要的项目按特定的顺序可能与它们迭代的顺序不同(例如,xml具有权重,然后是血压,然后是bmi,但输出应该是bp,weight,bmi) 。此外,如果没有其中一个,那么在输出中需要有一个空白空间记录它 – jjasper0729