XSL conversion for nested html tags

I have a series of documents output by a Java application that exports XML with html tags not bound, like for example

<b>some text</b>

(I cannot change this behavior).

An application that then uses this output should have all the html tags that have been escaped in

&lt;b&gt;some text &lt;/b&gt;

I use xslt below to avoid tags, but it is not surprising that it does not work for nested html tags , for example where

<u><b>A string of html</b></u>

When converting XSLT I get

&lt;u&gt;a string of html&lt;/u&gt; 

where nested <b> and </b> tags get removed altogether.

I am looking to achieve

&lt;u&gt;&lt;b&gt;A string of html&lt;/b&gt;&lt;/u&gt;

I am sure there is a simple answer to this by setting the select value or template, but I tried and failed

Any help would be greatly appreciated!

Sample document with embedded html tags

<?xml version="1.0" encoding="UTF-8"?>
<Main>
<Text><u><b>A string of html</b></u></Text>
</Main>

This is XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="UTF-8"/>
<xsl:strip-space elements="*" />  

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="Text/*">
  <xsl:value-of select="concat('&lt;',name(),'&gt;',.,'&lt;/',name(),'&gt;')" />
</xsl:template>

</xsl:stylesheet>

What produces

<?xml version="1.0" encoding="UTF-8"?>
<Main>
  <Text>&lt;u&gt;A string of html&lt;/u&gt;</Text>
</Main>

Internal bold tags were dropped as you can see.

- xslt?

: -)

+4
2

Text/*

<xsl:template match="Text//*">
  <xsl:value-of select="concat('&lt;',name(),'&gt;')" />
  <xsl:apply-templates />
  <xsl:value-of select="concat('&lt;/',name(),'&gt;')" />
</xsl:template>

, Text//* , . , "" .

XML :

<Main>
  <Text>&lt;u&gt;&lt;b&gt;A string of html&lt;/b&gt;&lt;/u&gt;</Text>
</Main>
+4

, , "", , Tim C . , , ( , , , HTML, , ):

<xsl:template match="Text//*">
  <xsl:value-of select="concat('&lt;',name())" />
  <xsl:apply-templates select="@*" mode="escape" />
  <xsl:text>&gt;</xsl:text>
  <xsl:apply-templates />
  <xsl:value-of select="concat('&lt;/',name(),'&gt;')" />
</xsl:template>

<xsl:template match="@*" mode="escape">
  <xsl:value-of select="concat(' ', name(), '=&quot;')" />
  <xsl:call-template name="doubleEscapeQuotes" />
  <xsl:text>"</xsl:text>
</xsl:template>

<xsl:template name="doubleEscapeQuotes">
  <xsl:param name="value" select="string(.)" />
  <xsl:choose>
    <xsl:when test="contains($value, '&quot;')">
      <xsl:value-of select="substring-before($value, '&quot;')" />
      <xsl:text>&amp;quot;</xsl:text>
      <xsl:call-template name="doubleEscapeQuotes">
        <xsl:with-param name="value" select="substring-after($value, '&quot;')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$value" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<a title="An &quot;example&quot; website" href="http://example.com">link</a>

&lt;a title="An &amp;quot;example&amp;quot; website" href="http://example.com"&gt;link&lt;/a&gt;

- - - escape-, , (&amp;amp;) (&amp;lt;) , , HTML , , ( , , , & <).

, . .

+1

All Articles