Extracting a class from the section attribute using xsl

前端 未结 1 1039
醉话见心
醉话见心 2020-12-07 04:39

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 acc

相关标签:
1条回答
  • 2020-12-07 05:31

    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>
    
    0 讨论(0)
提交回复
热议问题