【问题标题】:Extracting a class from the section attribute using xsl使用 xsl 从部分属性中提取类
【发布时间】:2012-11-20 04:34:15
【问题描述】:

我有一个 XSLT 问题,这是我上周提出的问题的后续问题。 XSL for Xml: Inserting specific classes using XSL

挑战是根据 section 属性插入类。我有一个来自@Kirill Polishchuk 提供的上一个问题的示例XSLT 解决方案,但如果我有非常大的数据集,我就无法使用这个解决方案。

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

请注意,我正在使用 XSLT 1.0。任何建议或指导将不胜感激。

谢谢你,JJ。

输入:

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

输出:

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

【问题讨论】:

    标签: html xml xslt xpath


    【解决方案1】:

    根据我的回答稍微修改了 XSLT:https://stackoverflow.com/a/13225163/787016。它添加了class 属性并使用substring-after 函数提取section 属性的右侧部分。

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多