XSL for Xml to table transformation for two rows and many columns

前端 未结 2 1097
不思量自难忘°
不思量自难忘° 2021-01-23 06:39

I have been trying to work on a variation of a question I asked last week.

XSL for Xml to table transformation.

I have to output the the first and last page numb

相关标签:
2条回答
  • 2021-01-23 07:12

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

    Output:

    <table>
      <tr>
        <td>Arsenal</td>
        <td />
        <td>Chelsea</td>
        <td />
        <td>ManUnited</td>
        <td />
        <td>ManCity</td>
        <td />
      </tr>
      <tr>
        <td>1</td>
        <td>6</td>
        <td>7</td>
        <td>12</td>
        <td>13</td>
        <td>18</td>
        <td>19</td>
        <td>24</td>
      </tr>
    </table>
    
    0 讨论(0)
  • 2021-01-23 07:33

    first you need to group section wise then get first and last node in for-each

    Group by `section`
      for-each `section`
        if position =1 or position = last
          print it
    

    Here is a xsl 1.0 grouping example reference. Hope you can proceed

    XSLT select only last version element in rowset

    **In xslt **

    <xsl:key name="k" match="page" use="@section"/>
    
    <xsl:template match="/root">
        <table border="1">
            <tr>
                <xsl:for-each select="page[generate-id() = generate-id(key('k', @section))]">
                    <td  colspan="2">
                        <xsl:value-of select="."/>
                    </td>
                </xsl:for-each>
            </tr>    
            <tr>
            <xsl:for-each select="page[generate-id() = generate-id(key('k', @section))]">
                <xsl:variable name="key">
                    <xsl:value-of select="."/>
                </xsl:variable>
                <xsl:for-each select="../page[@section=$key]">
                    <xsl:if test="(position() = 1) or (position() = last())">
                        <td>
                            <xsl:value-of select="current()/@number"/>
                        </td>
                    </xsl:if>
                </xsl:for-each>
            </xsl:for-each>
            </tr>
        </table>
    </xsl:template>
    

    0 讨论(0)
提交回复
热议问题