RE: [xsl] usage of entities (for dummies)

Subject: RE: [xsl] usage of entities (for dummies)
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 3 Aug 2006 11:26:32 +0100
Adding to DC's response: 

> 2.) What is the difference of the usage of - for example - 
> "&amp;" and "&#38;"? When do i have to use the one and not the other?

The first is technically an "entity reference", the second is a "character
reference". The only difference is that entities have to be declared in the
DTD, except for the five built-in ones. Numeric character references can be
used without needing a declaration.

> 3.) And where can i find a good overview of enitites?

I use Bob duCharme's "Annotated XML Specification" - very useful because it
gives the actual text of the specification, then Bob's explanation of what
it really means.
> Finally i have also a non-entity question:
> I have to replace many of equivalent placeholders in the same 
> text too. Do i have to nest replace-functions for each of 
> them in one another like ...
> <xsl:value-of select="replace(replace(., '\^12', '&ccedil;'), 
> '\^13', '&amp;')"/> ... or is there a more elegant solution for this?

As well as DC's solution, another approach is to have a table of

<xsl:variable name="mods" as="element(mod)*">
  <mod from="\^12" to="&ccedil"/>
  <mod from="\^13" to="&amp;"/>

and run through them with a recursive function:

<xsl:function name="f:multi-replace" as="xs:string">
  <xsl:param name="in" as="xs:string"/>
  <xsl:param name="mods" as="element(mod)*"/>
    <xsl:when test="$mods">
      <xsl:sequence select="f:multi-replace(
                              replace($in, $mods[1]/@from, $mods[1]/@to),
                              subsequence($mods, 2))"/>
      <xsl:sequence select="$in"/>

Michael Kay

Current Thread