Solved

preserve entities

Posted on 2001-06-09
2
888 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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
D3, dragging and rotating. 7 93
Fetch XML Unions? 3 357
json format text only 4 64
How do I bind the results to a grid 3 22
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 …
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now