Menu

substring-before($text, $newline) working?

Help
Anonymous
2001-07-30
2012-10-08
  • Anonymous

    Anonymous - 2001-07-30

    I have a text param with multiple lines (i.e., a string value with embedded newline characters). I also have a global variable defined:

    <xsl:variable name="newline" select="'&#x0A;'"/>

    This is working for me:

    <xsl:when test="contains($text, $newline)">

    But this is not:

    <xsl:value-of select="substring-before($text, $newline)/>

    When I replace $newline with '&#x0A;' in the substring-before function, it works.

    I also have an ENTITY defined for newline:

    <!ENTITY nl "&#x0A;">

    The entity works in general, but this fails, as well:

    <xsl:value-of select="substring-before($text, '&nl;')/>

    Should these be working, or am I missing a subtlety with how this all works?

     
    • Michael Kay

      Michael Kay - 2001-07-31

      I can't see any reason why contains() should succeed and substring-before() should fail. You don't say how it fails. Please supply a complete example (source document + stylesheet) that demonstrates the problem.

      Mike Kay

       
      • Anonymous

        Anonymous - 2001-07-31

        Here's a source file:
        <test>
            Line 1
            Line 2
        </test>

        Here's a stylesheet that works:
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE ClassGenerator [ <!ENTITY nl "&#x0A;"> ]>
        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text"/>
        <xsl:variable name="newline" select="'&nl;'"/>
        <xsl:template match="test">
            <xsl:variable name="text" select="."/>
            <xsl:call-template name="lines">
                <xsl:with-param name="text" select="$text"/>
            </xsl:call-template>
        </xsl:template>
        <xsl:template name="lines">
            <xsl:param name="text"/>
            <xsl:choose>
                <xsl:when test="$text = ''"/>
                <xsl:when test="contains($text, $newline)">
                    <xsl:variable name="next" select="substring-before($text, '&#x0A;')"/>
                    <xsl:variable name="rest" select="substring-after($text, '&#x0A;')"/>
                    <xsl:value-of select="concat('* ', $next, '&#x0A;')"/>
                    <xsl:call-template name="lines">
                        <xsl:with-param name="text" select="$rest"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat('# ', $text, '&#x0A;')"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
        </xsl:stylesheet>

        It outputs:
        *
        *     Line 1
        *     Line 2

        It uses $newline for the contains test, but '&#x0A;' for everything else. If I change the substring-before and substring-after calls to use $newline instead:
                    <xsl:variable name="next" select="substring-before($text, $newline)"/>
                    <xsl:variable name="rest" select="substring-after($text, $newline)"/>

        I get this:
        *

        *
        *
        *
        * Line
        * 1

        *
        *
        *
        * Line
        # 2

        If I change the concat calls to also use $newline:
                    <xsl:value-of select="concat('* ', $next, $newline)"/>

        such that now everything is using $newline, I get:
        *
        *  *  *  * Line * 1
        *  *  *  * Line # 2

        I am using Saxon 6.4.4, by the way (not Instant Saxon).

         
    • Anonymous

      Anonymous - 2001-07-31

      Saxon 6.4.3, that is.