在同一<root> </root>中提取第一组数据

时间:2013-04-04 00:18:07

标签: html xml xslt xslt-1.0

我有一个XSLT问题,后面是我之前提出的问题。

挑战在于从同一<root>中的重复集合中提取数据集。我有一个示例XSLT解决方案来自我之前提出的问题@Kirill Polishchuk。

Extracting a class from the section attribute using xsl

我只需要提取第一组数据,并忽略从number =“1”开始的第二组数据

请注意我正在使用XSLT 1.0。任何建议或指导都会非常感激。

输入:

<root>
  <page number="1" section="Arsenal_Stadium">Arsenal_Stadium</page> 
  <page number="2" section="Arsenal_Stadium">Arsenal_Stadium</page> 
  <page number="3" section="Arsenal_Stadium">Arsenal_Stadium</page> 
  <page number="4" section="Arsenal_Stadium">Arsenal_Stadium</page> 
  <page number="5" section="Arsenal_Stadium">Arsenal_Stadium</page> 
  <page number="6" section="Arsenal_Stadium">Arsenal_Stadium</page> 
  <page number="7" section="Arsenal_Stadium">Arsenal_Stadium</page> 
  <page number="8" section="Arsenal_Crowds">Arsenal_Crowds</page> 
  <page number="9" section="Arsenal_Crowds">Arsenal_Crowds</page> 
  <page number="10" section="Arsenal_Crowds">Arsenal_Crowds</page> 
  <page number="11" section="Arsenal_Crowds">Arsenal_Crowds</page> 
  <page number="12" section="Arsenal_Crowds">Arsenal_Crowds</page> 
  <page number="13" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="14" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="15" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="16" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="17" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="18" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="19" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="20" section="Arsenal_Outlook">Arsenal_Outlook</page> 
  <page number="21" section="Arsenal_Outlook">Arsenal_Outlook</page> 
  <page number="22" section="Arsenal_Outlook">Arsenal_Outlook</page> 
  <page number="23" section="Arsenal_Outlook">Arsenal_Outlook</page> 
  <page number="24" section="Arsenal_Outlook">Arsenal_Outlook</page> 
  <page number="1" section="Arsenal_Stadium">Arsenal_Stadium</page> 
  <page number="2" section="Arsenal_Stadium">Arsenal_Stadium</page> 
  <page number="3" section="Arsenal_Stadium">Arsenal_Stadium</page> 
  <page number="4" section="Arsenal_Stadium">Arsenal_Stadium</page> 
  <page number="5" section="Arsenal_Crowds">Arsenal_Crowds</page> 
  <page number="6" section="Arsenal_Crowds">Arsenal_Crowds</page> 
  <page number="7" section="Arsenal_Crowds">Arsenal_Crowds</page> 
  <page number="8" section="Arsenal_Crowds">Arsenal_Crowds</page>
  <page number="9" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="10" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="11" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="12" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="13" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="14" section="Arsenal_Finances">Arsenal_Finances</page> 
  <page number="15" section="Arsenal_Outlook">Arsenal_Outlook</page> 
  <page number="16" section="Arsenal_Outlook">Arsenal_Outlook</page> 
  <page number="17" section="Arsenal_Outlook">Arsenal_Outlook</page> 
  <page number="18" section="Arsenal_Outlook">Arsenal_Outlook</page>
 </root>

期望的输出:

<table>
<tr>
<td class="Stadium">Arsenal_Stadium</td>
<td></td>
<td class="Crowds">Arsenal_Crowds</td>
<td></td>
<td class="Finances">Arsenal_Finances</td>
<td></td>
<td class="Outlook">Arsenal_Outlook</td>
<td></td>
</tr>
<tr>
<td>1</td>
<td>7</td>
<td>8</td>
<td>12</td>
<td>13</td>
<td>19</td>
<td>20</td>
<td>24</td>
</tr>
</table>

1 个答案:

答案 0 :(得分:1)

此转化

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

  <xsl:key name="k" use="@section" match=
  "page[(@number > 1 and count(preceding-sibling::*[@number=1]) = 1)
      or
        (@number=1 and count(preceding-sibling::*[@number=1]) =0)
       ]"/>

  <xsl:template match="/*">
    <table>
      <tr>
        <xsl:apply-templates select=
          "page[generate-id() = generate-id(key('k', @section))]"/>
      </tr>
      <tr>
        <xsl:apply-templates mode="page"
          select="page[generate-id() = generate-id(key('k', @section))]" />
      </tr>
    </table>
  </xsl:template>

  <xsl:template match="page">
    <td class="{substring-after(@section, '_')}"><xsl:value-of select="."/></td>
    <td></td>
  </xsl:template>

  <xsl:template match="page" mode="page">
    <td><xsl:value-of select="@number"/></td>
    <td><xsl:value-of select="key('k', @section)[last()]/@number"/></td>
  </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时:

<root>
    <page number="1" section="Arsenal_Stadium">Arsenal_Stadium</page>
    <page number="2" section="Arsenal_Stadium">Arsenal_Stadium</page>
    <page number="3" section="Arsenal_Stadium">Arsenal_Stadium</page>
    <page number="4" section="Arsenal_Stadium">Arsenal_Stadium</page>
    <page number="5" section="Arsenal_Stadium">Arsenal_Stadium</page>
    <page number="6" section="Arsenal_Stadium">Arsenal_Stadium</page>
    <page number="7" section="Arsenal_Stadium">Arsenal_Stadium</page>
    <page number="8" section="Arsenal_Crowds">Arsenal_Crowds</page>
    <page number="9" section="Arsenal_Crowds">Arsenal_Crowds</page>
    <page number="10" section="Arsenal_Crowds">Arsenal_Crowds</page>
    <page number="11" section="Arsenal_Crowds">Arsenal_Crowds</page>
    <page number="12" section="Arsenal_Crowds">Arsenal_Crowds</page>
    <page number="13" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="14" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="15" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="16" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="17" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="18" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="19" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="20" section="Arsenal_Outlook">Arsenal_Outlook</page>
    <page number="21" section="Arsenal_Outlook">Arsenal_Outlook</page>
    <page number="22" section="Arsenal_Outlook">Arsenal_Outlook</page>
    <page number="23" section="Arsenal_Outlook">Arsenal_Outlook</page>
    <page number="24" section="Arsenal_Outlook">Arsenal_Outlook</page>
    <page number="1" section="Arsenal_Stadium">Arsenal_Stadium</page>
    <page number="2" section="Arsenal_Stadium">Arsenal_Stadium</page>
    <page number="3" section="Arsenal_Stadium">Arsenal_Stadium</page>
    <page number="4" section="Arsenal_Stadium">Arsenal_Stadium</page>
    <page number="5" section="Arsenal_Crowds">Arsenal_Crowds</page>
    <page number="6" section="Arsenal_Crowds">Arsenal_Crowds</page>
    <page number="7" section="Arsenal_Crowds">Arsenal_Crowds</page>
    <page number="8" section="Arsenal_Crowds">Arsenal_Crowds</page>
    <page number="9" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="10" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="11" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="12" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="13" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="14" section="Arsenal_Finances">Arsenal_Finances</page>
    <page number="15" section="Arsenal_Outlook">Arsenal_Outlook</page>
    <page number="16" section="Arsenal_Outlook">Arsenal_Outlook</page>
    <page number="17" section="Arsenal_Outlook">Arsenal_Outlook</page>
    <page number="18" section="Arsenal_Outlook">Arsenal_Outlook</page>
</root>

生成想要的正确结果

<table>
   <tr>
      <td class="Stadium">Arsenal_Stadium</td>
      <td/>
      <td class="Crowds">Arsenal_Crowds</td>
      <td/>
      <td class="Finances">Arsenal_Finances</td>
      <td/>
      <td class="Outlook">Arsenal_Outlook</td>
      <td/>
   </tr>
   <tr>
      <td>1</td>
      <td>7</td>
      <td>8</td>
      <td>12</td>
      <td>13</td>
      <td>19</td>
      <td>20</td>
      <td>24</td>
   </tr>
</table>

<强>概括

很容易概括这个转换以处理Kth“set。

例如,以下转换处理相同的第二个“集合”,提供XML文档(上图):

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

  <xsl:key name="k" use="@section" match=
  "page[(@number > 1 and count(preceding-sibling::*[@number=1]) = 2)
      or
        (@number=1 and count(preceding-sibling::*[@number=1]) =1)
       ]"/>

  <xsl:template match="/*">
    <table>
      <tr>
        <xsl:apply-templates select=
          "page[generate-id() = generate-id(key('k', @section))]"/>
      </tr>
      <tr>
        <xsl:apply-templates mode="page"
          select="page[generate-id() = generate-id(key('k', @section))]" />
      </tr>
    </table>
  </xsl:template>

  <xsl:template match="page">
    <td class="{substring-after(@section, '_')}"><xsl:value-of select="."/></td>
    <td></td>
  </xsl:template>

  <xsl:template match="page" mode="page">
    <td><xsl:value-of select="@number"/></td>
    <td><xsl:value-of select="key('k', @section)[last()]/@number"/></td>
  </xsl:template>
</xsl:stylesheet>

并且在这种情况下再次产生想要的正确结果:

<table>
   <tr>
      <td class="Stadium">Arsenal_Stadium</td>
      <td/>
      <td class="Crowds">Arsenal_Crowds</td>
      <td/>
      <td class="Finances">Arsenal_Finances</td>
      <td/>
      <td class="Outlook">Arsenal_Outlook</td>
      <td/>
   </tr>
   <tr>
      <td>1</td>
      <td>4</td>
      <td>5</td>
      <td>8</td>
      <td>9</td>
      <td>14</td>
      <td>15</td>
      <td>18</td>
   </tr>
</table>