如何使用XSLT处理给定的汽车目录XML源文件以生成输出?
Alright, let's walk through exactly how to use XSLT to turn your car catalog XML into a usable output—we'll use HTML as the target format since it's the most common and easy-to-view option.
Step 1: Your Complete XML Input
First, here's your original XML with the truncated Mercedes ClassB entry filled in for testing:
<?xml version="1.0" encoding="UTF-8"?> <catalog> <car> <brand>Mercedes</brand> <type>ClassA</type> <engine>Diesel</engine> <seats>5</seats> </car> <car> <brand>Audi</brand> <type>A8</type> <engine>Diesel</engine> <seats>2</seats> </car> <car> <brand>Mercedes</brand> <type>ClassB</type> <engine>Petrol</engine> <seats>5</seats> </car> </catalog>
Step 2: Create the XSLT Style Sheet
This XSLT file defines how to map your XML elements to structured HTML. Save it as car-catalog.xsl:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Root template: matches the top-level <catalog> and builds the HTML framework --> <xsl:template match="/catalog"> <html> <head> <title>Car Catalog</title> <style> table { border-collapse: collapse; width: 80%; margin: 20px auto; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } </style> </head> <body> <h1 style="text-align:center;">Car Catalog</h1> <table> <tr> <th>Brand</th> <th>Model Type</th> <th>Engine Type</th> <th>Number of Seats</th> </tr> <!-- Loop through every <car> entry in the catalog --> <xsl:for-each select="car"> <tr> <!-- Pull and insert values from each XML child element --> <td><xsl:value-of select="brand"/></td> <td><xsl:value-of select="type"/></td> <td><xsl:value-of select="engine"/></td> <td><xsl:value-of select="seats"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
Key Parts Explained
- Root Template: Targets the top
<catalog>element and sets up the full HTML document structure. <xsl:for-each>: Iterates over every<car>entry, so you don't have to write code for each individual vehicle.<xsl:value-of>: Extracts text content from XML elements (likebrandorseats) and inserts it into the HTML table.- Inline CSS: Adds basic styling to make the output clean—tweak this to match your design needs.
Step 3: Preview in a Browser
To see the transformed output instantly, add a processing instruction to link your XML to the XSLT file, right after the XML declaration:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="car-catalog.xsl"?> <catalog> <!-- Rest of your XML content --> </catalog>
Save both files in the same folder, then open the XML file in a modern browser—you'll see a formatted HTML table of your car catalog.
Step 4: Alternative Transformation Methods
If you need to generate output programmatically or via command line:
- Command Line: Use tools like
xsltproc(preinstalled on most Linux/macOS systems) ormsxsl(Windows). Example withxsltproc:xsltproc car-catalog.xsl car-catalog.xml > output.html - Programming Languages: Use libraries like:
- Python:
lxmllibrary'sXSLTclass - Java:
javax.xml.transformAPI - C#:
System.Xml.Xslnamespace
- Python:
Quick Customization Tips
- Plain Text Output: Replace the HTML structure with line-separated values for a simple text list.
- Filter Cars: Add an
<xsl:if>inside the loop to show only specific vehicles, e.g.:<xsl:if test="engine = 'Diesel'"> <!-- Only output diesel-powered cars --> </xsl:if> - Sort Entries: Use
<xsl:sort>inside the loop to order cars by brand or seat count:<xsl:for-each select="car"> <xsl:sort select="brand"/> <!-- Rest of the table row code --> </xsl:for-each>
内容的提问来源于stack exchange,提问作者Azygous92

