Xml xsl键未通过变量
我得到了以下xml:Xml xsl键未通过变量,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我得到了以下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/
<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>
2013年9月18日-重量-125磅
2013年9月18日-血压-120/80毫米汞柱
2013年9月18日-BMI-19 98
我需要让html输出如下所示:
<table>
<tr>
<td>9/18/2013</td>
<td>Blood Pressure - 120/80</td>
<td>Weight - 125lbs</td>
<td>BMI - 19</td>
</tr>
</table>
9/18/2013
血压-120/80
重量-125磅
体重指数-19
我在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>
 
</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:for-each>
</table>
</div>
</xsl:template>
血压:
;
(这只是一个片段。如果我可以让一个表格单元格显示血压,那么我可以将其复制到其他列表项中)。我遇到的问题是,约会没有问题。但是,下一个表格单元格未显示。我不知道我是否引用了错误的键,或者是否引用了要显示错误的值
谢谢看起来您在之后有了
子字符串,您应该在
之前有子字符串:
<xsl:variable name="values" select="key('vs_times', substring-after(., ' - '))"/>
此外,您还需要遍历$values
中的项目(对于每个,最好使用模板而不是),但您似乎已经知道了这一点。大概是这样的:
<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">
 
</xsl:template>
血压:
;
第二个模板的唯一问题是,列表中的每个项目的格式都可能不同。我已经研究过了,但是在第二个模板中选择/时会更多,在第一个模板中做起来会更简单。不过我可以尝试一下。@jjasper0729使用choose/when
代替模板几乎没有什么好的理由,尤其是在这种情况下。如果您想就如何以优雅的方式正确设置格式提出一个单独的问题,我非常乐意提供帮助。我想我可能会提出第二个问题。这非常有效,但我需要项目的特定顺序,这可能与它们迭代的顺序不同(例如,xml有权重,然后是血压,然后是bmi,但输出应该是bp,权重,bmi)。此外,如果没有这些问题之一,则输出中需要有一个空格来说明它。我在此处开始了一个新问题:。谢谢你在这方面的帮助。我仍然在学习来自.NET的xpath/xsl,所以在我的脑海中,模板与线性方法是我要努力解决的问题。
<xsl:when test="substring-before($values, ' - ') = 'Blood Pressure'">
<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">
 
</xsl:template>