Subject: Re: whats the best way to create and use values for lookup (key-value) such that you can loop through it with limits
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sat, 13 Sep 2003 08:39:02 +0200
|
One of the basic XSLT design patterns is about maintaining useful
programming data structures within the XSLT code.
XSLT allows to have arbitrary global scope elements belonging to a namespace
different from the XSLT namespace. Within such elements one can put any data
structure, which may be used in the transformation.
Here's a simple example of this technique that shows how to solve the
current problem:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myStruct="my:Struct"
exclude-result-prefixes="myStruct">
<xsl:output method="html" indent="yes"/>
<xsl:variable name="vmaxCols" select="2"/>
<myStruct:section1>
<col1>20</col1>
<col2>30</col2>
<col3>10</col3>
<col4>40</col4>
</myStruct:section1>
<myStruct:section2>
<mycol1>30</mycol1>
<mycol2>20</mycol2>
<mycol3>40</mycol3>
</myStruct:section2>
<xsl:template match="section1 | section2">
<table border="1">
<tr>
<xsl:for-each
select="document('')/*/myStruct:*
[local-name() = name(current())]/*
[position() <= $vmaxCols]">
<td width="{.}"><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
When the above transformation is applied on this source.xml:
<t>
<section1>
<row col1="x1" col2="y1" col3="z1"/>
<row col1="x2" col2="y2" col3="z2"/>
<row col1="x3" col2="y3" col3="z3"/>
<row col1="x4" col2="y4" col3="z4"/>
</section1>
<section2>
<row mycol1="x1" mycol2="y1" mycol3="z1"/>
<row mycol1="x2" mycol2="y2" mycol3="z2"/>
<row mycol1="x3" mycol2="y3" mycol3="z3"/>
</section2>
</t>
the wanted result is produced:
<table border="1">
<tr>
<td width="20">20</td>
<td width="30">30</td>
</tr>
</table>
<table border="1">
<tr>
<td width="30">30</td>
<td width="20">20</td>
</tr>
</table>
=====
Cheers,
Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
"SANWAL, ABHISHEK (HP-Houston)" <abhishek.sanwal@xxxxxx> wrote in message
news:24B68DDCFD49004882CD8D02D2E4338AFFBFD2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> How to pickup and insert values for individual column widths from
> variables/params/(any other value holder with local scope) already
> present in the XSL by limiting the number of values picked up to a
> variable that defines the number of columns or a separate count ?
>
> Basically I want to be able to specify in a template for a section the
> values for the column widths for a table that is present in that
> section.
> that matches a table XML structure the
>
> <xsl: template match section >
> <!-- values for column widths -->
> col 1 = 20
> col 2 = 30
> col 3 = 10
> col 4 = 40
> col 5 = "" <!-- may or may not be null ...that issue is open >
>
>
> <apply-template table/>
>
> <xsl: /template>
>
> <xsl: template match "table">
>
> <! Will use those values and apply them to the columns that it creates
> dynamically based on the number of columns present in the XML structure
> >
>
> How can I define the following so as to easily pickup values while
> iterating over the above value set defined in the XSL
>
> THOUGH PROCESS :::
> --------------------------
> whats the best way to create and use values for lookup such that you can
> loop through it (like a key value pair that can be looped through )
>
> Basically if I have J=10 values defined
>
> MC0 = 10
> MC2 = 12
> MC3 = 9
> MC4 = 17
> ..
> etc.
>
> (these values must be stored somehow in the XSL file)
>
> How can I iterate and limit the pick up of values from 0 to I where I is
> another variable that is less than J=10.
>
> 1st. How would you suggest storing the values ? variables ? params ?
> key()
> (I have not used key before.. just read somewhere that it can be used as
> such)
>
> 2nd. If I use one of the above structures for iteration then how do I
> iterate through and limit the pickup of values to I ??
>
> I want to be able to define the use of the variables based on an
> Iterator.
>
>
> Abhishek Sanwal
> HP - Houston Campus
> abhishek.sanwal@xxxxxx
>
>
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|