Copied to clipboard

Flag this post as spam?

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


  • Karl 5 posts 29 karma points
    Oct 29, 2009 @ 00:15
    Karl
    4

    inline XSLT for string manipulation

     

    Using inline XSLT and Umbraco Library to manipulate strings from Umbraco Items.

     

    Sometimes you may insert an Umbraco Page Item that returns a string:

    Grocery List: <umbraco:Item field="groceryList" runat="server"></umbraco:Item>

    But you may not like the resulting format of the string. For instance, if it is returning the values of a Dropdown, Multiple element, you will get a comma-separated list of returned values like this:

    My Grocery List: Kale, Celery, Apples, Carrots

    Next you want to format the results so that it lists vertically, with no commas. But you do not want to create an entire separate XSLT file just for this simple string formatting. The alternative is to use inline XSLT and the Umbraco Library. Here's how:

    Grocery List: <p/>

    <umbraco:Item field="groceryList" runat="server" xslt="umbraco.library:Replace({0},',','<br/>')" xsltDisableEscaping="true"></umbraco:Item>

     

    Now, the result will look like this:

    My Grocery List:

     

    Kale

    Celery

    Apples

    Carrots

     

    Here's a few things that I learned along the way:

    1. The Microsoft XSLT parser supports XPATH 1.0, and not XPATH 2.0, so, we couldn't use, for instance, the XPATH 2.0 very handy 'Replace' function. No problem, the Umbraco libraries have a lot of support to help us out until Microsoft gets around to doing that. In this case, the Umbraco.library.Replace function does the job.

    2. XPATH statements, as well as the Umbraco library for string manipulation, usually need a reference to the string that is being manipulated. As in the above, 'Replace' takes three arguments; first the string with which to start, second the string to be replaced (the comma), and third the string to replace with (the HTML line break <br/>). But for the first element, how do we reference back to the Umbraco:Item that we started with? The answer is to use the placeholder {0} - it is a reference to the returned value of umbraco:Item.

    3. If you want to insert any HTML tag elements (such as our line break <br/>), you must set the xsltDisableEscaping="true" attribute. Otherwise, the microsoft XSLT parser will escape the characters with &lt and &gt, and you will see the actual text of the HTML in the browser window.

     

    I hope this helps someone else! Please give me a thumbs-up if you like this post.  Ideas for better ways to do this are also welcome!

     

     

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Oct 29, 2009 @ 09:13
    Dirk De Grave
    1

    Phillip, why don't you create a wiki page for this? Great you're posting this, but do think it's more appropriate in the form of a wiki page. Will you? You'll get an extra vote if you do!

    Cheers,

    /Dirk

  • Karl 5 posts 29 karma points
    Nov 24, 2009 @ 19:30
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Nov 24, 2009 @ 19:41
    Sebastiaan Janssen
    0

    Nice, I never thought of using a replace!

    What I tend to do on things that are this nicely comma-seperated is using the split function in an XSLT macro like this:

    <xsl:variable name="groceries" value="umbraco.library:Split(groceryList, ',')/value" />
    <xsl:for-each select="$groceries">
    <xsl:value-of select="." /><br />
    </xsl:for-each>
Please Sign in or register to post replies

Write your reply to:

Draft