Change HTML tags to string

Here should go questions about transforming XML with XSLT and FOP.
Marzipan
Posts: 12

Change HTML tags to string

Wed Mar 14, 2012 3:36 pm

<html xmlns="http://www.w3.org/1999/xhtml" xml:space="preserve"><div><font color="#ff0000">CRITICAL CARE:</font> Audit central line insertion checklists for completion: twice weekly for each patient present in the unit with a CL for a total of 4 weeks. </div></html>

in a XML document and change it to string? Encapsulate it in single quotes?

an example would be appreciated.
adrian
Posts: 2477

Re: Change HTML tags to string

Wed Mar 14, 2012 4:22 pm

Hello,
Marzipan wrote:an example would be appreciated.
I was about to ask for the same thing.

What do you want to obtain in the output, can you offer an example?
You want the result of the XSLT transformation to be the concatenated text of the HTML(without the tags) between single quotes?

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Marzipan
Posts: 12

Re: Change HTML tags to string

Wed Mar 14, 2012 5:05 pm

No I want the entire HTML tag as the value. Not sure if I should append single or escaped quotes? How to encapsulate?
adrian
Posts: 2477

Re: Change HTML tags to string

Wed Mar 14, 2012 5:37 pm

You have to escape all the XML special characters: <, >, &, ", '
Oxygen already provides such a function. Select the text in the editor, right click on it and from the popup menu choose: Source > Escape Selection. Press OK in the Escape Characters dialog and the selected text will be escaped.
e.g.

Code: Select all

&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:space=&quot;preserve&quot;&gt;&lt;div&gt;&lt;font color=&quot;#ff0000&quot;&gt;CRITICAL CARE:&lt;/font&gt; Audit central line insertion checklists for completion: twice weekly for each patient present in the unit with a CL for a total of 4 weeks. &lt;/div&gt;&lt;/html&gt;


Quotes are only necessary if you want to use this as an attribute value.
e.g. <a attr="val">

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Marzipan
Posts: 12

Re: Change HTML tags to string

Wed Mar 14, 2012 6:01 pm

Is there a way to do this in a xslt only for all <html></html>
Marzipan
Posts: 12

Re: Change HTML tags to string

Fri Mar 16, 2012 4:42 pm

Found this and added to .xsl; not sure how to call it?

<xsl:template name="removeHtmlTags">
<xsl:param name="html"/>
<xsl:choose>
<xsl:when test="contains($html, '&lt;')">
<xsl:value-of select="substring-before($html, '&lt;')"/>
<!-- Recurse through HTML -->
<xsl:call-template name="removeHtmlTags">
<xsl:with-param name="html" select="substring-after($html, '&gt;')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$html"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>

<xsl:template match="/|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>

<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>


<!-- Removes levels with out data, easier to flatten and process in SSIS-->

<xsl:template match="*[contains(name(), 'Group')]">
<xsl:apply-templates/>
</xsl:template>


<xsl:template match="text()">
<xsl:value-of select="normalize-space(.)"/>
</xsl:template>

<xsl:template name="removeHtmlTags">
<xsl:param name="html"/>
<xsl:choose>
<xsl:when test="contains($html, '&lt;')">
<xsl:value-of select="substring-before($html, '&lt;')"/>
<!-- Recurse through HTML -->
<xsl:call-template name="removeHtmlTags">
<xsl:with-param name="html" select="substring-after($html, '&gt;')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$html"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 2 guests