Copied to clipboard

Flag this post as spam?

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


  • Johan1974 1 post 21 karma points
    Feb 17, 2013 @ 12:30
    Johan1974
    0

    Xslt tokenize split help

    Hello is it possible to use xslt to go from 

    Code: 
    <XML> 
    <Name>tag1value1;tag1value2</Name> 
    <Adress>tag2value1;tag2value2</Adress> 
    </XML> 
    To this 

    Code: 
    <XML> 
    <LINE> 
    <Name>tag1value1</Name> 
    <Adress>tag2value1</Adress> 
    </LINE> 
    <LINE> 
    <Name>tag1value2</Name> 
    <Adress>tag2value2</Adress> 
    </LINE> 
    </XML> 
    Any help would be most appreciated. 

    Johan 

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 17, 2013 @ 20:27
    Jan Skovgaard
    0

    Hi Johan and welcome to our :)

    I think it would be possible however I'm not sure it would be the most ideal approach.

    Where is your XML coming from? If it's generated in an extension that you can change I would probably make sure to have the proper format created in that layer instead.

    /Jan

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Feb 17, 2013 @ 22:31
    Chriztian Steinmeier
    0

    Hi Johan,

    I'm sure your data is probably a little more complex, but here's a straightforward way to solve this - process the Name elements, using the Split() extension to tokenize the data, and grab the corresponding Adress elements along with them:

    <?xml version="1.0"?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:template match="XML">
            <XML>
                <!-- Only process <Name> elements -->
                <xsl:apply-templates select="Name" />
            </XML>
        </xsl:template>
    
        <xsl:template match="Name">
            <xsl:variable name="names" select="umb:Split(., ';')/value" />
            <xsl:variable name="adresses" select="umb:Split(following-sibling::Adress[1], ';')/value" />
            <xsl:for-each select="$names">
                <xsl:variable name="pos" select="position()" />
                <LINE>
                    <Name>
                        <xsl:value-of select="." />
                    </Name>
                    <Adress>
                        <xsl:value-of select="$adresses[$pos]" />
                    </Adress>
                </LINE>
            </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>
    

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft