Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT for-each newbie problem

Reply
Thread Tools

XSLT for-each newbie problem

 
 
R
Guest
Posts: n/a
 
      03-17-2005
Hello everybody.

I've got a problem with one loop.

Given XML:
<Types>
<Type><range>1</range><name>Gast</name></Type>
<Type><range>2</range><name>Rekt</name></Type>
<Type><range>2</range><name>Sigm</name></Type>
<!-- range is 1-6 -->
</Types>

I want to produce HTML output like this (grouped by range):
<li>Gast</li>
<li>Rekt, Sigm</li>

well I can produce the single <li> element for 'Gast and 'Rekt, Sigm'
output but I don't know how to iterate through all the 'range'

I did sth like:
1) produce <li> for range='1'
2) produce <li> for range='2'
....
6) produce <li> for range='6'

how to itrate through ranges from 1 to 6 automatically?
I know that ranges are sorted, there cannot be 5 before 2 etc.

this is my step-by-step version
<xsl:template match="Types" mode="searching">
<!-- do it for range='1' -->
<li>
<!-- variable needed for the comma -->
<xsl:variable name="last">
<xsl:value-of select="count(Type[./range='1'])"/></xsl:variable>
<xsl:for-each select="Type[./range='1']">
<xsl:value-of select="name"/>
<xsl:if test="$last != position()">, </xsl:if>
</xsl:for-each>
</li>
<li>
<xsl:variable name="last">
<xsl:value-of select="count(Type[./range='1'])"/></xsl:variable>
<xsl:for-each select="Type[./range='1']">
<xsl:value-of select="name"/>
<xsl:if test="$last != position()">, </xsl:if>
</xsl:for-each>
</li>
<!-- and for the rest of ranges 3, 4, 5, 6 the same -->
</xsl:template>

thanks for any help
best regards R
 
Reply With Quote
 
 
 
 
William Park
Guest
Posts: n/a
 
      03-17-2005
R <> wrote:
> Hello everybody.
>
> I've got a problem with one loop.
>
> Given XML:
> <Types>
> <Type><range>1</range><name>Gast</name></Type>
> <Type><range>2</range><name>Rekt</name></Type>
> <Type><range>2</range><name>Sigm</name></Type>
> <!-- range is 1-6 -->
> </Types>
>
> I want to produce HTML output like this (grouped by range):
> <li>Gast</li>
> <li>Rekt, Sigm</li>


Well, it's not XSLT, but this is what I would do using Expat parser and
Bash shell:

data () # Usage: data [1-6] or data [Gast|Rekt|...]
{
case ${XML_ELEMENT_STACK[1]} in
range) range=$1 ;;
name) name=$1 ;;
esac
}
start () # Usage: start tag att=value ...
{
case $1 in
Types) unset range{1..6} ;;
Type) unset range name ;;
esac
}
end () # Usage: end tag
{
case $1 in
Type) declare -p range$range &>/dev/null && strcat range$range ", "
strcat range$range $name ;;
esac
}

xml -s start -e end -d data '<Types> ... </Types>'
declare -p range{1..6}

--
William Park <>, Toronto, Canada
Slackware Linux -- because it works.

 
Reply With Quote
 
 
 
 
Ben LamHang
Guest
Posts: n/a
 
      03-18-2005
Had a look at it. I tried using xsl:if / xml:when / xmltherwise / xml:sort
to compare previous siblings and build the xml from there. However xml
doesn't like when you leave an unterminated tag which you need to run
through and stores 2 or more values between the same tags...
So close to your code, you have to manually put the figures in...

<?xml version="1.0" ?>
<xsl:transform version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="Types">
<li>
<xsl:for-each select="Type[range='1']">
<xsl:value-of select="name" /><xsl:if test="position()!=last()">,
</xsl:if>
</xsl:for-each>
</li>

<li>
<xsl:for-each select="Type[range='2']">
<xsl:value-of select="name" /><xsl:if test="position()!=last()">,
</xsl:if>
</xsl:for-each>
</li>

<li>
<xsl:for-each select="Type[range='3']">
<xsl:value-of select="name" /><xsl:if test="position()!=last()">,
</xsl:if>
</xsl:for-each>
</li>

<li>
<xsl:for-each select="Type[range='4']">
<xsl:value-of select="name" /><xsl:if test="position()!=last()">,
</xsl:if>
</xsl:for-each>
</li>

<li>
<xsl:for-each select="Type[range='5']">
<xsl:value-of select="name" /><xsl:if test="position()!=last()">,
</xsl:if>
</xsl:for-each>
</li>

<li>
<xsl:for-each select="Type[range='6']">
<xsl:value-of select="name" /><xsl:if test="position()!=last()">,
</xsl:if>
</xsl:for-each>
</li>
</xsl:template>

</xsl:transform>

"R" <> wrote in message
news: om...
> Hello everybody.
>
> I've got a problem with one loop.
>
> Given XML:
> <Types>
> <Type><range>1</range><name>Gast</name></Type>
> <Type><range>2</range><name>Rekt</name></Type>
> <Type><range>2</range><name>Sigm</name></Type>
> <!-- range is 1-6 -->
> </Types>
>
> I want to produce HTML output like this (grouped by range):
> <li>Gast</li>
> <li>Rekt, Sigm</li>
>
> well I can produce the single <li> element for 'Gast and 'Rekt, Sigm'
> output but I don't know how to iterate through all the 'range'
>
> I did sth like:
> 1) produce <li> for range='1'
> 2) produce <li> for range='2'
> ...
> 6) produce <li> for range='6'
>
> how to itrate through ranges from 1 to 6 automatically?
> I know that ranges are sorted, there cannot be 5 before 2 etc.
>
> this is my step-by-step version
> <xsl:template match="Types" mode="searching">
> <!-- do it for range='1' -->
> <li>
> <!-- variable needed for the comma -->
> <xsl:variable name="last">
> <xsl:value-of select="count(Type[./range='1'])"/></xsl:variable>
> <xsl:for-each select="Type[./range='1']">
> <xsl:value-of select="name"/>
> <xsl:if test="$last != position()">, </xsl:if>
> </xsl:for-each>
> </li>
> <li>
> <xsl:variable name="last">
> <xsl:value-of select="count(Type[./range='1'])"/></xsl:variable>
> <xsl:for-each select="Type[./range='1']">
> <xsl:value-of select="name"/>
> <xsl:if test="$last != position()">, </xsl:if>
> </xsl:for-each>
> </li>
> <!-- and for the rest of ranges 3, 4, 5, 6 the same -->
> </xsl:template>
>
> thanks for any help
> best regards R



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Including XSLT/XML document within a XSLT document dar_imiro@hotmail.com XML 4 12-13-2005 02:26 AM
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
xslt alone or xslt/java for static site? ted XML 1 01-26-2004 10:41 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57