咨询:如何编写XSL样式表实现XML数据转指定HTML表格?
XML to HTML Table XSL Transformation Guide & Example
Hey there! Let's troubleshoot your XML-to-HTML table issue. Since you've already tried writing XSL but aren't getting the expected output, sharing a few key details will help zero in on the problem:
- A snippet of your actual XML structure (so we can see how your data is organized)
- The XSL code you've written so far
- A description or mockup of what you want the final HTML table to look like
To get you started, here's a common example of how to map XML data to an HTML table using XSLT 1.0, which you can adapt to your specific use case:
Sample XML
<products> <product> <id>101</id> <name>Wireless Headphones</name> <price>99.99</price> <category>Audio</category> </product> <product> <id>102</id> <name>Smart Watch</name> <price>199.99</price> <category>Wearables</category> </product> </products>
Corresponding XSLT
<?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>Product Catalog</h2> <table border="1"> <!-- Table Header --> <tr> <th>ID</th> <th>Product Name</th> <th>Price</th> <th>Category</th> </tr> <!-- Table Rows (loop through each product) --> <xsl:for-each select="products/product"> <tr> <td><xsl:value-of select="id"/></td> <td><xsl:value-of select="name"/></td> <td>$<xsl:value-of select="price"/></td> <td><xsl:value-of select="category"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
Key Notes for Adaptation:
- Use
<xsl:for-each select="your-xml-path"/>to loop through repeating elements (like<product>in the example) - Target specific child elements with
<xsl:value-of select="element-name"/>to populate table cells - Adjust the
<th>elements to match your XML's data fields - For more complex structures (nested elements, attributes), you can use
<xsl:apply-templates>instead of<xsl:for-each>for cleaner code
Once you share your specific XML and XSL, we can tweak this to fit your exact needs!
内容的提问来源于stack exchange,提问作者J. Hradek
相关产品推荐
相关产品推荐

