XSLT转换为固定表结构 [英] XSLT transformation into fixed table structure

查看:101
本文介绍了XSLT转换为固定表结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于具有固定结构和可变输入内容的输出表,我有一个关于XSLT转换的问题。

我列出了2个不同的例子。

所需的输出表是6列和两行。 p>

但是所需的td类和内容依赖于输入XML



我有一个前面的例子,由@Kirill Polishchuk



使用xsl从section属性中提取类

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



示例1



输入6个部分的XML

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

所需输出6行& 2栏

 < table> 
< tr>
< td class =Stadium> Stadium< / td>
< td class =人群>人群< / td>
< td class =支援>支援< / td>
< td class =收入>收入< / td>
< td class =费用>费用< / td>
< td class =Outlook> Outlook< / td>
< / tr>
< tr>
< td class =Stadium_R2> 1-4< / td>
< td class =Crowds_R2> 5-6< / td>
< td class =Support_R2> 7-10< / td>
< td class =Revenue_R2> 11-14< / td>
< td class =Cost_R2> 15-18< / td>
< td class =Outlook_R2> 19-22< / td>
< / tr>
< / table>

示例2



输入XML 4部分

 < root> 
< page number =1section =Arsenal_Stadium> Arsenal_Stadium< / page>
< page number =2section =Arsenal_Stadium> Arsenal_Stadium< / page>
< page number =3section =Arsenal_Stadium> Arsenal_Stadium< / page>
< page number =4section =Arsenal_Stadium> Arsenal_Stadium< / page>
< page number =5section =Arsenal_Support> Arsenal_Support< / page>
< page number =6section =Arsenal_Support> Arsenal_Support< / page>
< page number =7section =Arsenal_Support> Arsenal_Support< / page>
< page number =8section =Arsenal_Support> Arsenal_Support< / page>
< page number =9section =Arsenal_Cost> Arsenal_Cost< / page>
< page number =10section =Arsenal_Cost> Arsenal_Cost< / page>
< page number =11section =Arsenal_Cost> Arsenal_Cost< / page>
< page number =12section =Arsenal_Cost> Arsenal_Cost< / page>
< page number =13section =Arsenal_Outlook> Arsenal_Outlook< / page>
< page number =14section =Arsenal_Outlook> Arsenal_Outlook< / page>
< page number =15section =Arsenal_Outlook> Arsenal_Outlook< / page>
< page number =16section =Arsenal_Outlook> Arsenal_Outlook< / page>
< / root>

所需输出6行& 2栏

 < table> 
< tr>
< td class =Stadium> Stadium< / td>
< td class =支援>支援< / td>
< td class =费用>费用< / td>
< td class =Outlook> Outlook< / td>
< td class =None>< / td>
< td class =None>< / td>
< / tr>
< tr>
< td class =Stadium_R2> 1-4< / td>
< td class =Support_R2> 5-8< / td>
< td class =Cost_R2> 9-12< / td>
< td class =Outlook_R2> 13-16< / td>
< td class =None>< / td>
< td class =None>< / td>
< / tr>
< / table>

问候JJ。

解决方案

我相信这应该可以解决问题。请注意使用 NoneRows 模板来填充额外的单元格以创建6:

 < xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform> 
< xsl:output method =xmlindent =yesomit-xml-declaration =yes/>
< xsl:key name =kGroupmatch =pageuse =@ section/>

< xsl:template match =/ *>
< table>
select =* [generate-id()= generate-id(key('kGroup',@section)[1])]/> gt ;

< tr>
< xsl:apply-templates select =$ groupsmode =top/>
< xsl:call-template name =NoneCells>
< xsl:with-param name =countselect =6 - count($ groups)/>
< / xsl:call-template>
< / tr>
< tr>
< xsl:call-template name =NoneCells>
< xsl:with-param name =countselect =6 - count($ groups)/>
< / xsl:call-template>
< / tr>
< / table>
< / xsl:template>

< xsl:template match =pagemode =top>
< td class ={$ sectName}>
< xsl:value-of select =$ sectName/>
< / td>
< / xsl:template>

< xsl:template match =pagemode =pageNums>
< td class ={substring-after(@section,'Arsenal _')} _ R2>
< xsl:value-of select =concat($ groupMembers [1] / @ number,' - ',
$ groupMembers [last()] / @ number)/>
< / td>
< / xsl:template>

< xsl:template name =NoneCells>
< xsl:param name =count/>
< xsl:if test =$ count> 0>
< td class =None>< / td>
< xsl:call-template name =NoneCells>
< xsl:with-param name =countselect =$ count - 1/>
< / xsl:call-template>
< / xsl:if>
< / xsl:template>
< / xsl:stylesheet>

在您的第一个示例输入中运行时:

 <表> 
< tr>
< td class =Stadium> Stadium< / td>
< td class =人群>人群< / td>
< td class =支援>支援< / td>
< td class =收入>收入< / td>
< td class =费用>费用< / td>
< td class =Outlook> Outlook< / td>
< / tr>
< tr>
< td class =Stadium_R2> 1-4< / td>
< td class =Crowds_R2> 5-8< / td>
< td class =Support_R2> 9-12< / td>
< td class =Revenue_R2> 13-16< / td>
< td class =Cost_R2> 17-20< / td>
< td class =Outlook_R2> 21-24< / td>
< / tr>
< / table>

当您在第二个样本输入上运行时:

 <表> 
< tr>
< td class =Stadium> Stadium< / td>
< td class =支援>支援< / td>
< td class =费用>费用< / td>
< td class =Outlook> Outlook< / td>
< td class =None/>
< td class =None/>
< / tr>
< tr>
< td class =Stadium_R2> 1-4< / td>
< td class =Support_R2> 5-8< / td>
< td class =Cost_R2> 9-12< / td>
< td class =Outlook_R2> 13-16< / td>
< td class =None/>
< td class =None/>
< / tr>
< / table>


I have a question regarding XSLT transformation for ouput tables with fixed structure and variable input content.

I have outlined 2 different example.

The desired output table is 6 columns and two row.

But the desired td classes and content are dependent on the input XML

I have a previous example to start from provided by @Kirill Polishchuk

Extracting a class from the section attribute using xsl

Please note I'm working with XSLT 1.0. Any advice or guidance will be much appreciate.

Example 1

Input XML with 6 sections

<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_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_Support">Arsenal_Support</page> 
  <page number="10" section="Arsenal_Support">Arsenal_Support</page> 
  <page number="11" section="Arsenal_Support">Arsenal_Support</page> 
  <page number="12" section="Arsenal_Support">Arsenal_Support</page> 
  <page number="13" section="Arsenal_Revenue">Arsenal_Revenue</page> 
  <page number="14" section="Arsenal_Revenue">Arsenal_Revenue</page> 
  <page number="15" section="Arsenal_Revenue">Arsenal_Revenue</page> 
  <page number="16" section="Arsenal_Revenue">Arsenal_Revenue</page> 
  <page number="17" section="Arsenal_Cost">Arsenal_Cost</page> 
  <page number="18" section="Arsenal_Cost">Arsenal_Cost</page> 
  <page number="19" section="Arsenal_Cost">Arsenal_Cost</page> 
  <page number="20" section="Arsenal_Cost">Arsenal_Cost</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> 
 </root>

Desired Output 6 rows & 2 columns

<table>
<tr>
<td class="Stadium">Stadium</td>
<td class="Crowds">Crowds</td>
<td class="Support">Support</td>
<td class="Revenue">Revenue</td>
<td class="Cost">Cost</td>
<td class="Outlook">Outlook</td>
</tr>
<tr>
<td class="Stadium_R2">1-4</td>
<td class="Crowds_R2">5-6</td>
<td class="Support_R2">7-10</td>
<td class="Revenue_R2">11-14</td>
<td class="Cost_R2">15-18</td>
<td class="Outlook_R2">19-22</td>
</tr>
</table>

Example 2

Input XML 4 sections

<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_Support">Arsenal_Support</page> 
  <page number="6" section="Arsenal_Support">Arsenal_Support</page> 
  <page number="7" section="Arsenal_Support">Arsenal_Support</page> 
  <page number="8" section="Arsenal_Support">Arsenal_Support</page> 
  <page number="9" section="Arsenal_Cost">Arsenal_Cost</page> 
  <page number="10" section="Arsenal_Cost">Arsenal_Cost</page> 
  <page number="11" section="Arsenal_Cost">Arsenal_Cost</page> 
  <page number="12" section="Arsenal_Cost">Arsenal_Cost</page> 
  <page number="13" section="Arsenal_Outlook">Arsenal_Outlook</page> 
  <page number="14" section="Arsenal_Outlook">Arsenal_Outlook</page> 
  <page number="15" section="Arsenal_Outlook">Arsenal_Outlook</page> 
  <page number="16" section="Arsenal_Outlook">Arsenal_Outlook</page> 
 </root>

Desired Output 6 rows & 2 columns

<table>
<tr>
<td class="Stadium">Stadium</td>
<td class="Support">Support</td>
<td class="Cost">Cost</td>
<td class="Outlook">Outlook</td>
<td class="None"></td>
<td class="None"></td>
</tr>
<tr>
<td class="Stadium_R2">1-4</td>
<td class="Support_R2">5-8</td>
<td class="Cost_R2">9-12</td>
<td class="Outlook_R2">13-16</td>
<td class="None"></td>
<td class="None"></td>
</tr>
</table>

Regards JJ.

解决方案

I believe this should do the trick. Note the use of the NoneRows template to fill in the extra cells to make 6:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:key name="kGroup" match="page" use="@section"/>

  <xsl:template match="/*">
    <table>
      <xsl:variable name="groups"
         select="*[generate-id() = generate-id(key('kGroup', @section)[1])]" />

      <tr>
        <xsl:apply-templates select="$groups" mode="top" />
        <xsl:call-template name="NoneCells">
          <xsl:with-param name="count" select="6 - count($groups)" />
        </xsl:call-template>
      </tr>
      <tr>
        <xsl:apply-templates select="$groups" mode="pageNums" />
        <xsl:call-template name="NoneCells">
          <xsl:with-param name="count" select="6 - count($groups)" />
        </xsl:call-template>
      </tr>
    </table>
  </xsl:template>

  <xsl:template match="page" mode="top">
    <xsl:variable name="sectName" select="substring-after(@section, 'Arsenal_')" />
    <td class="{$sectName}">
      <xsl:value-of select="$sectName" />
    </td>
  </xsl:template>

  <xsl:template match="page" mode="pageNums">
    <xsl:variable name="groupMembers" select="key('kGroup', @section)" />
    <td class="{substring-after(@section, 'Arsenal_')}_R2">
      <xsl:value-of select="concat($groupMembers[1]/@number, '-', 
                                   $groupMembers[last()]/@number)"/>
    </td>
  </xsl:template>

  <xsl:template name="NoneCells">
    <xsl:param name="count" />
    <xsl:if test="$count > 0">
      <td class="None"></td>
      <xsl:call-template name="NoneCells">
        <xsl:with-param name="count" select="$count - 1" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

When run on your first sample input:

<table>
  <tr>
    <td class="Stadium">Stadium</td>
    <td class="Crowds">Crowds</td>
    <td class="Support">Support</td>
    <td class="Revenue">Revenue</td>
    <td class="Cost">Cost</td>
    <td class="Outlook">Outlook</td>
  </tr>
  <tr>
    <td class="Stadium_R2">1-4</td>
    <td class="Crowds_R2">5-8</td>
    <td class="Support_R2">9-12</td>
    <td class="Revenue_R2">13-16</td>
    <td class="Cost_R2">17-20</td>
    <td class="Outlook_R2">21-24</td>
  </tr>
</table>

When run on your second sample input:

<table>
  <tr>
    <td class="Stadium">Stadium</td>
    <td class="Support">Support</td>
    <td class="Cost">Cost</td>
    <td class="Outlook">Outlook</td>
    <td class="None" />
    <td class="None" />
  </tr>
  <tr>
    <td class="Stadium_R2">1-4</td>
    <td class="Support_R2">5-8</td>
    <td class="Cost_R2">9-12</td>
    <td class="Outlook_R2">13-16</td>
    <td class="None" />
    <td class="None" />
  </tr>
</table>

这篇关于XSLT转换为固定表结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆