本文介绍了用于Xml到表转换的XSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续我昨天的xslt问题,链接如下。



我必须输出每个部分的第一个和最后一个页码到一个表中。





问候JJ。



输入:

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

输出:

 < TR>< TD→1< / TD>< TD>阿森纳< / TD>< TD→6< / TD>< / TR> 
< tr>< td> 7< / td>< td>切尔西< / td>< td> 12< / td>< / tr>
< tr>< td> 13< / td>< td> ManU< / td>< td> 18< / td>< / tr>
< tr>< td> 19< / td>< td> Mancity< / td>< td> 24< / td>< / tr>


解决方案

使用:

 < xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform> 

< xsl:key name =kmatch =pageuse =@ section/>

< xsl:template match =/ root>
< table>
< / table>
< / xsl:template>

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

输出:

 <表> 
< tr>
< td> 1< / td>
< td>阿森纳< / td>
< td> 6< / td>
< / tr>
< tr>
< td> 7< / td>
< td>切尔西< / td>
< td> 12< / td>
< / tr>
< tr>
< td> 13< / td>
< td> ManUnited< / td>
< td> 18< / td>
< / tr>
< tr>
< td> 19< / td>
< td> ManCity< / td>
< td> 24< / td>
< / tr>
< / table>


Following up on my xslt question from yesterday, link found below.

first last element 2nd variation

I have to output the the first and last page number of each section into a table.

I'm puzzled on where to start. The XSLT 1.0 Solution and XSLT 2.0 Solution would be much appreciated. I would really appreciate any given help.

Regards JJ.

INPUT:

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

OUTPUT:

<tr><td>1</td><td>Arsenal</td><td>6</td></tr>
<tr><td>7</td><td>Chelsea</td><td>12</td></tr>
<tr><td>13</td><td>ManU</td><td>18</td></tr>
<tr><td>19</td><td>Mancity</td><td>24</td></tr>
解决方案

Use:

<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">
    <table>
      <xsl:apply-templates select="page[generate-id() = generate-id(key('k', @section))]"/>
    </table>
  </xsl:template>

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

Output:

<table>
  <tr>
    <td>1</td>
    <td>Arsenal</td>
    <td>6</td>
  </tr>
  <tr>
    <td>7</td>
    <td>Chelsea</td>
    <td>12</td>
  </tr>
  <tr>
    <td>13</td>
    <td>ManUnited</td>
    <td>18</td>
  </tr>
  <tr>
    <td>19</td>
    <td>ManCity</td>
    <td>24</td>
  </tr>
</table>

这篇关于用于Xml到表转换的XSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 05:19