首页 > 解决方案 > 对来自两个不同父母的项目进行分组/分页

问题描述

我是 XSLT 的新手,自 90 年代后期以来就没有接触过它,但我最近开始了一个涉及它的个人项目。

我有以下 XML,我正在尝试从中构建 HTML 字符表:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="./Statblock.xslt"?>
<Character>
 <Name value="Seiyatomo"/>
 <Family value="Soshi"/>
 <Clan value="Scorpion"/>
 <School value="Soshi Illusionist School"/>
 <Titles/>
 <Ninjo value=""/>
 <Giri value=""/>
 <Abilities>
  <Ability description="" name="The Kami's Whisper" />
 </Abilities>
 <Skills>
  ...
 </Skills>
 <Rings>
  ...
 </Rings>
 <Social glory="50" honor="35" status="35"/>
 <Wealth bu="0" koku="6" zeni="0"/>
 <Derived composure="8" endurance="4" focus="4" vigilance="3"/>
 <RankStatus titlestatus="Title: , Title XP: 0" curricstatus="Rank: 1, XP in Rank: 0"/>
 <Curriculum>
    ...
 </Curriculum>
 <Title/>
 <Techniques>
  <Technique name="Bō of Water" />
  <Technique name="Cloak of Night" />
  <Technique name="Token of Memory" />
  <Technique name="Commune with the Spirits" />
  <Technique name="All in Jest" />
  <Technique name="Dangerous Allure" />
  <Technique name="Shadowlands Taint (Water)" />
  <Technique name="Curiosity" />
  <Technique name="Dark Secret" />
  <Technique name="Fallen Ancestor" />
 </Techniques>
 <PersonalTraits/>
 <Equipment>
    ...
 </Equipment>
 <Heritage value="Glorious Sacrifice"/>
 <Notes value="..."/>
 <Advances/>
 <TotalXP value="0"/>
 <XPSpent value="0"/>
 <Portrait base64image=""/>
</Character>

我正在尝试将能力和技术节点的组合列表分组为 9 组,这样我将获得类似于以下内容的输出:

<page>
  <ability>The Kami's Whisper</ability>
  <technique>Bō of Water</technique>
  <technique>Cloak of Night</technique>
  <technique>Token of Memory</technique>
  <technique>Commune with the Spirits</technique>
  <technique>All in Jest</technique>
  <technique>Dangerous Allure</technique>
  <technique>Shadowlands Taint (Water)</technique>
  <technique>Curiosity</technique>
</page>
<page>
  <technique>Dark Secret</technique>
  <technique>Fallen Ancestor</technique>
</page>

XML 中 <Ability> 和 <Technique> 节点的数量是任意的,但我希望所有 <ability> 节点在输出中排在第一位,然后是 <technique> 节点。

奖励积分,如果最后一个 <page> 可以充满空的 <technique> 条目以将页面填充到完整的 9 个集合,但这是次要问题。

我尝试搜索类似的问题,但要么我没有看到任何类似的问题,要么我没有充分理解问题/答案以将它们识别为重复。

注意:如果需要,可以让生成 XML 的应用程序的作者对 XML 进行一些更改,但我不能直接控制 XML 本身。

标签: xmlxslt

解决方案


至少第一部分看起来很简单:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Character">
    <xsl:copy>
        <xsl:copy-of select="Name" />  
        <xsl:call-template name="paginate">
            <xsl:with-param name="nodes" select="Abilities/Ability | Techniques/Technique"/>
        </xsl:call-template>    
    </xsl:copy>
</xsl:template>

<xsl:template match="Ability">
    <ability>
        <xsl:value-of select="@name"/>
    </ability>
</xsl:template>

<xsl:template match="Technique">
    <technique>
        <xsl:value-of select="@name"/>
    </technique>
</xsl:template>

<xsl:template name="paginate">
    <xsl:param name="nodes"/>
    <xsl:param name="pagesize" select="9"/>
    <page>
        <xsl:apply-templates select="$nodes[position() &lt;= $pagesize]"/>
    </page>
    <xsl:if test="count($nodes) > $pagesize">
        <xsl:call-template name="paginate">
            <xsl:with-param name="nodes" select="$nodes[position() > $pagesize]"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

在XSLT 2.0中更简单:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Character">
    <xsl:copy>
        <xsl:copy-of select="Name" /> 
        <xsl:for-each-group select="Abilities/Ability | Techniques/Technique" group-by="(position()-1) idiv 9">
            <page>
                <xsl:apply-templates select="current-group()"/>
            </page>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

<xsl:template match="Ability | Technique">
    <xsl:element name="{lower-case(name())}">
        <xsl:value-of select="@name"/>
    </xsl:element>      
</xsl:template>

</xsl:stylesheet>

对于第二部分,只需添加(在 XSLT 2.0 中):

<xsl:for-each select="1 to 9 - count(current-group())">
    <technique/>
</xsl:for-each>

演示https ://xsltfiddle.liberty-development.net/gWvjQgq


推荐阅读