tags:

views:

82

answers:

2

I am trying to include special characters such as £, #, " in an XSL document, but have been unable to find a good way to do this. If anybody can suggest a way to achieve this, I'd be very grateful. Example below.

Nick

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:template match="/">
<HTML>
  <HEAD></HEAD>
  <BODY>
    <TABLE>
      <TR>
        <TD>Account&nbsp;Name</TD>
        <TD>Finish</TD>
        <TD>Start</TD>
        <TD>Completed</TD>
        <TD>Completed&nbsp;Date</TD>
        <TD>Day</TD>
        <TD>User</TD>
        <TD>Your&nbsp;Name</TD>
        <TD>Company&nbsp;Name</TD>
        <TD>Job&nbsp;Title</TD>
        <TD>Would&nbsp;you&nbsp;like&nbsp;to&nbsp;attend?</TD>
     </TR>
      <xsl:for-each select="NewDataSet/Table">
        <TR>
          <TD>
            <xsl:value-of disable-output-escaping="yes" select="Account&nbsp;Name"/>
          </TD>
          <TD>
A: 

You can use the xsl:text tag for this, link text

Ivo
+4  A: 

Just put the characters in the file as is, £ and # aren't special characters.

Your problem is the &nbsp; this is not a known entity in XML only in HTML and hence your XSL is invalid. I would use &#160; which is the unicode character entity for the non-breaking space.

Save your file as UTF-8 is also a good idea.

AnthonyWJones
Thanks for your response Anthony. I'm getting the following error:Message = "Expected end of the expression, found ' '. Account --> <-- Name" <xsl:for-each select="NewDataSet/Table"> <TR> <TD> <xsl:value-of select="Account Name"/>Hopefully I'm missing something pretty obvious here!Thanks againNick
Nick
Hmm, we had no end of fun and games with £ so I'd dispute its not being special. Pragmatically if you put it in as the numeric value i.e. £ you will be more likely to get the desired result.
Murph
+1. £ and the non-breaking space are nothing special; if you are having trouble with them you are having trouble with all non-ASCII characters, which is a shame in this century. @Nick: `select="Account Name"` isn't a valid XPath regardless of how you encode the non-breaking space. What are you trying to do?
bobince
@Murph: £ isn't special, period. Your problems will have arisen from the document character encoding. Most likely you saved the file as Windows-1252 encoding but had not include encoding="Windows-1252" in you xml declaration. This is the most common cause this confusion of "special characters" where its actually incorrect encoding at fault.
AnthonyWJones
@bobince. Thanks for taking the time to reply. I have written a class to replace literal values with string extensions, which appears to have resolved the problems. Unfortuntely, I have no control (at this point) of the data coming in which is riddled with formatting issues so I had to manipulate the data source too. Thanks all.
Nick
@Nick: as bobince points out the XPath you are trying to use is invalid. For it to work the source XML would need to have an element with the name "Account Name" (where the space is actually a 160 character). However that XML would be invalid to start with, you cannot include whitespace (whether it be a plain space or a nbsp) in an XML element name.
AnthonyWJones