views:

144

answers:

2

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!

+1  A: 

Here is a simple, recursive XSLT 1.0 solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="text"/>

 <xsl:template match="/*">
Aisle   Row Shelf   Titles&#xA;<xsl:text/>
  <xsl:apply-templates/>
 </xsl:template>

 <xsl:template match="aisle">
   <xsl:call-template name="makeRows">
     <xsl:with-param name="pText"
     select="concat(@label, '&#9;', row, '&#9;', shelf)"/>
     <xsl:with-param name="pNumRows" select="books"/>
   </xsl:call-template>
 </xsl:template>

 <xsl:template name="makeRows">
  <xsl:param name="pText"/>
  <xsl:param name="pNumRows" select="0"/>

  <xsl:if test="$pNumRows > 0">
    <xsl:value-of select="concat($pText, '&#xA;')"/>

    <xsl:call-template name="makeRows">
      <xsl:with-param name="pText" select="$pText"/>
      <xsl:with-param name="pNumRows" select="$pNumRows -1"/>
    </xsl:call-template>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<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>

the wanted, correct result is produced:

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
Dimitre Novatchev
Hmm, I just saw the XSL 2.0 version. Maybe I should try that. With this version, I'm getting a "fatal" error, at <xsl:if test="$pNumRows > 0">Telling me, "Required item type of first operand of '>' is numeric. Cannot convert string "" to a double"Do I need to indicate that it's something along the lines of type="numeric"somewhere?
LOlliffe
P.S. Although, even with the "fatal" error, it does seem to be working. ???
LOlliffe
+1  A: 

And an even simpler XSLT 2.0 non-recursive solution:

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  exclude-result-prefixes="xs" 
  >
  <xsl:output method="text"/>

  <xsl:template match="/*">
    Aisle   Row Shelf   Titles&#xA;<xsl:text/>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="aisle">
    <xsl:variable name="vText" select=
      "concat(@label, '&#9;', row, '&#9;', shelf)"
    />

    <xsl:for-each select="1 to xs:integer(books)">
      <xsl:value-of select="concat($vText, '&#xA;')"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Dimitre Novatchev
So strange. That is, my real XSL is much more complex than my example, and because of that "books" is actually a variable (more like $bookCount), which in this 2.0 is also giving me an error 'Cannot convert zero-length string to an integer', but also, just like the 1.0 version, is still giving me the results I need. In any case, it seems to work, so THANK YOU!
LOlliffe