 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 19th, 2011, 02:15 PM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Can I do this in xslt
Hello,
I have found another better script here : http://www.bobulous.org.uk/coding/paging-xslt.html
What I want to achieve is that if I on let's say page 2 so $page=2 then the variable Itemsperpage will be altered to let's say 2.
I hope it's clear what I want.
Roelof
|
|

November 19th, 2011, 05:50 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
No, don't understand. $itemsPerPage is how many items you want per page, correct? Well just set it to whatever you want in the XSLT file.
If this isn't what you meant then you'll have to be more clear.
|
|

November 20th, 2011, 05:33 AM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I try to say it another way.
At this moment the script produce this output :
page 1 4 articles
page 2 4 articles
page 3 4 articles
What I want is that it produce this output as a example:
page 1 5 articles
page 2 3 articles
page 3 4 articles
I hope it's clear now.
|
|

November 20th, 2011, 06:00 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Well if you know exactly how many articles you have, and how many you want per page then you could change $itemsPerPage to a sequence:
<xsl:variable name="itemsPerPage" select="5,3,4"/>
and then to get the number of items per page for a given page do $itemsPerPage[$page] (assuming page 1 is $page=1 etc)
|
|

November 20th, 2011, 06:07 AM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I understand that part.
But there is another problem.
I have this site ( http://www.tamarawobben.nl).
As you can see everything is displayed per month in a menu.
Now I want to make this site in xml and xslt with this menu ( http://www.mathachew.com/sandbox/collapsible-menu/).
So I can't know which sequence the reader reads the website so I think the solution cannot work. The month is stored in a parameter $value in Symphony CMS.
Roelof
|
|

November 20th, 2011, 07:47 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Hi
We'd very much like to help, but the help we provide on this forum is where you show us some input XML, what you want the output XML or HTML to look like, and we help you write a XSLT stylesheet to convert one to the other.
We know nothing about Symphony CMS, nor do we know how the data for this web site is arranged. You'll have to give us more to go on.
|
|

November 20th, 2011, 08:15 AM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Oke, that's fair.
I have this xml of the siet till now.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<data>
<params>
<today>2011-11-20</today>
<current-time>13:12</current-time>
<this-year>2011</this-year>
<this-month>11</this-month>
<this-day>20</this-day>
<timezone>+01:00</timezone>
<website-name>Tamara Wobben</website-name>
<page-title>Frontpage</page-title>
<root>http://test.tamarawobben.nl</root>
<workspace>http://test.tamarawobben.nl/workspace</workspace>
<root-page>frontpage</root-page>
<current-page>frontpage</current-page>
<current-page-id>1</current-page-id>
<current-path>/?debug</current-path>
<parent-path>/</parent-path>
<current-url>http://test.tamarawobben.nl/?debug</current-url>
<upload-limit>2097152</upload-limit>
<symphony-version>2.2.4</symphony-version>
<cookie-username>xxx</cookie-username>
<cookie-pass>xxxxx</cookie-pass>
</params>
<events />
<display-articles>
<section id="1" handle="blog">Blog</section>
<entry id="1">
<titel handle="zwanger">Zwanger ??</titel>
<datum time="23:00" weekday="5">2005-02-04</datum>
<tekst mode="formatted"><p>Hoera, het is zover, eindelijk zwanger. De test laat niets te twijfelen over. Ik ben net een stuiterbal, blij en energie voor tien. Ondanks het prille begin, heb ik een goed gevoel over deze zwangerschap. Waarom? geen idee. Het voelt gewoon goed. Het is moeilijk om âs middags mijn mond dicht te houden als ik met mijn zus in de stad ben. Het is maar goed dat er een carnavalsoptocht is, het geeft een beetje afleiding.</p>
</tekst>
</entry>
<entry id="2">
<titel handle="7-weken-echo">7 weken echo</titel>
<datum time="22:00" weekday="1">2005-02-21</datum>
<tekst mode="formatted"><p>Ik stond al onder behandeling van een gynaecoloog en heb een echo gehad om te kijken of alles goed met je is. Ons kleintje (zo noem ik je) je bent 7 weken en 6,5 millimeter, onvoorstelbaar dat je nu al jouw hartje kunt zien kloppen. De vermoedelijke bevallingsdatum wordt op 9 oktober 2005 vastgesteld, Tjonge wat klinkt dat nog ver weg. De energie voor 10 was van korte duur, veel slaap is ervoor in de plaats gekomen, âs avonds om acht uur rol ik om van de slaap. Gekke gewoonte: waterijs, ik kan er niet vanaf blijven, spekjes en zuurtjes. Waar ik een hekel aan heb gekregen, kip snijden en bakken en de geur van wokolie. Bah, wat smerig.</p>
</tekst>
</entry>
<entry id="3">
<titel handle="appelgebak">Appelgebak</titel>
<datum time="23:00" weekday="6">2005-02-26</datum>
<tekst mode="formatted"><p>Met appelgebak zijn we naar jouw opa Mans en oma Marie gegaan. Ze zijn blij voor ons en ik hoop dat je veel plezier zult hebben met je opa Mans en oma Marie zodra je bent geboren. Nu nog opa Jan en Dagmar vertellen dat je op komst bent</p>
</tekst>
</entry>
<entry id="4">
<titel handle="9-weken-echo">9 weken echo</titel>
<datum time="22:00" weekday="3">2005-03-09</datum>
<tekst mode="formatted"><p>Opnieuw is er een echo gemaakt en dit keer kon papa met me mee. Inmiddels ben je 9 weken en op de echo was een mooi kloppend hartje te zien, je bent nu 2,5 centimeter. Wij genieten met volle teugen, ik ben goed te pas, geen last van misselijkheid, alleen maar moe.
Wij krijgen de kans om een tripletest en nekplooimeting te laten doen, en dat gaat binnenkort gebeuren. Jij bent hoe dan ook welkom, maar als er iets met je is willen we dat graag weten</p>
</tekst>
</entry>
<entry id="5">
<titel handle="tripletest">Tripletest</titel>
<datum time="23:00" weekday="3">2005-03-16</datum>
<tekst mode="formatted"><p>Alvast bloed laten prikken voor de tripletest. De uitstag is dan binnen als de nekplooimeting wordt gedaan. Sinds kort kan dat in het S.M.T. in Hengelo, dus dat is mooi meegenomen. Nog niet veel mensen weten dat je in mijn buik aan het groeien bent, maar ik had het al wel tegen een vriendin verteld en het leuke was dat ik de week erna een hele stapel broeken te leen kreeg en een stapel Wij jonge ouders. Dus lees ik volop. Heerlijk om in weg te dromen en ik ben benieuwd hoe jij eruit zult zien.</p>
</tekst>
</entry>
</display-articles>
</data>
What I try to achieve is that the output will be of a month divided into pages.
But I want to decide how many articles are on the page. And between the pages I want to use prevoius / next so a reader can go to the next or the previous page.
Roelof
Last edited by roelof; November 20th, 2011 at 08:19 AM..
|
|

November 20th, 2011, 01:13 PM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello,
I did a little googeling and could this work.
Code:
<xsl:choose>
<xsl:when test="value eq 2005-02">
<xsl:variable name="itemsPerPage" select="5,3,4"/>
<xsl:otherwise>
<xsl:when test="$value eq 2005-03">
<xsl:variable name="itemsPerPage" select="1,2,3"/>
</xsl:when>
</xsl:otherwise>
</xsl:when>
</xsl:choose>
Roelof
|
|

November 21st, 2011, 03:38 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
The variable you declare would not be available outside of the xsl:choose element.
Also, the when/otherwise is like a switch statement. Each "when" has a test, and then finally there is an "otherwise" element for the cases that match none of the test.
Try this instead:
Code:
<xsl:variable name="itemsPerPage"/>
<xsl:choose>
<xsl:when test="value eq 2005-02">
<xsl:value-of select="5,3,4"/>
</xsl:when>
<xsl:when test="$value eq 2005-03">
<xsl:value-of select="1,2,3"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="3,2,1"/>
</xsl:otherwise>
</xsl:when>
</xsl:choose>
|
|

November 21st, 2011, 08:22 AM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I don't give the output as expected.
When I use this xml file as a test file.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This sample file is for use with the test-xml-paging.xslt template. -->
<!-- For more information, see http://www.bobulous.org.uk/coding/paging-xslt.html -->
<items>
<item>
<title>T1</title>
<summary>Great.</summary>
<value>2005-02</value>
</item>
<item>
<title>T2</title>
<summary>Better than the first?</summary>
<value>2005-02</value>
</item>
<item>
<title>T3</title>
<summary>Definitely not as good as the first two.</summary>
<value>2005-02</value>
</item>
<item>
<title>T4</title>
<summary>Bloody dire by all accounts.</summary>
<value>2005-02</value>
</item>
<item>
<title>T5</title>
<summary>Who knows?</summary>
<value>2005-02</value>
</item>
<item>
<title>T6</title>
<summary>Let's hope not.</summary>
<value>2005-02</value>
</item>
<item>
<title>T7</title>
<summary>Ah, come on now.</summary>
<value>2005-03</value>
</item>
<item>
<title>T8</title>
<summary>You gotta be kidding me!</summary>
<value>2005-03</value>
</item>
<item>
<title>T9</title>
<summary>Seriously?!!</summary>
<value>2005-03</value>
</item>
<item>
<title>T10</title>
<summary>I'm not even listening any more.</summary>
<value>2005-03</value>
</item>
<item>
<title>T11</title>
<summary>Go away!</summary>
<value>2005-03</value>
</item>
<item>
<title>T12</title>
<summary>Who is still paying for this rubbish?</summary>
<value>2005-03</value>
</item>
<item>
<title>T13</title>
<summary>Why won't you just die?!?!?</summary>
<value>2005-03</value>
</item>
</items>
And use this xslt :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This XSLT template is a demonstration of how to generate pages of results, and is intended to be used with the sample file test-xml-item-file.xml -->
<!-- For more information, see http://www.bobulous.org.uk/coding/paging-xslt.html -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml" version="2.0">
<!-- Define how many items should appear on each page -->
<xsl:variable name="itemsPerPage" select="4"/>
<!-- Store total item count in a variable -->
<xsl:variable name="itemCount" select="count(/items/item)"/>
<!-- Calculate how many pages will be needed in total -->
<xsl:variable name="pagesNeeded" select="ceiling($itemCount div $itemsPerPage)"/>
<!-- I'm using an XHTML/UTF-8 output format, but make sure to change your template so that it outputs to the format you require. -->
<xsl:output name="XHTML" method="xhtml" version="1.0" encoding="UTF-8" indent="no" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" omit-xml-declaration="yes"/>
<!-- Master template -->
<xsl:template match="/">
<!-- Gather the item elements into groups (of size $itemsPerPage) -->
<xsl:variable name="value" select=""/>
<xsl:variable name="itemsPerPage"/>
<xsl:choose>
<xsl:when test="$value eq 2005-02">
<xsl:value-of select="5,3,4"/>
</xsl:when>
<xsl:when test="$value eq 2005-03">
<xsl:value-of select="1,2,3"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="3,2,1"/>
</xsl:otherwise>
</xsl:choose>
<!-- Construct a filename for the page we're working on -->
<xsl:variable name="pageName">
<xsl:choose>
<xsl:when test="position() eq 1">
<xsl:text>index.html</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>index_p</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>.html</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- Produce the output document (with the constructed filename) -->
<xsl:result-document href="{$pageName}" format="XHTML">
<!-- Begin XHTML page structure (for item listing page) -->
<xsl:element name="html">
<xsl:attribute name="xml:lang" select="'en'"/>
<xsl:attribute name="lang" select="'en'"/>
<!-- Add a comment to warn that this page is automatically generated, and must not be edited by hand (because edits will simply be overwritten next time XSLT is run. -->
<xsl:comment>
!!! This page is generated by XSLT stylesheet !!! DO NOT EDIT THIS PAGE DIRECTLY !!!
</xsl:comment>
<xsl:element name="head"><xsl:element name="title">Item listing (page <xsl:value-of select="position()"/>)</xsl:element></xsl:element><!-- end of head -->
<xsl:element name="body">
<xsl:element name="h1">Items (page <xsl:value-of select="position()"/>)</xsl:element>
<xsl:element name="p">This page is a test of the <xsl:element name="a"><xsl:attribute name="href">http://www.bobulous.org.uk/coding/paging-xslt.html</xsl:attribute>XSLT paging template</xsl:element>.</xsl:element>
<xsl:element name="p">You are on page <xsl:value-of select="position()"/> of <xsl:value-of select="$pagesNeeded"/>.</xsl:element>
<!-- For each item in this current group (i.e. on this result page) apply the summarise-item mode template (see further down in this file) -->
<xsl:for-each select="current-group()">
<xsl:apply-templates select="." mode="summarise-item"/>
</xsl:for-each>
<!-- Call the "prev-and-next" template (see further down) to add "previous page" and "next page" links -->
<xsl:call-template name="prev-and-next">
<xsl:with-param name="currentPage" select="position()"/>
<xsl:with-param name="totalPages" select="$pagesNeeded"/>
</xsl:call-template>
</xsl:element><!-- end of body element -->
</xsl:element><!-- end of html element -->
</xsl:result-document>
</xsl:template>
<!-- Define a mode template which generates a summary for an item element -->
<xsl:template match="item" mode="summarise-item">
<!-- Simply write an h2 element and p element for each item -->
<xsl:element name="h2">
<xsl:value-of select="title"/>
</xsl:element>
<xsl:element name="p">
<xsl:value-of select="summary"/>
</xsl:element>
</xsl:template>
<!-- Define a named template which produces "previous page" and "next page" links based on the page parameter values passed to it -->
<xsl:template name="prev-and-next">
<xsl:param name="currentPage"/>
<xsl:param name="totalPages"/>
<xsl:element name="div">
<xsl:if test="$totalPages > 1">
<xsl:element name="hr"/>
</xsl:if>
<xsl:element name="p">
<xsl:if test="$currentPage > 1">
<xsl:choose>
<xsl:when test="$currentPage eq 2">
<xsl:element name="a"><xsl:attribute name="href">index.html</xsl:attribute>Previous page</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="a"><xsl:attribute name="href">index_p<xsl:value-of select="$currentPage - 1"/>.html</xsl:attribute>Previous page</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<!-- Add a little spacer symbol between the "next" and "previous" links, if both are present -->
<xsl:if test="$currentPage > 1 and $currentPage < $totalPages"> ↔ </xsl:if>
<xsl:if test="$currentPage < $totalPages">
<xsl:element name="a"><xsl:attribute name="href">index_p<xsl:value-of select="$currentPage + 1"/>.html</xsl:attribute>Next page</xsl:element>
</xsl:if>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Then I see first a page with numbers and a page with some text but not the summarise.
Roelof
|
|
 |