Copied to clipboard

Flag this post as spam?

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


  • Robert Valcourt 70 posts 103 karma points
    Nov 10, 2016 @ 21:05
    Robert Valcourt
    0

    Combine/merge data in XSLT variable

    The scenario: $currentPage has a textbox multple datatype with the following data:

    value1
    value2
    value3
    value4
    

    The textbox multpile has an alias of 'theValues' with 4 lines of data. I use the following XSLT to extract this data and print to a list:

    <xsl:variable name="lines" select="umbraco.library:Split($currentPage/data[@alias='theValues'], '&#x0A;')//value"/>
    <xsl:for-each select="$lines">
        <li><xsl:value-of select="current()/text()"/></li>
    </xsl:for-each>
    

    This works fine. I'd like to add $currentPage/@nodeName into the 'lines' variable. In other words, combine/merge the data. Lets assume the @nodeName value is 'Page'. I would want the output to look like this:

    <li>Page</li>
    <li>value1</li>
    <li>value2</li>
    <li>value3</li>
    <li>value4</li>
    

    I have tried the following after searching for examples of merging data in an XSLT variable:

    <xsl:variable name="lines">
        <xsl:value-of select="$currentPage/@nodeName"/>
        <xsl:value-of select="umbraco.library:Split($currentPage/data[@alias='theValues'], '&#x0A;')//value"/>
    </xsl:variable>
    <xsl:variable name="merged" select="msxml:node-set($lines)"/>
    
    <xsl:for-each select="$merged">
        <li><xsl:value-of select="current()/text()"/></li>
    </xsl:for-each>
    

    This however does not output the data as desired. The following is the output:

    <li>Pagevalue1<li>
    

    It seems to have merged @nodeName with the first value of the textbox multiple. This produced a single merged value instead of 5 unique combined values.

    Hoping someone might help point me in the right direction. Please and thank you.

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Nov 11, 2016 @ 10:21
    Steve Morgan
    0

    Hi,

    It's been ages (and I don't have anything handy to test this with).. but something like:

    <xsl:variable name="lines" select="contact($currentPage/@nodeName + ',', umbraco.library:Split($currentPage/data[@alias='theValues'], '&#x0A;')//value)"/>
    

    Just concatenating on the page name at the start of the list?

    HTH

    Steve

  • Robert Valcourt 70 posts 103 karma points
    Nov 11, 2016 @ 14:16
    Robert Valcourt
    0

    I came up with the following last night. This script uses the true doctype aliases and date formatting. It's not pretty but it seems to work.

    <xsl:variable name="pre">
        <xsl:value-of select="$currentPage/data[@alias='eventDate']"/><xsl:text>&#x0A;</xsl:text>
    </xsl:variable>
    // add linebreak to datepicker data
    
    <xsl:variable name="addtime" select="umbraco.library:Replace($currentPage/data[@alias='additionalDates'], '&#x0A;', 'T00:00:00&#x0A;')"/>
    // replace all linebreaks with date formatting and re-add linebreak at end
    
    <xsl:variable name="finaltime">
        <xsl:value-of select="$addtime"/><xsl:text>T00:00:00</xsl:text>
    </xsl:variable>
    // adds a linebreak to the end (final line has none)
    
    <xsl:variable name="lines">
        <xsl:value-of select="$pre"/>
        <xsl:value-of select="$finaltime"/>
    </xsl:variable>
    // merges the data
    
    <xsl:variable name="merged" select="umbraco.library:Split($lines, '&#x0A;')//value"/>
    // splits the merged data data into array
    
    <xsl:for-each select="$merged">
        <li><xsl:value-of select="current()/text()"/></li>
    </xsl:for-each>
    // loops data into a list
    

    eventDate is an Umbraco datepicker which outputs the date like this: 2016-11-11T00:00:00. The textbox multiple allows the user to input additional dates in this format: 2016-11-11. The script above reads the text multiple, adds the "T00:00:00" and inserts the linebreak back.

    Then splits the lines based on &#x0A; and builds a variable, then finally merges with eventDate. The final output is:

    <li>2016-11-11T00:00:00</li> // eventDate
    <li>2016-11-12T00:00:00</li> // textbox line1
    <li>2016-11-13T00:00:00</li> // textbox line2
    <li>2016-11-14T00:00:00</li> // textbox line3
    <li>2016-11-15T00:00:00</li> // textbox line4
    

    No doubt there might have been a better way.

Please Sign in or register to post replies

Write your reply to:

Draft