Display values

Discussion in 'XML' started by Ravi, Nov 11, 2003.

  1. Ravi

    Ravi Guest

    My xml looks like

    <abc>
    23 45 67 2 123
    </abc>

    I wish to display these values comma separated

    23; 45; 67; 2; 123

    However if I use <xsl:value-of select="abc"> I get the entire list of
    values i.e. all 5 values. How can I obtain each component of the value?
    I cannot use for-each to select the value of each child as abc has no
    children in this case. Or should I modify the xml itself to include the
    comma?

    TIA.
    Ravi, Nov 11, 2003
    #1
    1. Advertising

  2. "Ravi" <> wrote in message
    news:bor66u$fgk$...
    > My xml looks like
    >
    > <abc>
    > 23 45 67 2 123
    > </abc>
    >
    > I wish to display these values comma separated
    >
    > 23; 45; 67; 2; 123
    >
    > However if I use <xsl:value-of select="abc"> I get the entire list of
    > values i.e. all 5 values. How can I obtain each component of the value?
    > I cannot use for-each to select the value of each child as abc has no
    > children in this case. Or should I modify the xml itself to include the
    > comma?


    If you can modify the XML, you might start generating it as
    <abc>
    <nr>23</nr>
    <nr>56</nr>
    <nr>67</nr>
    <nr>2</nr>
    <nr>123</nr>
    </abc>
    which would make transforming it into a separated list a lot easier.

    With your current XML, it would be possible using a recursive template
    and the substring-before function.

    The root of the problem is that five distinct entities have been rolled
    into a single text node.

    Groetjes,
    Maarten Wiltink
    Maarten Wiltink, Nov 11, 2003
    #2
    1. Advertising

  3. Ravi wrote:
    > My xml looks like
    >
    > <abc>
    > 23 45 67 2 123
    > </abc>
    >
    > I wish to display these values comma separated
    >
    > 23; 45; 67; 2; 123


    <xsl:value-of select="translate(abc,' ',';')"/>

    will result in

    23;45;67;2;123
    --
    Johannes Koch
    In te domine speravi; non confundar in aeternum.
    (Te Deum, 4th cent.)
    Johannes Koch, Nov 12, 2003
    #3
  4. In article <bor66u$fgk$>,
    Ravi <> wrote:

    % <abc>
    % 23 45 67 2 123
    % </abc>
    %
    % I wish to display these values comma separated
    %
    % 23; 45; 67; 2; 123

    Those are semi-colons, not commas. To do this in XSLT, you need to
    create a recursive template. The approach is to create a template which
    takes two arguments, s and t. If the s is zero-length, the template
    emits t. Otherwise, it builds new arguments, moving the first word of s
    to the end of t, and calls itself.

    <xsl:template match="abc">
    <xsl:call-template name="semi-colonify">
    <xsl:with-param name="s" select="string(.)"/>
    </xsl:call-template>
    </xsl:template>


    <xsl:template name="semi-colonify">
    <xsl:param name="s"/>
    <xsl:param name="t"/>

    <xsl:choose>
    <!-- s is not zero-length, so split it -->
    <xsl:when test="$s">
    <xsl:call-template name="semi-colonify">
    <xsl:with-param name="t">
    <!-- need to fiddle if there's already a t -->
    <xsl:if test="$t">
    <xsl:value-of select="$t"/>
    <xsl:text>; </xsl:text>
    </xsl:if>
    <xsl:variable name="sbs" select="substring-before($s, ' ')"/>
    <xsl:choose>
    <xsl:when test="$sbs">
    <xsl:value-of select="$sbs"/>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:value-of select="$s"/>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:with-param>

    <xsl:with-param name="s" select="substring-after($s, ' ')"/>
    </xsl:call-template>
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:value-of select="$t"/>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    XSLT can handle most string processing problems, but you may find
    it's easier to use another language. You have to balance portability
    requirements against your ability to be productive. For instance,
    this could be done with Rexx:

    <rexx:function name='my:semi-colonify' all-strings='yes'>
    return changestr(' ', arg(1), '; ')
    </rexx:function>

    <xsl:template match="abc">
    <xsl:value-of select='my:semi-colonify(.)'/>
    </xsl:template>

    Something similar can likely be done equally portably using python or
    a bit more portably using ecma script.
    --

    Patrick TJ McPhee
    East York Canada
    Patrick TJ McPhee, Nov 12, 2003
    #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. Imran Aziz
    Replies:
    4
    Views:
    1,506
    Imran Aziz
    Aug 11, 2005
  2. Marc Twain

    Display and display model

    Marc Twain, Jan 13, 2004, in forum: Java
    Replies:
    1
    Views:
    369
    Matt Humphrey
    Jan 13, 2004
  3. SG
    Replies:
    6
    Views:
    30,285
    ricuzzo
    Jan 15, 2008
  4. blank101
    Replies:
    2
    Views:
    519
    blank101
    Feb 5, 2005
  5. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    How to: display page, post back, display again

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Jul 14, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    3,005
    Walter Wang [MSFT]
    Jul 14, 2006
Loading...

Share This Page