XSL Transform

Copper Contributor

VS allows programmers to issue documentation from their program thanks to HTML annotations.

I want to use XSLT for building a simple local web server for hosting this doc.

Unfortunately, the following html page does not work, any manner I use to launch the index page (Edge, Chrome, via a local web server, ...) :

<!DOCTYPE html>
<link rel="stylesheet" href="styles.css">
<link rel="XSL/Transform" xref="myindex.xsl">
<xml id="catalog">
<html>
<body>

<h1>HTML Heading</h1>
<p>HTML paragraph.</p>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
</body>
</html>

 

and myindex.xsl :

<?XSL/Transforml 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>My CDs</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">Assembly Name</th>
    </tr>
    <xsl:for-each select="catalog/cd">
      <tr>
      <td><xsl:value-of select="title"/></td>
  <td><xsl:value-of select="artist"/></td>
      </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I am on Windows 11. I tried everything: having the xml embedded i the index.html, external, different web browsers (Edge, Chrome, Firefox), local web servers (XAMPP, Rebex, ...), it is like it ignores tansformations and just renders the raw content. Anyone, an idea ?
1 Reply
Hi @BeBor1426
Thank you for the posting here,
Here is your problem's solution!!
In your HTML file, you're using <link> to reference the XSLT stylesheet. This is incorrect. Instead, you should use the <?xml-stylesheet?> processing instruction to link the XSLT file. Here's the corrected HTML code:
<!DOCTYPE html>
<html>
<head>
<title>My Catalog</title>
<link rel="stylesheet" href="styles.css">
<?xml-stylesheet type="text/xsl" href="myindex.xsl"?>
</head>
<body>

<h1>HTML Heading</h1>
<p>HTML paragraph.</p>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
</body>
</html>
In your XSLT file, there's a typo in the opening <?XSL/Transforml ... ?> tag. It should be <?xml-stylesheet ... ?> instead. Also, there's a small issue with your XPath expressions in the <xsl:for-each> loop. It should be <xsl:value-of select="title"/> and <xsl:value-of select="artist"/>. Here's the corrected XSLT code:
<?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>My CDs</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Make sure that both index.html and myindex.xsl are in the same directory, and then try opening index.html in your web browser again. It should now apply the XSLT transformation correctly.
Best Regards,
AddWebSolution