这是我编辑后的建议,包含一些细微的变化,以确保我们para
在没有可分组的情况下处理所有元素:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf"
version="2.0">
<xsl:param name="group-role" select="'Box subhead '"/>
<xsl:output indent="yes"/>
<xsl:function name="mf:group" as="element()*">
<xsl:param name="paras" as="element(para)*"/>
<xsl:param name="head" as="xs:string"/>
<xsl:for-each-group select="$paras" group-starting-with="para[@role = concat($group-role, $head)]">
<xsl:choose>
<xsl:when test="self::para[@role = concat($group-role, $head)]">
<sec>
<xsl:apply-templates select="."/>
<xsl:sequence select="mf:group(current-group() except ., codepoints-to-string(string-to-codepoints($head)[1] + 1))"/>
</sec>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:function>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* , node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="boxed-text">
<xsl:copy>
<xsl:variable name="first-sh" select="para[@role = concat($group-role, 'A')][1]"/>
<xsl:apply-templates select="if ($first-sh) then $first-sh/preceding-sibling::para else para"/>
<xsl:sequence select="mf:group(($first-sh, $first-sh/following-sibling::para), 'A')"/>
</xsl:copy>
</xsl:template>
<xsl:template match="para[matches(@role, concat($group-role, '[A-Z]'))]">
<title>
<xsl:apply-templates/>
</title>
</xsl:template>
<xsl:template match="para[@role = 'Box legend']">
<caption>
<para>
<xsl:apply-templates/>
</para>
</caption>
</xsl:template>
<xsl:template match="para[@role = ('Box head', 'Box text')]">
<para>
<xsl:apply-templates/>
</para>
</xsl:template>
</xsl:stylesheet>
当应用于输入时
<boxed-text>
<para role="Box legend">Box 2 Caption</para>
<para role="Box head">Text Text Text</para>
<para role="Box text">Text Text Text.<sup>1</sup></para>
<para role="Box subhead A">Text Text Text</para>
<para role="Box text">Text Text Text.</para>
<para role="Box subhead A">Text Text Text</para>
<para role="Box text">Text Text Text.</para>
<para role="Box subhead B">Text Text Text</para>
<para role="Box text">Text Text Text.</para>
</boxed-text>
使用 Saxon 9.4 我得到了结果
<boxed-text>
<caption>
<para>Box 2 Caption</para>
</caption>
<para>Text Text Text</para>
<para>Text Text Text.<sup>1</sup>
</para>
<sec>
<title>Text Text Text</title>
<para>Text Text Text.</para>
</sec>
<sec>
<title>Text Text Text</title>
<para>Text Text Text.</para>
<sec>
<title>Text Text Text</title>
<para>Text Text Text.</para>
</sec>
</sec>
</boxed-text>
当应用于输入时
<boxed-text>
<para role="Box legend">Box 2 Caption</para>
<para role="Box head">Text Text Text</para>
<para role="Box text">Text Text Text.<sup>1</sup></para>
<para role="Box text">Text Text Text.</para>
<para role="Box text">Text Text Text.</para>
<para role="Box text">Text Text Text.</para>
</boxed-text>
我得到结果
<boxed-text>
<caption>
<para>Box 2 Caption</para>
</caption>
<para>Text Text Text</para>
<para>Text Text Text.<sup>1</sup>
</para>
<para>Text Text Text.</para>
<para>Text Text Text.</para>
<para>Text Text Text.</para>
</boxed-text>