You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSL遍历同标签元素问题:学习XSD时XML转HTML效果不佳求助

Fixing Your XML-to-HTML XSL Transformation

First, let's make sure your XML is complete (since it was cut off in your question). Here's the full, well-formed version:

<?xml version="1.0" encoding="UTF-8"?>
<sales>
  <division id="North">
    <revenue>10</revenue>
    <growth>9</growth>
    <bonus>7</bonus>
  </division>
  <division id="South">
    <revenue>4</revenue>
    <growth>3</growth>
    <bonus>4</bonus>
  </division>
  <division id="West">
    <revenue>6</revenue>
    <growth>5</growth>
    <bonus>6</bonus>
  </division>
</sales>

Now, here's a working XSLT stylesheet that converts this XML into a clean, readable HTML table. I'll break down the key parts so you can tweak it to fit your needs:

<?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>
      <head>
        <title>Sales Division Report</title>
        <style>
          table { border-collapse: collapse; width: 80%; margin: 20px auto; }
          th, td { border: 1px solid #ddd; padding: 8px; text-align: center; }
          th { background-color: #f2f2f2; }
          h1 { text-align: center; color: #333; }
        </style>
      </head>
      <body>
        <h1>Sales Division Performance</h1>
        <table>
          <tr>
            <th>Division</th>
            <th>Revenue</th>
            <th>Growth</th>
            <th>Bonus</th>
          </tr>
          <xsl:for-each select="sales/division">
            <tr>
              <td><xsl:value-of select="@id"/></td>
              <td><xsl:value-of select="revenue"/></td>
              <td><xsl:value-of select="growth"/></td>
              <td><xsl:value-of select="bonus"/></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Key Details to Note:

  • Root Template: The <xsl:template match="/"> targets the very top of your XML document, so this is where we build our full HTML structure.
  • XPath Selectors:
    • sales/division tells the transformer to loop through every <division> element inside the <sales> root.
    • @id grabs the id attribute of each division (the @ symbol is how you reference attributes in XPath).
    • revenue, growth, etc., pull the text content of those child elements directly.
  • Basic Styling: The embedded CSS makes the output table look polished—you can adjust colors, spacing, or layout here as needed.

Common Pitfalls You Might Be Hitting:

  1. Malformed XML: If your XML is incomplete (like the cut-off West division in your question), the transformer will throw errors. Always validate your XML first to ensure it's well-formed.
  2. Incorrect XPath: Double-check your select attributes. For example, if you tried select="division" instead of sales/division from the root, it won't find any elements.
  3. Missing XSL Namespace: The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" line is mandatory—without it, the processor won't recognize the XSLT tags.
  4. Unlinked Stylesheet: To test this, save the XSL as sales-transform.xsl, then add this line right after your XML declaration:
    <?xml-stylesheet type="text/xsl" href="sales-transform.xsl"?>
    
    Open the XML file in a modern browser, and it should render the formatted HTML table automatically.

内容的提问来源于stack exchange,提问作者Christian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:26:27