This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: write out '&' instead of '&'


Hi Chris,

> Using Xalan to do xml to xml transformation. Need write out special
> characters as entities, e.g. ' as apostrophe. Now the number is
> a variable, but '&#<xsl:value-of select="$entityNumber">;' will not
> work in xslt. And I don't want to write '&amp;#<xsl:value-of
> select="$entityNumber">;'. What will be the way to get around it?

You can't usually control in XSLT the way that characters are
outputted - whether they're given as the character itself or a
character entity, or whether the character entity is a hex or decimal
one. You shouldn't usually want to: those are things to do with the
physical document and you should be thinking on a higher plane :)

But your situation, where you've got a character number and you want
to get a character out, is a bit different.  This is what
disable-output-escaping is for.  First, you *have* to use &amp;
instead of '&' whenever you want an ampersand in XML.  Otherwise the
parser gets all confused and thinks it should find an entity name.

If you just do:

  <xsl:text>&amp;#</xsl:text>
  <xsl:value-of select="$entityNumber" />
  <xsl:text>;</xsl:text>

then the XML output will be:

  &amp;#39;

which translates to the string (e.g. displayed in a browser)

  &#39;

In order to give XML output of:

  &#39;

you need to tell the XSLT processor not to escape the '&'.  You do
this through the disable-output-escaping, which can be placed on
xsl:text or xsl:value-of.  So if you do:

  <xsl:text disable-output-escaping="yes">&amp;#</xsl:text>
  <xsl:value-of select="$entityNumber" />
  <xsl:text>;</xsl:text>

then the XML output will be:

  &#39;

which translates to the string (e.g. displayed in a browser)

  '

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]