Solved

preserve entities

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parse data from XMLtype data in Oracle SQL 5 75
Custom Ribbon doesn't refresh 7 77
XML namaspace 2 43
XML XSLT If loop 4 20
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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, …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 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

12 Experts available now in Live!

Get 1:1 Help Now