Re: [xsl] Resolving Unescaped Entity Refs

Subject: Re: [xsl] Resolving Unescaped Entity Refs
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 30 Apr 2002 21:39:27 +0100
Hi Nik,

>> I am working on a transformation that uses the same 4 letter 'code'
>> as a back-end parameter and an entity reference. Currently the
>> 'code' appears twice for each node - once escaped and once not
>> escaped, and I'd like to find a way to eliminate one.
>> 
>> Current Example:
>> 
>> <node display="&code;">code</node>
>> 
>> I'd like to replace it with:
>> 
>> <node>code</node>
>> 
>> and create functionality that would resolve this using an entity
>> reference in an external DTD (<!ENTITY code "Resolved Display
>> Value">)

When your XML document is parsed, the entity reference &code; is
automatically replaced with the text from the DTD. So as far as the
XSLT is concerned, it will be just as if the XML document was:

  <node display="Resolved Display Value">code</node>

I guess that at the moment you're using the value of the display
attribute in some way, so you know this.

I think that you're asking about how to have <node>code</node> in your
source and replace it during the transformation so that you get
<node>Resolved Display Value</node>. The only kinds of documents that
XSLT can get information from (without a custom EntityResolver, at
least) are XML documents, and DTDs don't count as XML documents, so
you can't represent the replacement using an entity definition and
expect it to work. However, you could represent the entity in XML:

  <entity name="code">Resolved Display Value</entity>

and then access the document containing that XML using the document()
function. Something like:

<xsl:template match="node">
  <xsl:value-of
    select="document('entities.xml')/entity[@name = current()]" />
</xsl:template>

>> I thought I could use <xsl:value-of
>> select="unparsed-entity-uri(/node) />, however, in testing I find
>> this is incorrect.

Yes; the unparsed-entity-uri() function gets the URI of an *unparsed*
entity -- things like images, defined with entity definitions such as:

  <!ENTITY logo SYSTEM 'logo.gif' NDATA gif>

rather than *parsed* entities, which are textual substitutions.
  
Cheers,

Jeni

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


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


Current Thread