Reputation: 349
Follow-up from insert page-break after all elements except the last, when position() is not reliable.
How would I achieve the following result (more info below):
<pages>
<page title="Manchuria">
<edit version="2"/>
<edit version="3"/>
</page>
<page-break/>
<page title="Manchuria">
<edit version="4"/>
<edit version="5"/>
</page>
<page-break/>
<page title="Manchuria">
<edit version="6"/>
<edit version="7"/>
</page>
<page-break/>
<page title="Perfect the first time"/>
<page-break/>
<page title="Zombie Librarian">
<edit version="2"/>
</page>
</pages>
From the following input:
<articles>
<article id="er113" title="Manchuria" publishable="true" version="7">
<original>
<article id="xc141" version="1"/>
</original>
<edits>
<article id="er111" version="2"/>
<article id="yu475" version="4"/>
<article id="iu931" version="3"/>
<article id="er111" version="5"/>
<article id="er112" version="6"/>
<article id="er113" version="7"/>
</edits>
</article>
<article id="ww555" title="Perfect the first time" publishable="true" version="1">
<original>
<article id="ww555" version="1"/>
</original>
<edits/>
</article>
<article id="nb741" title="Zombie Librarian" publishable="true" version="2">
<original>
<article id="nc441" version="1"/>
</original>
<edits>
<article id="nb741" version="2"/>
</edits>
</article>
</articles>
starting from the following xslt:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<pages>
<xsl:variable name="pages">
<xsl:apply-templates select="/articles/article[@publishable='true']" mode="subfilter"/>
</xsl:variable>
<xsl:apply-templates select="msxsl:node-set($pages)/article" mode="layout"/>
</pages>
</xsl:template>
<xsl:template match="article" mode="subfilter">
<xsl:variable name="id_of_latest_edit">
<xsl:for-each select="edits/article">
<xsl:sort data-type="number" select="@version"/>
<xsl:if test="position()=last()">
<xsl:value-of select="@id"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:if test="@id = $id_of_latest_edit or not(edits/article)">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
<xsl:template match="article" mode="layout">
<page title="{@title}">
<xsl:apply-templates select="edits/article" mode="editdetails"/>
</page>
<xsl:if test="position()!=last()">
<page-break/>
</xsl:if>
</xsl:template>
<xsl:template match="article" mode="editdetails">
<xsl:param name="editPos"/>
<edit version="{@version}"/>
</xsl:template>
</xsl:stylesheet>
The problem A page can have maximum n edit elements (n=2 in this example), and the edits need to be sorted by version.
What I tried Before the linked question was solved, I tried something along the lines of:
<xsl:template match="article" mode="subfilter">
<xsl:variable name="currentnode" select="."/>
<!-- always print -->
<xsl:apply-templates select="." mode="layout"/>
<!-- print extra pages when number of articles exceeds limit -->
<xsl:if test="count(edits/article) > 2">
<xsl:for-each select="edits/article[(position() mod 2) = 0]">
<xsl:apply-templates select="$currentnode" mode="layout"/>
</xsl:for-each>
</xsl:if>
</xsl:template>
which gets me the multiple pages and I would still need to filter and sort the edit elements. However, the new code (to get the page-breaks) uses the node-set() function and I can't seem to figure out how to merge this approach (if it is even a good idea to try). Is maybe muenchian grouping needed?
Upvotes: 0
Views: 30
Reputation: 117102
I suppose this is one way you could look at it:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/articles">
<pages>
<xsl:variable name="pages">
<xsl:apply-templates select="article[@publishable='true']" mode="preprocess"/>
</xsl:variable>
<xsl:apply-templates select="exsl:node-set($pages)/page"/>
</pages>
</xsl:template>
<xsl:template match="article[not(edits/article)]" mode="preprocess">
<page title="{@title}"/>
</xsl:template>
<xsl:template match="article" mode="preprocess">
<xsl:variable name="title" select="@title" />
<xsl:for-each select="edits/article">
<xsl:sort select="@version" data-type="number" order="ascending"/>
<xsl:if test="position() mod 2 = 1">
<page title="{$title}">
<edit version="{@version}"/>
<xsl:if test="position()!=last()">
<edit version="{@version + 1}"/>
</xsl:if>
</page>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="page">
<xsl:copy-of select="."/>
<xsl:if test="position()!=last()">
<page-break/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Note that this assumes versions are numbered consecutively.
Upvotes: 1