Change HTML tags to string

Here should go questions about transforming XML with XSLT and FOP.
Marzipan
Posts: 12
Joined: Fri Mar 02, 2012 12:11 am

Change HTML tags to string

Post by Marzipan »

<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: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Change HTML tags to string

Post by adrian »

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
Joined: Fri Mar 02, 2012 12:11 am

Re: Change HTML tags to string

Post by Marzipan »

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: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Change HTML tags to string

Post by adrian »

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

<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>
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
Joined: Fri Mar 02, 2012 12:11 am

Re: Change HTML tags to string

Post by Marzipan »

Is there a way to do this in a xslt only for all <html></html>
Marzipan
Posts: 12
Joined: Fri Mar 02, 2012 12:11 am

Re: Change HTML tags to string

Post by Marzipan »

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, '<')">
<xsl:value-of select="substring-before($html, '<')"/>
<!-- Recurse through HTML -->
<xsl:call-template name="removeHtmlTags">
<xsl:with-param name="html" select="substring-after($html, '>')"/>
</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, '<')">
<xsl:value-of select="substring-before($html, '<')"/>
<!-- Recurse through HTML -->
<xsl:call-template name="removeHtmlTags">
<xsl:with-param name="html" select="substring-after($html, '>')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$html"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
Post Reply