Parsing comma-separated values with XSL?

Discussion in 'XML' started by RogerTBrick, Mar 4, 2005.

  1. RogerTBrick

    RogerTBrick Guest

    Cheers for the help last time guys. Again this is probably dead
    simple but try as I might, I just can't work it out.

    THe XML file I am being give contains some code value that need to be
    exchanged for text in the final output (to HTML as it happens).
    <root>
    <thingy code="2"/>
    <root>

    My first solution (the one I thought would be easiest) was you use two
    comma spearated lists and simply grab the correct text value based on
    the index.
    <xsl:variable name="codes">1,2,3,4</xsl:variable>
    <xsl:variable name="text">aaa,bbb,ccc,ddd</xsl:variable>

    But I am stumped. Is this even possible with XSL? Or is there a
    better way of doing the substitution that I'm missing?

    Thanks again,

    J.
     
    RogerTBrick, Mar 4, 2005
    #1
    1. Advertising

  2. RogerTBrick wrote:


    > THe XML file I am being give contains some code value that need to be
    > exchanged for text in the final output (to HTML as it happens).
    > <root>
    > <thingy code="2"/>
    > <root>
    >
    > My first solution (the one I thought would be easiest) was you use two
    > comma spearated lists and simply grab the correct text value based on
    > the index.
    > <xsl:variable name="codes">1,2,3,4</xsl:variable>
    > <xsl:variable name="text">aaa,bbb,ccc,ddd</xsl:variable>
    >
    > But I am stumped. Is this even possible with XSL? Or is there a
    > better way of doing the substitution that I'm missing?


    You can build a map in the XSLT stylesheet using elements in a separate
    namespace and access it as follows

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0"
    xmlns:mp="http://example.com/2005/03/map1">

    <xsl:param name="searchKey" select="1" />

    <map xmlns="http://example.com/2005/03/map1">
    <item>
    <key>1</key>
    <value>aaa</value>
    </item>
    <item>
    <key>2</key>
    <value>bbb</value>
    </item>
    </map>

    <xsl:template match="/">
    <result>
    <xsl:value-of
    select="document('')/xsl:stylesheet/mp:map/mp:item[mp:key =
    $searchKey]/mp:value" />
    </result>
    </xsl:template>

    </xsl:stylesheet>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 4, 2005
    #2
    1. Advertising


  3. > But I am stumped. Is this even possible with XSL?


    yes but string handling isn't xslt1's strong point, It's rather better
    at handling xml structure.

    I'd stick

    <things>
    <text>aaa</text>
    <text>bbb</text>
    <text>ccc</text>
    </things>

    in things.xml then do

    <xsl:template match="thingy">
    <xsl:copy-of
    select="document('things.xml')/
    things/text[position()=current()/@code]/node()"/>
    </xsl:template>

    or if your codes don't allways go 1 2 3

    I'd stick
    <things>
    <text code="x">aaa</text>
    <text code="y">bbb</text>
    <text code="z">ccc</text>
    </things>

    in things.xml then do

    <xsl:template match="thingy">
    <xsl:copy-of
    select="document('things.xml')/
    things/text[@code=current()/@code]/node()"/>
    </xsl:template>

    so as to extract on the code attribute not on position.

    If your list is long you could use a key to speed up searching for the
    right replacement.

    David
     
    David Carlisle, Mar 4, 2005
    #3
  4. RogerTBrick

    RogerTBrick Guest

    David Carlisle <> wrote in message news:<>...
    > yes but string handling isn't xslt1's strong point, It's rather better
    > at handling xml structure.
    >
    > I'd stick
    >
    > <things>
    > <text>aaa</text>
    > <text>bbb</text>
    > <text>ccc</text>
    > </things>

    Ooo, you've no idea how much I wish I could do that. Thanks for the
    help guys, I knew it was a filthy hack when I started, but needs must
    and all that.

    J.
     
    RogerTBrick, Mar 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DrLovely@.
    Replies:
    7
    Views:
    512
    DrLovely@.
    Mar 25, 2005
  2. ronan_40060

    Parsing a Comma Separated String in C

    ronan_40060, Sep 6, 2006, in forum: C Programming
    Replies:
    1
    Views:
    2,429
    Rudresh R kaddipudi
    Dec 22, 2006
  3. hu8
    Replies:
    5
    Views:
    269
    Stefan Lang
    Jan 31, 2005
  4. Justin To

    Parsing a comma-separated file

    Justin To, Jun 9, 2008, in forum: Ruby
    Replies:
    10
    Views:
    274
    Justin To
    Jun 10, 2008
  5. Yakov
    Replies:
    2
    Views:
    154
    Yakov
    May 12, 2007
Loading...

Share This Page