Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gordon Saxby 1461 posts 1883 karma points
    Nov 03, 2009 @ 14:05
    Gordon Saxby
    0

    Convert multiple lines to unordered list

    Is it possible, with XSLT, to take a number of lines in a multi-line text (Textbox Multiple) and convert each line into a <li> item?

  • atze187 160 posts 215 karma points
    Nov 03, 2009 @ 14:17
    atze187
    0

    Rumors on the web say, it is possible, but example didn't work for me, so i switched to a .net control.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 03, 2009 @ 14:25
    Lee Kelleher
    0

    Hi Gordon,

    Try this XSLT:

    <xsl:variable name="lines" select="umbraco.library:Split(data[@alias='bodyText'], '&#xD;')" />
    <xsl:if test="count($lines/value) &gt; 0">
        <ul>
            <xsl:for-each select="$lines/value">
                <li>
                    <xsl:value-of select="." />
                </li>
            </xsl:for-each>
        </ul>
    </xsl:if>
    
    The "&#xD;" entity is for new-lines... but as far as I'm aware it is only for /r (not /r/n - as in Windows), although this should give you the desired result!
    Remember to change the "data[@alias='bodytext']" with whatever is containing your multi-line content.
    Cheers,
    - Lee
  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 03, 2009 @ 14:26
    Chriztian Steinmeier
    0

    A recursive template should do the trick (modify the param statement to select the property holding your multiline textbox):

    <!-- Recursive template to write statements from a textarea property (e.g., newline-separated values) to successive <li> elements -->
    <xsl:template name="output-statements">
        <xsl:param name="statements" select="data[@alias = 'statements']" />
        <xsl:if test="contains($statements, '&#x0A;')">
            <li>
                <xsl:value-of select="substring-before($statements, '&#x0A;')" />
            </li>
            <xsl:call-template name="output-statements">
                <xsl:with-param name="statements" select="substring-after($statements, '&#x0A;')" />
            </xsl:call-template>
        </xsl:if>
        <xsl:if test="normalize-space($statements)">
            <li>
                <xsl:value-of select="$statements" />
            </li>
        </xsl:if>
    </xsl:template>
    

    You execute it with the call-template instruction from:

    <xsl:call-template name="output-statements" />

    /Chriztian

  • dandrayne 1138 posts 2262 karma points
    Nov 03, 2009 @ 14:41
    dandrayne
    0

    Yep, this is possible - but very flakey.  The two examples above should work for most situations, but I've seen linebreaks use "&#xd;" and "&#xa;" both separately and together in the same line.

    It's important to remember that the chars for linebreaks will differ between OS' ->  http://www.osix.net/modules/article/?id=93

    Here's another (!) alternative solution, for text entered on a windows machine

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">


    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:template name="newLineToLi">
    <xsl:param name="contentToChange" />
    <li><xsl:value-of select='umbraco.library:Replace($contentToChange, "&#xa;", "&lt;/li&gt;&lt;li&gt;")' disable-output-escaping="yes" /></li>
    </xsl:template>

    <xsl:template match="/">
    <xsl:call-template name="newLineToLi">
    <xsl:with-param name="contentToChange" select="$currentPage/data [@alias='metaDescription']" />
    </xsl:call-template>
    </xsl:template>

    </xsl:stylesheet>

    Dan

  • Gordon Saxby 1461 posts 1883 karma points
    Nov 03, 2009 @ 15:30
    Gordon Saxby
    1

    Thanks Lee, that worked perfectly :-)

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 03, 2009 @ 15:57
    Chriztian Steinmeier
    1

    - just a quick note on linefeeds in XML: An XML parser is required to normalize linefeed/carriage-return sequences to a single linefeed character (&#x0a;), before handing the document over to the receiver. So whether we're on Windows, Linux or Mac OS  we should actually be able to easily recognize any newline character in content.

    (http://www.w3.org/TR/REC-xml/#sec-line-ends)

    /Chriztian

  • Mike Chambers 636 posts 1253 karma points c-trib
    Aug 26, 2011 @ 14:22
    Mike Chambers
    0

    Another alternative, not sure on relative speeds... this one using regexp...

     

    <xsl:template match="/">
      <ul>
         <li>
          <xsl:value-of disable-output-escaping="yes" select="Exslt.ExsltRegularExpressions:replace($currentPage/*[name() = $property], '(&#xa;)+', 'Gi', '&lt;/li&gt;&lt;li&gt;')"/>
        </li>
      </ul>
    </xsl:template>

Please Sign in or register to post replies

Write your reply to:

Draft