xsl-list
[Top] [All Lists]

Re: Accessing multiple values of an attribute

2002-10-01 10:57:57

--- "Clifford, Karen" <Karen dot Clifford at fid-intl dot com> wrote:

 
Hello everyone, 

I have xml similar to the following


<Table stripes="blue red green purple">
      <Boxgrp>
              <Boxbod>
                      <Boxrow>
                              <Content> First Row</Content>
                      </Boxrow>
                      <Boxrow>
                              <Content> Second Row</Content>
                      </Boxrow>
                      <Boxrow>
                              <Content> Third Row</Content>
                      </Boxrow>
                      <Boxrow>
                              <Content> Fourth Row</Content>
                      </Boxrow>
                      <Boxrow>
                              <Content> Fifth Row</Content>
                      </Boxrow>
                      <Boxrow>
                              <Content> Sixth Row</Content>
                      </Boxrow>


The number of values existing in the stripes attribute is variable
(not
always 4), and the rows in the table can also be of different numbers
(i.e.
I could get one with 20 rows)
Using xsl, I must access the values of the 'stripes' attribute and
colour
the rows of the box.....i.e. in the above case, the box has 6 rows
and
4
different colours so I want my rows to be coloured in the following
way: 
Row 1: blue
Row 2: red
Row 3: green
Row 4: purple
Row 5: blue
Row 6: red   etc
 "         green
 "         purple

Question:
First of all, how do I access the different values of the stripes
attribute
(is there an easy way that I have overlooked?)?
I need to know 
(a) how many of them there are (for my mod function)
(b) what values they have. 

I was trying string-before and string-after but that could get messy
if
I
got alot of different colours.

Thanks & Regards

Karen


Hi Karen,

This is a most simple task if you use the "str-split-to-words" from
FXSL.

Given your source xml:
---------------------
<Table stripes="blue red green purple"> 
  <Boxgrp>              
    <Boxbod>                    
      <Boxrow>                          
        <Content> First Row</Content>                   
      </Boxrow>                 
      <Boxrow>                          
        <Content> Second Row</Content>                  
      </Boxrow>                 
      <Boxrow>                          
        <Content> Third Row</Content>                   
      </Boxrow>                 
      <Boxrow>                          
        <Content> Fourth Row</Content>                  
      </Boxrow>                 
      <Boxrow>                          
        <Content> Fifth Row</Content>                   
      </Boxrow>                 
      <Boxrow>                          
        <Content> Sixth Row</Content>                   
      </Boxrow>    
    </Boxbod>  
  </Boxgrp>
</Table>

This transformation will extract all colours from the atribute value
and produce a node-set, each element node of which contains the name of
exactly one colour. Then the count() function gives you the number of
colours.

stylesheet:
----------
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 xmlns:vendor="urn:schemas-microsoft-com:xslt" 
 exclude-result-prefixes="vendor"


   <xsl:import href="strSplit-to-Words.xsl"/>

   <xsl:output indent="yes" omit-xml-declaration="yes"/>
   
    <xsl:template match="/">
      <xsl:variable name="vrtf-wordNodes">
        <xsl:call-template name="str-split-to-words">
          <xsl:with-param name="pStr" select="/Table/@stripes"/>
          <xsl:with-param name="pDelimiters" 
                          select="' '"/>
        </xsl:call-template>
      </xsl:variable>
      
      <xsl:variable name="vwordNodes" 
                    select="vendor:node-set($vrtf-wordNodes)"/>
      
      <xsl:copy-of select="$vwordNodes"/>
      
      <xsl:text>&#xA;</xsl:text>
      
      <xsl:value-of select="concat(count($vwordNodes/*), ' colours')"/>
      
    </xsl:template>
    
</xsl:stylesheet>

Result:
------
<word>blue</word><word>red</word><word>green</word><word>purple</word>
4 colours


So, if the variable $vnColours contains the number of colours, then the
colour for the N-th row will be:

$vwordNodes/word[position() = $vRowPosition mod $vnColours]


Hope that this really helped.




=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>