将元素分成 n 个子元素组(69940580 的跟进)

Splitting up elements into groups of n child elements (follow-up from 69940580)

来自 的跟进。

如何实现以下结果(更多信息见下文):

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

来自以下输入:

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

从以下 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>

问题 一个页面最多可以有n个编辑元素(本例中为n=2),编辑需要按版本排序[=15] =]

我试过的 在解决链接问题之前,我尝试了以下方法:

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

这让我获得了多个页面,但我仍然需要对编辑元素进行过滤和排序。但是,新代码(获取分页符)使用 node-set() 函数,我似乎无法弄清楚如何合并这种方法(如果尝试是个好主意的话)。可能需要 muenchian 分组吗?

我想这是您可以查看的一种方式:

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>

请注意,这假定版本是连续编号的。