Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

preserve entities

Posted on 2001-06-09
2
Medium Priority
?
920 Views
Last Modified: 2012-05-04
i'm using MSXML to transform some XML data to HTML
the XML contains entities like ü and others
my problem is, that after the transformations the entities
are gone (resolved and displayed as the described
character u-umlaut)
is there a way to preserve the entities or to recreate
them after the transformation?

here is some sample code (probably not working):

-------test.xml (start)-------
<?xml version="1.0" ?>
<!DOCTYPE test [
          <!ENTITY % ISOlat1
          PUBLIC "ISO 8879-1986//ENTITIES Added Latin 1//EN//XML"
          "http://www.icaap.org/software/ixml/ixml/ent/ISOlat1.pen">
          %ISOlat1;
]>

<test>
  <mytags>
      <tag1>Ue - &Uuml;</tag1>
      <tag2>Ae - &Auml;</tag2>
      <tag3>Oe - &Ouml;</tag3>
      <tag4>ue - &uuml;</tag4>
      <tag5>ae - &auml;</tag5>
      <tag6>oe - &ouml;</tag6>
  </mytags>
</test>
-------test.xml (end)-------

-------test.xsl (start)-------
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">
<xsl:output method="html"/>
<xsl:template match="test">
  <html><body>
    <xsl:apply-templates/>
  </body></html>
</xsl:template>

<xsl:template match="mytags">
  <p>
    <xsl:value-of select="tag1"/><br/>
    <xsl:value-of select="tag2"/><br/>
    <xsl:value-of select="tag3"/><br/>
    <xsl:value-of select="tag4"/><br/>
    <xsl:value-of select="tag5"/><br/>
  </p>
</xsl:template>
</xsl:stylesheet>
-------test.xsl (end)-------


0
Comment
Question by:egono
2 Comments
 
LVL 6

Accepted Solution

by:
PeterCiuffetti earned 480 total points
ID: 6178667
I don't know of an easy way to do this.  The problem is that the processing model of XSLT input says to load the text as Unicode.  The internal representation has lost the knowledge that the character started its life as a general entitiy.

The only way I know to get it back out as an entity is to test for each relevant Unicode character and output the entities yourself using the disable-output-encoding feature of xsl:text.

Here is a stylesheet that has a recursive named template called for any text() node.  It outputs the five entities you gave in your example.  For it to work, you'd have to list each possible entity from your dtd.  If you have a lot of possible entities, this will be possibly very slow.

You should therefore examine why you are bothering to do this.  What is wrong with outputing the Unicode version of the character?  It'll usually take up less space than the 6 bytes used to record an entity.

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">

<xsl:output method="html" standalone="yes" encoding="ascii"/>

<xsl:template match="test">
 <html><body>
   <xsl:apply-templates/>
 </body></html>
</xsl:template>

<xsl:template match="mytags">
 <p>
   <xsl:apply-templates select="tag1"/><br/>
   <xsl:apply-templates select="tag2"/><br/>
   <xsl:apply-templates select="tag3"/><br/>
   <xsl:apply-templates select="tag4"/><br/>
   <xsl:apply-templates select="tag5"/><br/>
 </p>
</xsl:template>

<xsl:template match="text()">
   <xsl:call-template name="translateText">
      <xsl:with-param name="theText" select="."/>
   </xsl:call-template>
</xsl:template>

<xsl:template name="translateText">
   <xsl:param name="theText"/>
   <xsl:if test="string-length($theText) &gt;= '1'">
      <xsl:variable name="c" select="substring($theText,1,1)"/>
      <xsl:choose>
         <xsl:when test="$c = 'U'"><xsl:text disable-output-escaping="yes">&amp;Uuml;</xsl:text></xsl:when>
         <xsl:when test="$c = 'A'"><xsl:text disable-output-escaping="yes">&amp;Auml;</xsl:text></xsl:when>
         <xsl:when test="$c = 'O'"><xsl:text disable-output-escaping="yes">&amp;Ouml;</xsl:text></xsl:when>
         <xsl:when test="$c = 'u'"><xsl:text disable-output-escaping="yes">&amp;auml;</xsl:text></xsl:when>
         <xsl:when test="$c = 'a'"><xsl:text disable-output-escaping="yes">&amp;ouml;</xsl:text></xsl:when>
         <xsl:otherwise><xsl:value-of select="$c"/></xsl:otherwise>
      </xsl:choose>
      <xsl:if test="string-length($theText) &gt; '1'">
         <xsl:call-template name="translateText">
            <xsl:with-param name="theText" select="substring($theText, 2)"/>
         </xsl:call-template>
      </xsl:if>
   </xsl:if>
</xsl:template>

</xsl:stylesheet>
0
 
LVL 2

Author Comment

by:egono
ID: 6181011
excellent working answer - thank you very much

my output have to be HTML with correct encoded characters,
so I need to convert the umlauts - speed doesn't matter
in this case
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question