【问题标题】:XSLT - include value from XML in HTML Table HeaderXSLT - 在 HTML 表头中包含来自 XML 的值
【发布时间】:2015-08-17 04:44:39
【问题描述】:

我正在使用一个具有这种格式的 XML 记录的简单示例来学习 XSLT:

<cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
</cd>

以下 XSLT 可以很好地将 XML 中的所有记录作为 HTML 表返回:

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th style="text-align:left">Title</th>
        <th style="text-align:left">Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
 </xsl:template>
</xsl:stylesheet>

我现在被要求在 Title 标头中包含第一个 XML 记录中的 country 字段的值(所有记录的 country 将具有相同的值)。

我尝试了以下方法,但它不起作用:

<th style="text-align:left">Title <xsl:value-of select="country"/></th>

我以前从来没有从“for-each”循环中检索 XML 的值,所以不确定正确的语法是什么,或者它是否可能?

【问题讨论】:

  • 您的 XML 示例与您的 XSLT 不匹配:它没有 catalog 包装器。
  • 我只包含了 XML 的片段,但它确实包含了 包装器。抱歉遗漏了。

标签: xml xslt


【解决方案1】:

试试:

<xsl:value-of select="catalog/cd[1]/country"/>

【讨论】:

    猜你喜欢
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多