How to implement Carriage return in XSLT

后端 未结 10 1327
甜味超标
甜味超标 2020-12-28 15:18

I want to implement carriage return within xslt. The problem is I have a varible: Step 1 = Value 1 breaktag Step 2 = Value 2 as a string and would like to appear as

相关标签:
10条回答
  • 2020-12-28 16:08

    This works for me, as carriage-return + life feed.

    <xsl:text>&#xD;&#xA;</xsl:text>
    

    The "&#10;" string does not work.

    0 讨论(0)
  • 2020-12-28 16:08

    This is the only solution that worked for me. Except I was replacing &#10; with \r\n

    0 讨论(0)
  • 2020-12-28 16:14

    Here is an approach that uses a recursive template, which looks for &#10; in the string from the database and then outputs the substring before. If there is a substring after &#10; remaining, then the template calls itself until there is nothing left. In case &#10; is not present then the text is simply output.

    Here is the template call (just replace @ActivityExtDescription with your database field):

    <xsl:call-template name="MultilineTextOutput">
        <xsl:with-param name="text" select="@ActivityExtDescription" />
    </xsl:call-template>
    

    and here is the code for the template itself:

    <xsl:template name="MultilineTextOutput">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&#10;')">
            <xsl:variable name="text-before-first-break">
                <xsl:value-of select="substring-before($text, '&#10;')" />
            </xsl:variable>
            <xsl:variable name="text-after-first-break">
                <xsl:value-of select="substring-after($text, '&#10;')" />
            </xsl:variable>
    
            <xsl:if test="not($text-before-first-break = '')">
                <xsl:value-of select="$text-before-first-break" /><br />
            </xsl:if>
    
            <xsl:if test="not($text-after-first-break = '')">
                <xsl:call-template name="MultilineTextOutput">
                    <xsl:with-param name="text" select="$text-after-first-break" />
                </xsl:call-template>
            </xsl:if>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" /><br />
        </xsl:otherwise>
    </xsl:choose>
    

    Works like a charm!!!

    0 讨论(0)
  • 2020-12-28 16:17

    The cleanest way I've found is to insert !ENTITY declarations at the top of the stylesheet for newline, tab, and other common text constructs. When having to insert a slew of formatting elements into your output this makes the transform sheet look much cleaner.

    For example:

    <?xml version="1.0"?>
    <!DOCTYPE xsl:stylesheet [
        <!ENTITY nl "<xsl:text>
    </xsl:text>">
    ]>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:template match="step">
        &nl;&nl;
        <xsl:apply-templates  />
      </xsl:template>
    

    ...

    0 讨论(0)
提交回复
热议问题