1

I receive text from source system with HTML special escape characters in middle of text. Now I need to translate it to the actual character in XSLT 2.0

I/p like

<Data>Lenevo100 f&amp;uuml;r Laser</Data>
<Data>DELL &amp;Agrave;llow Drucker</Data>

and the expected output would be

<DataOutput>Lenevo100 fÜr Laser</DataOutput>
<DataOutput>DELL Àllow Drucker</DataOutput>

If it is always only 2 or 3 values i can do the replace or translate function but the problem is this HTML special code characters are so many .Can any provide the solution to achieve this,

List of HTML special characters code https://www.ou.edu/research/electron/internet/special.shtml

HKmar
  • 45
  • 6
  • There may be solutions specific to a particular XSLT processor or execution platform. Let us know which product you are using for your XSLT 2.0 library. – Michael Kay Apr 03 '23 at 13:54
  • I am using this XSLT 2.0 Oracle Cloud OIC product – HKmar Apr 03 '23 at 15:42
  • Which XSLT 2 processor is used in Oracle Cloud OIC? Does it allow you to use Java libraries from XSLT so that you could call into an HTML parser? – Martin Honnen Apr 03 '23 at 20:27
  • No it wont allow to call Java libraries. – HKmar Apr 04 '23 at 04:49
  • What about my posted answer, to use the XSLT library parsing HTML/HTML entity references? Can't you use that either? – Martin Honnen Apr 04 '23 at 09:45
  • Hi Martin, I checked the XSLT library and it's also not allowing. – HKmar Apr 04 '23 at 11:26
  • Well, you don't have to import from the given URL, you can also download and then save to a location from which the cloud processor does allow an `xsl:import` or `xsl:include`. If you can't `xsl:import` or `xsl:include` anything, you can try to copy the code from the library inline into your own XSLT. – Martin Honnen Apr 04 '23 at 15:49
  • Sure Martin, let me try to do that. – HKmar Apr 05 '23 at 05:01

1 Answers1

1

David Carlisle has an HTML parser written in XSLT 2.0, it is online at https://github.com/davidcarlisle/web-xslt/blob/main/htmlparse/htmlparse.xsl, you can use it as follows:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:dpc="data:,dpc"
    exclude-result-prefixes="#all"
    version="2.0">
  
  <xsl:import href="https://raw.githubusercontent.com/davidcarlisle/web-xslt/main/htmlparse/htmlparse.xsl"/>

  <xsl:template match="Data">
    <DataOutput>
      <xsl:value-of select="dpc:htmlparse(., '', true())"/>
    </DataOutput>
  </xsl:template>
  
</xsl:stylesheet>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110