【发布时间】:2013-08-30 14:52:56
【问题描述】:
我正在使用以下 xml 信息:
<section>
<...>
</section>
<section>
<templateId root="2.16.840.1.113883.10.20.22.2.10" />
<text>
<table id="Appointments">
<tr>
<td id="heading">Appointments</td>
</tr>
<tr>
<td id="content">No future appointments scheduled.</td>
</tr>
</table>
<br />
<table id="Referrals">
<tr>
<td id="heading">Referrals</td>
</tr>
<tr>
<td id="content">No referrals available.</td>
</tr>
</table>
<br />
</text>
<section>
<section>
<...>
</section>
文档中有多个section节点(有自己的子节点,包括templateId)。我遇到了这个问题,所以我想具体说明一下 xml 信息。
在我的 xslt 文件中,我想取出一张特定的表格。我通过以下方式引用它(我正在尝试使用模板并且我是 XSL 的新手,所以请多多包涵)
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//section[templateId/@root='2.16.840.1.113883.10.20.22.2.17']/text/table[@id='Appointments']" />
</xsl: template>
<xsl:template match="section[templateId/@root='2.16.840.1.113883.10.20.22.2.17']/text/table[@id='Appointments']">
<div style="float: left; width: 50%;">
<span style="font-weight: bold;">
<xsl:value-of select="tr/td[@id='heading']"/>:
</span>
<br />
<xsl:call-template name="replace">
<xsl:with-param name="string" select="tr/td[@id='content']"/>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template name="replace">
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($string,' ')">
<xsl:value-of select="substring-before($string,' ')"/>
<br/>
<xsl:call-template name="replace">
<xsl:with-param name="string" select="substring-after($string,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
在这个特定的 xml 示例中,输出应该是:
约会:
没有安排未来的约会。
我认为匹配和选择需要一些调整,但不确定是哪一部分。
另外,如果可以调整模板,以便我可以传递一个带有 table/@id 值的参数,以便我可以将这个模板用于几个项目,那将更加有益(推荐的输出和本例中的约会将是相同的)。
感谢您的帮助
【问题讨论】:
-
您的真实文档是否声明了任何命名空间(特别是在树的更高位置是否有默认命名空间
xmlns="...")? -
@Ian Roberts - 更新了 xslt 示例以包含标题信息。
-
我不是指 XSLT,我指的是输入 XML 文档(包含
<section>元素的文档)。 -
很抱歉。不,xml 文档中没有。
-
注意:上面这个例子中的 section 元素是损坏的/不正确的,所以如果没有看到正确的 XML,很难说......而不是结束这个部分,你开始一个新的部分。您在该模板中的 XSL 中也有一个错误……它以: template> 结尾。注意空格。如果这些是您的真实来源,则不应运行任何内容,并且 XSLT 处理器应抛出错误