Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - xslt: How could I use xslt to create a table with multiple columns and rows?

How would I take this xml and create a table with a column for each "section" element and then display all the "document" elements in that column using xslt?

<Documents>
  <Section>
  <SectionName>Green</SectionName>
    <Document>
      <FileName>Tier 1 Schedules</FileName>     
    </Document>
    <Document>
      <FileName>Tier 3 Schedules</FileName>      
    </Document>
    <Document>
      <FileName>Setback Schedule</FileName>    
    </Document>
    <Document>
      <FileName>Tier 2 Governance</FileName>    
    </Document>
 </Section>
 <Section>
 <SectionName>MRO/Refurb</SectionName>
   <Document>
     <FileName>Tier 2 Governance</FileName>    
   </Document>
 </Section>

Thanks, Al

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This solution uses no recursion and hi-lights a few useful XSLT techniques such as Muenchian grouping, keys, finding maximum and iterating without recursion.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:strip-space elements="*"/>

    <xsl:key name="kSectsByValue" match="SectionName"
         use="."/>

    <xsl:key name="kDocBySect" match="Document"
         use="../SectionName"/>

    <xsl:variable name="vCols" select=
       "/*/*/SectionName
                [generate-id()
                =          
                 generate-id(key('kSectsByValue',.)[1])
                 ]"/>

    <xsl:variable name="vMaxRows">
             <xsl:for-each select="$vCols">
               <xsl:sort data-type="number" order="descending"
                    select="count(key('kDocBySect', .))"      />
               <xsl:if test="position() = 1">
                 <xsl:value-of select="count(key('kDocBySect', .))"/>
               </xsl:if>
             </xsl:for-each>
    </xsl:variable>

    <xsl:template match="/">
             <table>
               <tr>
                 <xsl:apply-templates select="$vCols"/>
               </tr>

               <xsl:for-each select=
                 "(/*/*/Document)[not(position() > $vMaxRows)]">                   
                 <tr>

                   <xsl:variable name="vPos" select="position()"/>

                   <xsl:for-each select="$vCols">
                     <td>
                       <xsl:value-of select=
                           "../Document[$vPos]/FileName"/>
                     </td>
                   </xsl:for-each>

                 </tr>
              </xsl:for-each>
            </table>

    </xsl:template>

    <xsl:template match="SectionName">
            <td>
              <xsl:value-of select="." />
            </td>   
    </xsl:template>
</xsl:stylesheet>

when applied on the original XML document (corrected to be well-formed):

<Documents>
    <Section>
        <SectionName>Green</SectionName>
        <Document>
            <FileName>Tier 1 Schedules</FileName>
        </Document>
        <Document>
            <FileName>Tier 3 Schedules</FileName>
        </Document>
        <Document>
            <FileName>Setback Schedule</FileName>
        </Document>
        <Document>
            <FileName>Tier 2 Governance</FileName>
        </Document>
    </Section>
    <Section>
        <SectionName>MRO/Refurb</SectionName>
        <Document>
            <FileName>Tier 2 Governance</FileName>
        </Document>
    </Section>
</Documents>

produces the desired result:

<table>
   <tr>
      <td>Green</td>
      <td>MRO/Refurb</td>
   </tr>
   <tr>
      <td>Tier 1 Schedules</td>
      <td>Tier 2 Governance</td>
   </tr>
   <tr>
      <td>Tier 3 Schedules</td>
      <td/>
   </tr>
   <tr>
      <td>Setback Schedule</td>
      <td/>
   </tr>
   <tr>
      <td>Tier 2 Governance</td>
      <td/>
   </tr>
</table>

Do note:

  1. We use the Muenchian method for grouping in order to find all different column names, not relying that in the XML document they will be unique.

  2. Keys are used both for the Muenchian grouping and for finding all items belonging to a column.

  3. The maximum number of rows is found and kept in the variable $vMaxRows

  4. We iterate N times to produce the N rows of the table -- not using recursion!

  5. The N-th row is output by applying templates to all column items that have position N in their column.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...