xsl 1.0为什么节点不匹配返回数据

时间:2013-08-30 14:52:57

标签: xslt xslt-1.0

我正在使用以下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>

文档中有多个节节点(具有自己的子节点,包括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,'&#10;')">
      <xsl:value-of select="substring-before($string,'&#10;')"/>
      <br/>
      <xsl:call-template name="replace">
        <xsl:with-param name="string" select="substring-after($string,'&#10;')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

在这个特定的xml示例中,输出应为:

预约:
没有安排未来的约会。

我在考虑比赛并选择需要一些调整但不确定是哪一部分。

另外,如果模板可以调整,以便我可以传递一个带有table / @ id值的参数,这样我就可以将这个模板重用于几个项目,这将更有利(推荐的输出)和本例中的约会相同)。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

这是您的XML部分根属性(从XML剪切并粘贴):

 root="2.16.840.1.113883.10.20.22.2.10" 

这是你的测试XSL:

 root='2.16.840.1.113883.10.20.22.2.17'

当然他们不匹配,一个以“10”结尾,另一个以“17”结尾

将数据更改为“17”并更正注释中的其他错误会产生:

 <div style="float: left; width: 50%;"><span style="font-weight: bold;">Appointments:
       </span><br>No future appointments scheduled.
 </div