基于属性xslt-2.0进行分组

时间:2012-12-19 06:32:30

标签: xslt-2.0

是否可以转换此

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

这样的事情?

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

我很难用xsl:for-each-group完成这项工作。答案将非常感谢。提前谢谢!

1 个答案:

答案 0 :(得分:0)

以下是我编辑过的建议,其中包含一些细微的更改,以确保在没有任何内容组合时处理所有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>