【问题标题】:XSLT : Apply a template to place child nodes into distinct table columnsXSLT:应用模板将子节点放置到不同的表列中
【发布时间】:2014-03-08 18:03:40
【问题描述】:

我是 XML 转换的新手,目前正在处理 XSLT 文件。我想在我在第一个模板中创建的表中将每个“人”元素放置到不同的行及其子节点(名称、acct-no、fav-color)到每行的不同列。我正在应用与“person”元素匹配的第二个模板,因此可以为每个 person 元素插入一个表格行及其单元格。

这是我的 XML 文件。

        <?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
        <?xml-stylesheet href="people.xsl" type="text/xsl"?>
        <people>
         <list-name>Favorite Colors</list-name>
         <person>
           <name>
            <first>Bob</first>
            <last>Toddson</last>
           </name>
           <acct-no>327598</acct-no>
           <fav-color hex="#ff0000">Red</fav-color>
        </person>
        <person>
           <name>
            <first>Red</first>
            <last>McBlue</last>
           </name>
           <acct-no>209890</acct-no>
           <fav-color hex="#00ff00">Green</fav-color>
       </person>
       <person>
          <name>
            <first>Tammy</first>
            <last>Yu</last>
          </name>
          <acct-no>978541</acct-no>
          <fav-color hex="#7fff00">Chartreuse</fav-color>
       </person>
       <person>
          <name>
           <first>Phillip</first>
           <last>Cardwell</last>
          </name>
          <acct-no>258929</acct-no>
          <fav-color hex="#d2b48c">Tan</fav-color>
      </person>
     </people>

还有 XSL 文件。

    <xsl:template match="/">

     <head>
        <title> People Report: <xsl:value-of select="/people/list-name" />
        </title>
     </head>
     <body>
      <center>
        <h1> People Report: <xsl:value-of select="/people/list-name" /> </h1>
      </center>

      <table border="1" align="center">
       <tr>
         <td>Last Name</td>
         <td>First Name</td>
         <td>Account Number</td>
         <td>Favorite Color</td>
       </tr>
       <xsl:apply-templates select="/people/person" />
      </table>

    </body>
   </html>
 </xsl:template>


<xsl:template match="person">
 <tr>
  <td>
    <xsl:value-of select="name/last" />
  </td>
  <td>
    <xsl:value-of select="name/first" />
  </td>
  <td>
    <xsl:value-of select="acct-no" />
  </td>
  <td>
    <xsl:value-of select="fav-color" />
  </td>
</tr>
</xsl:template>

</xsl:stylesheet>

我当前的输出仅将所有 person 元素及其子节点放在表的第一行第一列中。我认为我的问题是由于没有使用 for-each 命令和里面的 apply-templates,所以它可以迭代每个 person 元素。正如我在 SE 的相关帖子中所读到的,apply-templates 命令将适用于每个 person 元素,因此在我的情况下不需要使用 for-each。我已经挣扎了几个小时了,我看不到或不明白我在哪里遗漏了一些东西,所以任何帮助都将不胜感激。

预期输出:

我得到的输出:

先谢谢了。

【问题讨论】:

  • 你能发布你想要达到的输出吗?
  • "我当前的输出仅将所有 person 元素及其子节点放在表的第一行第一列中。" 你确定吗?使用您的输入和样式表,我得到了一个 4x4 表格,其中所有内容都在其位置。

标签: xml xslt


【解决方案1】:

试试这个模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="/">
        <html>
        <head>
            <title> People Report: <xsl:value-of select="/people/list-name" />
            </title>
        </head>
        <body>
            <center>
                <h1> People Report: <xsl:value-of select="/people/list-name" /> </h1>
            </center>

            <table border="1" align="center">
                <tr>
                    <td>Last Name</td>
                    <td>First Name</td>
                    <td>Account Number</td>
                    <td>Favorite Color</td>
                </tr>
                <xsl:for-each select="/people/person">
                    <tr>
                        <td>
                            <xsl:value-of select="name/last" />
                        </td>
                        <td>
                            <xsl:value-of select="name/first" />
                        </td>
                        <td>
                            <xsl:value-of select="acct-no" />
                        </td>
                        <td>
                            <xsl:value-of select="fav-color" />
                        </td>
                    </tr>
                </xsl:for-each>
            </table>

        </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    我会将xmlns="http://www.w3.org/1999/xhtml" 移动到样式表的根元素,如下所示

    <xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    

    确保所有模板中的所有结果元素都创建为 XHTML 元素。

    那么对我来说,完整的示例 http://home.arcor.de/martin.honnen/xslt/test2014030901.xml 在 IE 10 和当前版本的 Firefox 和 Opera 中呈现良好。

    【讨论】:

    • 我把它添加到了你提到的样式表的根元素中,现在我只得到了正确位置的第一人称元素。我都尝试过 IE 10 和 Firefox [在 Windows 7 上],但我的输出仍然与我最近在我的帖子中编辑的相同。可能是 Dreamweaver(CS6) 的配置问题?
    • 示例 home.arcor.de/martin.honnen/xslt/test2014030901.xml 是否无法为您正确呈现?如果您仍有问题,请发布完整示例的 URL,以便其他人可以重现该问题。
    猜你喜欢
    • 2016-07-18
    • 2016-12-12
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    相关资源
    最近更新 更多