Sanjeev
Sanjeev

Reputation: 211

escaping html tags inside xslt:value-of select


I am converting text inside a XML document element (which also contains other texts not only a hyperlink) to hyperlink using the following:

<xsl:value-of 
     select="replace(AUTHOR_CONTACT_INFORMATION, 
                     '([email protected])', 
                     '&lt;a href=&quot;$1&quot;&gt;$1&lt;/a&gt;')"/>

XML Source:

<AUTHOR_CONTACT_INFORMATION>Plugin author can be reached at [email protected] for his email.</AUTHOR_CONTACT_INFORMATION>

Unfortunately, I cannot escape the '<' & '>' tags used in a href. My XSLT processor is Qt4's QXmlQuery class. It seems it doesn't support 'disable-output-escaping' attribute in the above expression and I can't seem to find any other way than to manually parse the final output and replace each '&lt' & '&gt' with '<' & '>' using C++ code.

PS: Couldn't get 'analyse-string' to work in qt either. It just returns empty string. Refered to http://www.dpawson.co.uk/xsl/rev2/regex2.html question 3 and used the following:

<xsl:analyze-string select="AUTHOR_CONTACT_INFORMATION" regex="([email protected])">
    <xsl:matching-substring>
        <a href="{.}"><xsl:value-of select="." /></a>
    </xsl:matching-substring>

    <xsl:non-matching-substring>  
        <xsl:value-of select="." />
    </xsl:non-matching-substring>
</xsl:analyze-string>

The above works fine in 'Altova XMLSpy' doing exactly what I want. But in Qt4, the output has empty string (To be more accurate my <td> tag before this expression is also mysteriously gone in the output, weird). Qt4 documentation says its processor supports 'xsl:analyze-string'. Any one have a working example of 'xsl:analyze-string' in Qt?

Qt4 documentation says its processor doesn't support 'xsl:character-map' so no point going there.

Without 'xsl:analyze-string' working I am back to square one with 'replace()'.

-Sanjeev

Upvotes: 2

Views: 2707

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

Hi all, I am converting text inside a XML document element to hyperlink using the following:

<xsl:value-of select="replace(AUTHOR_CONTACT_INFORMATION,

'(RegEx here)', '<a href="$1">$1</a>')"

Unfortunately, I cannot escape the '<' & '>' tags used in a href

The short answer: You shouldn't even try to do that.

Escaping markup is demoting it to one-dimensional text and it will only show as text in a browser.

The proper way to do this is to generate the markup as (a sequence of) literal result elements:

<xsl:template match="AUTHOR_CONTACT_INFORMATION">
  <a href="{someExpression}"><xsl:value-of select="someExpr2"/></a>
</xsl:template>

UPDATE: The OP has provided more information about the real problem. The problem is that thie following is "not working" on his XSLT processor -- Qt4 ???

<xsl:analyze-string select="AUTHOR_CONTACT_INFORMATION" regex="([email protected])">
    <xsl:matching-substring>
        <a href="{.}">
            <xsl:value-of select="." />
        </a>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
        <xsl:value-of select="." />
    </xsl:non-matching-substring>
</xsl:analyze-string>

I have verified that this codes behaves as expected and produces the expected result when run under Saxon 9.1.05.

This means that Qt4 is not a compliant XSLT 2.0 processor.

Here I can offer the following alternative solution, which works both with an XSLT 1.0 processor and an XSLT 2.0 processor:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="AUTHOR_CONTACT_INFORMATION">
  <xsl:value-of select=
   "substring-before(., '[email protected]')"/>
  <a href="[email protected]">[email protected]</a>
  <xsl:value-of select=
   "substring-after(., '[email protected]')"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<AUTHOR_CONTACT_INFORMATION>Plugin author can be reached at [email protected] for his email.</AUTHOR_CONTACT_INFORMATION>

the wanted, correct result is produced (with all 8 different XSLT processors I have at hand):

Plugin author can be reached at <a href="[email protected]">[email protected]</a> for his email.

Upvotes: 2

Related Questions