Группировка нескольких групп в XSLT 2
Я пытаюсь добавить иерархию в какой-то грязный экструдированный наборный XML. Я не могу управлять группированием нескольких видов групп в одном родительском элементе одновременно.
Что я имею (упрощенно, очевидно):
<article>
<h1>A section title here</h1>
<p>A paragraph.</p>
<p>Another paragraph.</p>
<bl>Bulleted list item.</bl>
<bl>Another bulleted list item.</bl>
<h1>Another section title</h1>
<p>Yet another paragraph.</p>
</article>
Что я хочу:
<article>
<sec>
<h1>A section title here</h1>
<p>A paragraph.</p>
<p>Another paragraph.</p>
<list>
<list-item>Bulleted list item.</list-item>
<list-item>Another bulleted list item.</list-item>
</list>
</sec>
<sec>
<h1>Another section title</h1>
<p>Yet another paragraph.</p>
</sec>
</article>
Это почти работает для элементов списка:
<xsl:for-each-group select="*" group-adjacent="boolean(self::BL)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<list><xsl:apply-templates select="current-group()"/></list>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
но он обрабатывает только самый первый список в статье; и как только я пытаюсь добавить еще одну группу xsl:for-each-group для покрытия разделов, элемент списка 1 перестает работать.
Идеи? Спасибо заранее!
1 ответ
Решение
Вот примерная таблица стилей, которая производит вывод, который вы опубликовали для входного образца, который вы опубликовали:
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="article">
<xsl:copy>
<xsl:for-each-group select="*" group-starting-with="h1">
<sec>
<xsl:copy-of select="."/>
<xsl:for-each-group select="current-group() except ." group-adjacent="boolean(self::bl)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<list>
<xsl:apply-templates select="current-group()"/>
</list>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</sec>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="bl">
<list-item>
<xsl:apply-templates/>
</list-item>
</xsl:template>
</xsl:stylesheet>