0

XSLT XML 问题。

我对我之前的问题有一个变体。我必须输出每个部分的第一页和最后一页元素。如下所示。任何帮助都感激不尽。

谢谢JJ。

输入:

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

输出:

 <root>
  <page number="1" section="Arsenal">Arsenal</page> 
  <page number="6" section="Arsenal">Arsenal</page> 
  <page number="7" section="Chelsea">Chelsea</page> 
  <page number="12" section="Chelsea">Chelsea</page> 
  <page number="13" section="ManUnited">ManUnited</page> 
  <page number="18" section="ManUnited">ManUnited</page> 
  <page number="19" section="ManCity">ManCity</page> 
  <page number="24" section="ManCity">ManCity</page> 
 </root>
4

1 回答 1

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" match="page" use="@section"/>

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

  <xsl:template match="page">
    <xsl:copy-of select=". | key('k', @section)[last()]"/>
  </xsl:template>
</xsl:stylesheet>
于 2012-10-29T04:06:03.900 回答