Question

I have a Library object that contains a collection of Books... The Library object has properties like Name, Address, Phone... While the Book object has properties like ISDN, Title, Author, and Price.

XML looks something like this...

<Library>
    <Name>Metro Library</Name>
    <Address>1 Post Rd. Brooklyn, NY 11218</Address>
    <Phone>800 976-7070</Phone>
    <Books>
        <Book>
            <ISDN>123456789</ISDN>
            <Title>Fishing with Luke</Title>
            <Author>Luke Miller</Author>
            <Price>18.99</Price>
        </Book>
        <Book>
            <ISDN>234567890</ISDN>
            <Title>Hunting with Paul</Title>
            <Author>Paul Worthington</Author>
            <Price>28.99</Price>
        </Book>
        ...
        And more books
        ...
    </Books>
</Library>

I have a template with space for only 10 per page for example. There can be hundreds of books in the list of Books... So I need to limit the number of books and repeat the template every 10 books.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <div>
                    <table>
                        <tr>
                            <td>NAME</td>
                            <td><xsl:value-of select="/Library/Name"/></td>              
                        </tr>
                  <tr>
                            <td>ADDRESS</td>
                            <td><xsl:value-of select="/Library/Address"/></td>              
                        </tr>
                        <tr>
                            <td>PHONE</td>
                            <td><xsl:value-of select="/Library/Phone"/></td>              
                        </tr>
                   </table>
                   <table>
                       <xsl:for-each select="/Library/Books/Book">
                           <tr>
                               <td><xsl:value-of select="position()"/></td>
                               <td><xsl:value-of select="ISDN"/></td>
                               <td><xsl:value-of select="Title"/></td>
                               <td><xsl:value-of select="Author"/></td>
                               <td><xsl:value-of select="Price"/></td>                
                           </tr>
                       </xsl:for-each>
                   </table>
               </div>
           </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

How can I get the Library information to appear on all repeating pages and add 10 books per page?... First page has Library info with Books 1 thru 10, Second page has Library info with Books 11 thru 20, and so on??

Thanks

Était-ce utile?

La solution

For starters, try not to use for-each, apply-templates allows the engine to optimise the order that events are processed.

It appears that you are calling this stylesheet from some other system, so the approach I've taken is to define a pagination param. In the host language, when you call this just change the root parameter. This then allows you to select the require pages in this line here:

Books/Book[($page - 1)*10 &lt; position() and position() &lt;= ($page)*10]

This should do the trick.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:param name="page" select="1"/>
  <xsl:template match="/Library">
    <html>
        <body>
            <div>
                <table>
                    <tr>
                        <td>NAME</td>
                        <td>
                            <xsl:value-of select="/Name"/>
                        </td>
                    </tr>
                    <tr>
                        <td>ADDRESS</td>
                        <td>
                            <xsl:value-of select="/Address"/>
                        </td>
                    </tr>
                    <tr>
                        <td>PHONE</td>
                        <td>
                            <xsl:value-of select="/Phone"/>
                        </td>
                    </tr>
                </table>
                <table>
                    <xsl:apply-templates select="Books/Book[($page - 1)*10 &lt; position() and position() &lt;= ($page)*10]"/>
                </table>
            </div>
        </body>
    </html>
  </xsl:template>
  <xsl:template match="/Book">
    <tr>
        <td>
            <xsl:value-of select="position()"/>
        </td>
        <td>
            <xsl:value-of select="ISDN"/>
        </td>
        <td>
            <xsl:value-of select="Title"/>
        </td>
        <td>
            <xsl:value-of select="Author"/>
        </td>
        <td>
            <xsl:value-of select="Price"/>
        </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top