We help IT Professionals succeed at work.

Bullets and XSL

wsteph
wsteph asked
on
Anyone know how to have a bullet point rendered when performing the XSLT using the MSXML 3 SP2 parser?

I use • but when transforming it creates a square instead. Funny thing is when I do a transformation by using the stylesheet directive in the XML file instead of by using the MSXML 3 DOM object it works correctly.
Comment
Watch Question

Commented:
wsteph, it's all about encoding:

Take this is the XML Document for the purposes of explanation:

XML Document
============
<?xml version="1.0"?>
<root>This is a bullet &#8226;</root>

XSLT Document 1
===============
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>Encoding</title>
</head>
<body>
<div><xsl:copy-of select="root/node()" /></div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

HTML Output 1
===========
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-16">
<title>Encoding</title>
</head>
<body>
<div>This is a bullet •</div>
</body>
</html>

Now when you view this HTML, the bullet appears as a square. This simply so because the charset is now set to UTF-16 which does not support the &#8226; encoding.

The charset UTF-16 comes as a result of any one of the following:
1. used the transformNode to get the HTML Output as in ASP:
 <%
  Response.Write oXML.transformNode(oXSLT);
 %>
AND there exists a head element, AND output method is html.

According to the W3C specs, if there exists a head element and the output is html, the parser must insert a meta tag with the charset. Using MSXML3, this results a charset UTF-16 in the meta tag, and hence the square look.

2. used the transformNodeToObject method to get the HTML Output
 <%
  oXML.transformNodeToObject oXSLT, Response
 %>
AND you did not specify an encoding in the xsl:output element.

Notes
=====
1. The problem you describe will go away if you have the proper encoding charset set in the HTML Output.
2. Pay attention now, the only way to get the proper output is to ensure you have your expected encoding set in the xsl:output element AND use the transformNodeToObject method.

ASP Code
========
<%
 Dim oXML, oXSLT
 Set oXML = Server.CreateObject("Msxml2.DOMDocument.3.0")
 Set oXSLT = Server.CreateObject("Msxml2.DOMDocument.3.0")
 oXML.async = False : oXSLT.async = False
 oXML.load Server.MapPath("xml/encoding.xml")
 oXSLT.load Server.MapPath("xslt/encoding.xsl")
 oXML.transformNodeToObject oXSLT, Response
 Set oXML = Nothing
 Set oXSLT = Nothing
%>

XSLT Document Having Proper CharSet in HTML Output
==================================================
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" encoding="iso-8859-1"/>
<xsl:template match="/">
<html>
<head>
<title>Encoding</title>
</head>
<body>
<div><xsl:copy-of select="root/node()" /></div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

HTML Output that show the bullet correctly
==========================================
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Encoding</title>
</head>
<body>
<div>This is a bullet &#8226;</div>
</body>
</html>


Regards,

Brandon Driesen

Author

Commented:
Thanks again Brandon.