Solved

preserve entities

Posted on 2001-06-09
2
909 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

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…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

624 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