Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL array variable?

Status
Not open for further replies.

kclan

Programmer
Joined
Mar 25, 2004
Messages
3
Location
HK
Is there any implicit method to use array to store the attribute values of a specified element?
My task is to check if there is a previous value exists such that there could be different expressions for them. For example, my xml is like this:

<p>
<c id=1>A</c>
<c id=2 ref=1>B</c>
<c id=3>C</c>
<c id=4>D</c>
<c id=5 ref=3>E</c>
</p>

-When looping through the element node c, firstly add the id value 1 into the array and display A with a specified color, say red.
-Then add the array by 2 and check that 1 is already in the array, so display B with the same color of A, i.e. red.
-Then add the array by 3 and display C with another color, say blue.
-Then add the array by 4 and display D with another color, say green.
-Then add the array by 5 and check that 3 is alreay in the array, so display E with the same color of C, i.e. blue.

The final output should be like this:

<font color="red">A<font>
<font color="red">B<font>
<font color="blue">C<font>
<font color="green">D<font>
<font color="blue">E<font>

Can I use xsl to do a such task? Thankyou very much.


 
No, xsl doesn't have arrays; the concept that comes closest is the node-set.
In your case however you don't need an array: you can always reconstruct the value you used before.
Here's an example:
Code:
  <xsl:template match="c">
      <tr>
         <td>
            <xsl:attribute name="bgcolor">
               <xsl:call-template name="color">
                  <xsl:with-param name="id">
                     <xsl:choose>
                        <xsl:when test="@ref">
                           <xsl:value-of select="@ref" />
                        </xsl:when>
                        <xsl:otherwise>
                           <xsl:value-of select="@id" />
                        </xsl:otherwise>
                     </xsl:choose>
                  </xsl:with-param>
               </xsl:call-template>
            </xsl:attribute>
            <xsl:value-of select="." />
         </td>
      </tr>
   </xsl:template>

   <xsl:template name="color">
      <xsl:param name="id" />
      <!-- number is: the count of pereceding nodes that don't have a ref-attribute -->
      <xsl:variable name="number" select="count(//c[@id=$id]/preceding-sibling::c[not (@ref)])" />

      <xsl:choose>
         <xsl:when test="$number=0">
            <xsl:value-of select="'RED'" />
         </xsl:when>
         <xsl:when test="$number=1">
            <xsl:value-of select="'BLUE'" />
         </xsl:when>
         <xsl:when test="$number=2">
            <xsl:value-of select="'GREEN'" />
         </xsl:when>
      </xsl:choose>

   </xsl:template>
 
Thankyou.

If I don't know how many pereceding nodes that don't have a ref-attribute, I seem need to predefine as many as hard coding color. Is there any flexible way to deal with a such color value?
 
The xsl I posted counts the number of preceding nodes without ref, so all you have to do is add enough color-variables:
Code:
      <xsl:choose>
         <xsl:when test="$number=0">
            <xsl:value-of select="'RED'" />
         </xsl:when>
         <xsl:when test="$number=1">
            <xsl:value-of select="'BLUE'" />
         </xsl:when>
         <xsl:when test="$number=2">
            <xsl:value-of select="'GREEN'" />
         </xsl:when>
         <xsl:when test="$number=3">
            <xsl:value-of select="'SILVER'" />
         </xsl:when>
         <!-- ETC ETC -->
         <xsl:otherwise>
            <xsl:value-of select="'GREY'" />
         </xsl:otherwise>
      </xsl:choose>
Of course, you might also invent a formula, where you relate the percentage of blue, red and green to the value of $number.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top