group-starting-with 没有像我预期的那样工作

group-starting-with not working as I expect it to

我正在尝试将 cals table 转换为一系列列表,但我正在努力进行分组。

table 由几个标题行 (thead/row) 和一堆 body 行 (tbody/row) 组成。 body 行有两种类型: a) 标准数据行,以及 b) 一个跨式标题行,'break up' table 在视觉上描绘了每个部分。

我想为每个部分创建一个新列表,所以我正在使用

<xsl:for-each-group group-starting-with="row/entry/hd4" select="table/tgroup/tbody/row">

我在 group-starting-with 属性中尝试了各种选项,包括

entry/@nameend
entry[@nameend]
entry/hd4
entry[hd4]

根据我的背景阅读,我注意到该值应该是 pattern,而不是 condition,所以我'我猜谓词是错误的。

<anch fac="yes">
   <p/>
   <calstable>
      <table frame="none">
         <tgroup cols="4" colsep="0" rowsep="0">
            <colspec colname="1" colnum="1" colwidth="39pt" align="center"/>
            <colspec colname="2" colnum="2" colwidth="39pt" align="center"/>
            <colspec colname="3" colnum="3" colwidth="45pt" align="center"/>
            <colspec colname="4" colnum="4" colwidth="33pt" align="center"/>
            <thead>
               <row valign="bottom">
                  <entry align="center">Anchorage</entry>
                  <entry align="center">Lat.</entry>
                  <entry align="center">Long.</entry>
                  <entry align="center">Draft</entry>
               </row>
               <row valign="bottom">
                  <entry align="center">No.</entry>
                  <entry align="center">(S)</entry>
                  <entry align="center">(E)</entry>
                  <entry align="center">(m.)</entry>
                  <entry> </entry>
               </row>
            </thead>
            <tbody>
               <row>
                  <entry align="left" namest="1" nameend="4"> 
                     <hd4>Outer</hd4> 
                  </entry>
               </row>
               <row>
                  <entry>O1</entry>
                  <entry>17° 57.0&#8242;</entry>
                  <entry>122° 04.0&#8242;</entry>
                  <entry>9.5</entry>
               </row>
               <row>
                  <entry>O2</entry>
                  <entry>17° 56.0&#8242;</entry>
                  <entry>122° 04.0&#8242;</entry>
                  <entry>9.5</entry>
               </row>
               <row>
                  <entry>O3</entry>
                  <entry>17° 55.0&#8242;</entry>
                  <entry>122° 04.0&#8242;</entry>
                  <entry>7.0</entry>
               </row>
               <row>
                  <entry align="left" namest="1" nameend="4"> 
                     <hd4>Gantheaume Bay</hd4> 
                  </entry>
               </row>
               <row>
                  <entry>G1</entry>
                  <entry>17° 56.5&#8242;</entry>
                  <entry>122° 10.0&#8242;</entry>
                  <entry>8.0</entry>
               </row>
               <row>
                  <entry>G2</entry>
                  <entry>17° 55.0&#8242;</entry>
                  <entry>122° 10.0&#8242;</entry>
                  <entry>8.0</entry>
               </row>
               <row>
                  <entry>G2</entry>
                  <entry>17° 56.5&#8242;</entry>
                  <entry>122° 09.5&#8242;</entry>
                  <entry>8.0</entry>
               </row>
            </tbody>
         </tgroup>
      </table>
   </calstable>
</anch>

和如下的xslt

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="2.0"
>    
    <xsl:output indent="yes" method="xml" standalone="yes"/>

    <xsl:template match="/">
        <anch>
            <xsl:apply-templates select="anch/calstable" mode="cals-to-list"/>
        </anch>
    </xsl:template>

    <xsl:template match="calstable[//thead[count(row) = 2]]" mode="cals-to-list">
        <xsl:for-each-group group-starting-with="row/entry/hd4" select="table/tgroup/tbody/row">
            <list>
                <xsl:apply-templates select="current-group()" mode="cals-to-list"/>
            </list>
        </xsl:for-each-group>
     </xsl:template>

    <xsl:template match="tbody/row[entry[@nameend]]" mode="cals-to-list">
        <xsl:variable name="lhtype">
            <xsl:choose>
                <xsl:when test="entry/hd2">2</xsl:when>
                <xsl:when test="entry/hd3">3</xsl:when>
                <xsl:when test="entry/hd4">4</xsl:when>
                <xsl:otherwise>2</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <lh lhtype="{$lhtype}">
           <xsl:apply-templates select="entry[@nameend]" mode="cals-to-list"/>
        </lh>
    </xsl:template>


    <xsl:template match="tbody/row[not(entry[@nameend])]" mode="cals-to-list">
        <li>
            <xsl:apply-templates select="entry" mode="cals-to-list"/>
        </li>
    </xsl:template>

    <xsl:template match="row/entry[@nameend]" mode="cals-to-list">
        <xsl:value-of select="*/text()"/>
    </xsl:template>

    <xsl:template match="row/entry[not(@nameend)]" mode="cals-to-list">
        <xsl:variable name="pos" select="position()"/>
        <xsl:variable name="thead-rows" select="count(ancestor::*[local-name()='tgroup']/thead/row)"/>
        <xsl:variable name="top-line" select="normalize-space(ancestor::*[local-name()='tgroup']/thead/row[position() != $thead-rows]/entry[$pos]/text())"/>
        <xsl:variable name="bot-line" select="normalize-space(ancestor::*[local-name()='tgroup']/thead/row[$thead-rows]/entry[$pos]/text())"/>
        <xsl:variable name="unit" select="normalize-space(substring-before(substring-after($bot-line,'('),')'))"/>
        <xsl:value-of select="$top-line"/><xsl:text> </xsl:text><xsl:value-of select="."/><xsl:text> </xsl:text><xsl:value-of select="$unit"/><xsl:text> </xsl:text>
    </xsl:template>



 </xsl:stylesheet>

这就是我得到的

<anch xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <list>
      <lh lhtype="4">Outer</lh>
      <li>Anchorage O1  Lat. 17° 57.0′ S Long. 122° 04.0′ E Draft 9.5 m. </li>
      <li>Anchorage O2  Lat. 17° 56.0′ S Long. 122° 04.0′ E Draft 9.5 m. </li>
      <li>Anchorage O3  Lat. 17° 55.0′ S Long. 122° 04.0′ E Draft 7.0 m. </li>
      <lh lhtype="4">Gantheaume Bay</lh>
      <li>Anchorage G1  Lat. 17° 56.5′ S Long. 122° 10.0′ E Draft 8.0 m. </li>
      <li>Anchorage G2  Lat. 17° 55.0′ S Long. 122° 10.0′ E Draft 8.0 m. </li>
      <li>Anchorage G2  Lat. 17° 56.5′ S Long. 122° 09.5′ E Draft 8.0 m. </li>
   </list>
</anch>

但我希望列表会在进入 Gantheaume Bay 之前中断并重新启动一个新列表。

谁能解释一下我哪里出错了,正确的模式应该是什么?

TIA

您正在选择带有 xsl:for-each-grouprow 元素,因此 group-starting-with 也必须是 row 元素(分别代表组中的第一个元素)。

将您的 xsl:for-each-group 更改为...

<xsl:for-each-group group-starting-with="row[entry/hd4]" select="table/tgroup/tbody/row">