Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How have xsl:function return a string value including html tags

I am attempting to transpose a java function to an xsl:function spec. The function basically places html tags around substrings. I now bump into difficulties: using the java inline code this works perfectly, but I am unable to figure out how to prevent output escaping when using the xsl:function. How can I achieve the output to contain the wanted html tags?

A simplified example of what I am trying to achieve is the following: input parameter value "AB" should lead to a string A<b>B</b>, shown in html browser as AB of course.

Example function I tried is the below; but then the resulting string is A&lt ;b&gt ;B&lt ;/b&gt ; (note that I had to add blanks to prevent the entities from getting interpreted in this editor), which of course shows up in browers as A<b>B</b>.

Note that xsl:element cannot be used in the xsl:function code, because that has no effect; I want the string result of the function call to contain < and > characters, and then add the string result to the output result file.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:custom="http://localhost:8080/customFunctions">

    <xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>

    <xsl:function name="custom:test">
        <xsl:param name="str"/> 

        <xsl:value-of select="substring($str,1,1)"/>
        <xsl:text disable-output-escaping="yes"><![CDATA[<b>]]></xsl:text>
        <xsl:value-of select="substring($str,2)"/>
        <xsl:text disable-output-escaping="yes"><![CDATA[</b>]]></xsl:text>
    </xsl:function>

    <xsl:template match="/">
        <xsl:element name="html">
            <xsl:element name="body">
                <xsl:value-of select="custom:test('AB')"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
like image 519
Maestro13 Avatar asked Aug 08 '26 15:08

Maestro13


1 Answers

Here is an example, use sequence instead value-of and make sure your function returns nodes (which is usually simply done by writing literal result elements):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:custom="http://localhost:8080/customFunctions"
    exclude-result-prefixes="custom">

    <xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>

    <xsl:function name="custom:test">
        <xsl:param name="str"/> 

        <xsl:value-of select="substring($str,1,1)"/>
        <b>
          <xsl:value-of select="substring($str,2)"/>
        </b>
    </xsl:function>

    <xsl:template match="/">
        <xsl:element name="html">
            <xsl:element name="body">
                <xsl:sequence select="custom:test('AB')"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
like image 187
Martin Honnen Avatar answered Aug 10 '26 20:08

Martin Honnen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!