Solved

preserve entities

Posted on 2001-06-09
2
899 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 120 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

803 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