【问题标题】:converting xml to html table cannot get it right将 xml 转换为 html 表无法正确处理
【发布时间】:2015-03-01 15:13:28
【问题描述】:

xslt 新手,所以放轻松,我一直在尝试通过 xslt 将 xml 转换为 html,但我似乎无法正确处理。

标题和行不应硬编码,应尽可能通用。

想要的结果:

Xml 使用我无法控制

        <?xml version="1.0" encoding="utf-8"?>
    <Generated>
      <Employees>
        <Employee name="Joe Bloggs">
          <Sales>
            <Sale key="Sale-Id" value="333" />
            <Sale key="Sale-Field1" value="a" />
            <Sale key="Sale-Field2" value="b" />
          </Sales>
        </Employee>
        <Employee name="Mark Bloggs">
          <Sales>
            <Sale key="Sale-Id" value="334" />
            <Sale key="Sale-Field1" value="c" />
            <Sale key="Sale-Field2" value="d" />
          </Sales>
        </Employee>
      </Employees>
    </Generated>

XSLT 我的尝试

        <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" version="4" encoding="UTF-8" indent="no" omit-xml-declaration="yes"/>
      <!-- main body -->
      <xsl:template match="/">
        <html>
          <body>
            <h3>Employees</h3>
            <table border="1">
              <tr bgcolor="blue">
                <!--Header only so select first row to get headers-->
                <xsl:for-each select="(Generated/Employees/Employee)[1]/Sales/Sale">
                  <th>
                    <xsl:value-of select="@key"/>
                  </th>
                </xsl:for-each>
              </tr>
              <!--Get all the other rows-->
              <xsl:for-each select="(Generated/Employees/Employee)/Sales/Sale">
                <tr>
                  <td>
                    <xsl:value-of select="@value"/>
                  </td>
                </tr>
              </xsl:for-each>
              </table>
          </body>
        </html>
      </xsl:template>
    </xsl:stylesheet>

我的错误结果

任何建议如何解决此问题并根据上图获得我想要的结果

非常感谢

【问题讨论】:

    标签: html xml xslt xslt-2.0


    【解决方案1】:

    Martin Honnen's answer 为基础(请接受他的回答)。

    我想念的是如何获得名称。我需要另一个嵌套的 foreach 吗?如何?

    不需要额外的for-each。在第一行的开头引入另一个td 并将其命名为“名称”。然后,在每个Employee 元素的for-each 内,输出一个包含@name 值的td 元素。

    XSLT 样式表

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
       <xsl:output method="html" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"></xsl:output>
       <xsl:template match="/">
          <html>
             <body>
                <h3>Employees</h3>
                <table border="1">
                   <tr bgcolor="blue">
                      <td>Name</td>
                      <xsl:for-each select="(Generated/Employees/Employee)[1]/Sales/Sale">
                         <th>
                            <xsl:value-of select="@key"></xsl:value-of>
                         </th>
                      </xsl:for-each>
                   </tr>
                   <xsl:for-each select="Generated/Employees/Employee">
                      <tr>
                         <td>
                            <xsl:value-of select="@name"></xsl:value-of>
                         </td>
                         <xsl:for-each select="Sales/Sale">
                            <td>
                               <xsl:value-of select="@value"></xsl:value-of>
                            </td>
                         </xsl:for-each>
                      </tr>
                   </xsl:for-each>
                </table>
             </body>
          </html>
       </xsl:template>
    </xsl:stylesheet>
    

    HTML 输出

    <html>
       <body>
          <h3>Employees</h3>
          <table border="1">
             <tr bgcolor="blue">
                <td>Name</td>
                <th>Sale-Id</th>
                <th>Sale-Field1</th>
                <th>Sale-Field2</th>
             </tr>
             <tr>
                <td>Joe Bloggs</td>
                <td>333</td>
                <td>a</td>
                <td>b</td>
             </tr>
             <tr>
                <td>Mark Bloggs</td>
                <td>334</td>
                <td>c</td>
                <td>d</td>
             </tr>
          </table>
       </body>
    </html>
    

    呈现的 HTML

    【讨论】:

      【解决方案2】:

      改变

                <xsl:for-each select="(Generated/Employees/Employee)/Sales/Sale">
                  <tr>
                    <td>
                      <xsl:value-of select="@value"/>
                    </td>
                  </tr>
                </xsl:for-each>
      

                <xsl:for-each select="Generated/Employees/Employee">
                  <tr>
                   <xsl:for-each select="Sales/Sale">
                    <td>
                      <xsl:value-of select="@value"/>
                    </td>
                   </xsl:for-each>
                  </tr>
                </xsl:for-each>
      

      【讨论】:

      • ,哇,效果达 99%。我想念的是如何获得名称。我需要另一个嵌套的 foreach 吗?如何?
      • @developer9969 不,您不需要额外的for-each - 请参阅我的回答了解详细信息。
      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 2015-10-14
      • 2012-08-16
      • 2014-09-14
      • 1970-01-01
      • 2020-09-05
      相关资源
      最近更新 更多