Iteration, or copying in XSL, based on a count. How do I do it?

Posted by LOlliffe on Stack Overflow See other posts from Stack Overflow or by LOlliffe
Published on 2010-05-12T19:39:02Z Indexed on 2010/05/12 20:04 UTC
Read the original article Hit count: 288

Filed under:
|
|
|

I'm trying to create an inventory list (tab-delimited text, for the output) from XML data. The catch is, I need to take the line that I created, and list it multiple times (iterate ???), based on a number found in the XML. So, from the XML below:

<?xml version="1.0" encoding="UTF-8"?>
<library>
  <aisle label="AA">
    <row>bb</row>
    <shelf>a</shelf>
    <books>4</books>
  </aisle>
  <aisle label="BB">
    <row>cc</row>
    <shelf>d</shelf>
    <books>3</books>
  </aisle>
</library>

I need to take the value found in "books", and then copy the text line that number of times. The result looking like this:

Aisle   Row Shelf   Titles
AA     bb   a   
AA     bb   a   
AA     bb   a   
AA     bb   a   
BB     cc   d   
BB     cc   d   
BB     cc   d   

So that the inventory taker, can then write in the titles found on each shelf. I have the basic structure of my XSL, but I'm not sure how to do the "iteration" portion.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="2.0">

<xsl:output omit-xml-declaration="yes"/>
<xsl:variable name="tab" select="'&#09;'"/>
<xsl:variable name="newline" select="'&#10;'"/>

<xsl:template match="/">
    <!-- Start Spreadsheet header -->
    <xsl:text>Aisle</xsl:text>
    <xsl:value-of select="$tab"/>
    <xsl:text>Row</xsl:text>
    <xsl:value-of select="$tab"/>
    <xsl:text>Shelf</xsl:text>
    <xsl:value-of select="$tab"/>
    <xsl:text>Titles</xsl:text>
    <xsl:value-of select="$newline"/>
    <!-- End spreadsheet header -->

    <!-- Start entering values from XML -->
    <xsl:for-each select="library/aisle">
        <xsl:value-of select="@label"/>
        <xsl:value-of select="$tab"/>
        <xsl:value-of select="row"/>
        <xsl:value-of select="$tab"/>
        <xsl:value-of select="shelf"/>
        <xsl:value-of select="$tab"/>
        <xsl:value-of select="$tab"/>
        <xsl:value-of select="$newline"/>
    </xsl:for-each>
    <!-- End of values from XML -->

    <!-- Iteration of the above needed, based on count value in "books" -->

</xsl:template>
</xsl:stylesheet>

Any help would be greatly appreciated. For starters, is "iteration" the right term to be using for this?

Thanks!

© Stack Overflow or respective owner

Related posts about iteration

Related posts about xslt