使用xsl从section属性中提取类 [英] Extracting a class from the section attribute using xsl

查看:94
本文介绍了使用xsl从section属性中提取类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上周提出的问题中有一个XSLT问题。
用于Xml的XSL:使用XSL插入特定的类



挑战在于根据section属性插入类。我从@Kirill Polishchuk提供的上一个问题中获得了示例XSLT解决方案,但是如果我拥有非常大的数据集,则无法使用此解决方案。

我只需要在section属性中的下划线之后提取文本,并将其用作输出xml中的类。我现在很困惑。



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



谢谢JJ。



输入:

 < 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_Stadium> Arsenal_Stadium< / page>
< page number =6section =Arsenal_Stadium> Arsenal_Stadium< / page>
< page number =7section =Arsenal_Stadium> Arsenal_Stadium< / page>
< page number =8section =Arsenal_Crowds> Arsenal_Crowds< / page>
< page number =9section =Arsenal_Crowds> Arsenal_Crowds< / page>
< page number =10section =Arsenal_Crowds> Arsenal_Crowds< / page>
< page number =11section =Arsenal_Crowds> Arsenal_Crowds< / page>
< page number =12section =Arsenal_Crowds> Arsenal_Crowds< / page>
< page number =13section =Arsenal_Finances> Arsenal_Finances< / page>
< page number =14section =Arsenal_Finances> Arsenal_Finances< / page>
< page number =15section =Arsenal_Finances> Arsenal_Finances< / page>
< page number =16section =Arsenal_Finances> Arsenal_Finances< / page>
< page number =17section =Arsenal_Finances> Arsenal_Finances< / page>
< page number =18section =Arsenal_Finances> Arsenal_Finances< / page>
< page number =19section =Arsenal_Finances> Arsenal_Finances< / page>
< page number =20section =Arsenal_Outlook> Arsenal_Outlook< / 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>

输出:

 <表> 
< tr>
< td class =Stadium> Arsenal_Stadium< / td>
< td>< / td>
< td class =人群> 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>


解决方案

从我的答案略微修改XSLT:https://stackoverflow.com/a/13225163/787016 。它添加 class 属性,并使用 substring-after 函数来提取部分的右部分属性。

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

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

< xsl:template match =/ root>
< table>
< tr>
< / tr>
< tr>
< / 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 =pagemode =page>
< td>
< xsl:value-of select =@ number/>
< / td>
< td>
< xsl:value-of select =key('k',@section)[last()] / @ number/>
< / td>
< / xsl:template>

< / xsl:stylesheet>


I have a XSLT question which follows on from the question I asked last week. XSL for Xml: Inserting specific classes using XSL

The challenge is to insert classes according to the section attribute. I have an example XSLT solution from my previous question provided by @Kirill Polishchuk, but I can not this solution if I have very large data set.

I just need to extract the text after the underscore in the section attribute and use it as a class in the output xml. I'm puzzled at the moment.

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

Thank You JJ.

Input:

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

Output:

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

解决方案

Slightly modified XSLT from my answer: https://stackoverflow.com/a/13225163/787016. It adds class attribute and uses substring-after function to extract right part of section attribute.

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

这篇关于使用xsl从section属性中提取类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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