将 xml 转换为 html table 无法正确处理

converting xml to html table cannot get it right

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

Headers 行不应硬编码,应尽可能通用。

想要的结果:

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>

我的错误结果

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

非常感谢

改变

          <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>

基于(请接受他的回答)。

what I miss is how do I get the name.Do I need another nested foreach?and how?

不需要额外的 for-each。在第一行的开头再引入一个td,称之为"Name"。然后,在每个 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