【问题标题】:xsl 1.0 Why won't a node match return dataxsl 1.0 为什么节点匹配不返回数据
【发布时间】: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,'&#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 值的参数,以便我可以将这个模板用于几个项目,那将更加有益(推荐的输出和本例中的约会将是相同的)。

感谢您的帮助

【问题讨论】:

  • 您的真实文档是否声明了任何命名空间(特别是在树的更高位置是否有默认命名空间xmlns="...")?
  • @Ian Roberts - 更新了 xslt 示例以包含标题信息。
  • 我不是指 XSLT,我指的是输入 XML 文档(包含 &lt;section&gt; 元素的文档)。
  • 很抱歉。不,xml 文档中没有。
  • 注意:上面这个例子中的 section 元素是损坏的/不正确的,所以如果没有看到正确的 XML,很难说......而不是结束这个部分,你开始一个新的部分。您在该模板中的 XSL 中也有一个错误……它以: template> 结尾。注意空格。如果这些是您的真实来源,则不应运行任何内容,并且 XSLT 处理器应抛出错误

标签: xslt xslt-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”并纠正我的 cmets 中的其他错误:

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

【讨论】:

  • 哦。我的。上帝。谢谢你。我什至没有注意到这一点。我从其他东西复制和粘贴,甚至没有改变它。
猜你喜欢
  • 2013-06-17
  • 2015-07-20
  • 1970-01-01
  • 2020-10-19
  • 2018-06-12
  • 2013-06-09
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
相关资源
最近更新 更多