View Single Post
  #2 (permalink)  
Old August 22nd, 2019, 06:07 AM
Martin Honnen Martin Honnen is offline
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

It seems like grouping by name and then checking if there is at least one non-empty element in the group is a way to establish the columns you want to output:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	exclude-result-prefixes="#all"
	version="3.0">

  <xsl:output method="html" indent="yes" html-version="5"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>.NET XSLT Fiddle Example</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="custom-data/columns">
      <xsl:variable name="columns" select="columns"/>
            
      <xsl:variable name="non-empty-cols"
        as="xs:string*">
          <xsl:for-each-group select="$columns/*" group-by="name(.)">
              <xsl:sequence
                 select="current-grouping-key()[current-group()[normalize-space()]]"/>
          </xsl:for-each-group>
      </xsl:variable>
      
      <table border="1">
          <thead>
              <tr>
                  <xsl:for-each select="$non-empty-cols">
                      <th>
                          <xsl:value-of select="."/>
                      </th>
                  </xsl:for-each>
              </tr>
          </thead>
          <tbody>
              <xsl:apply-templates select="columns">
                  <xsl:with-param name="cols" select="$non-empty-cols"/>
              </xsl:apply-templates>
          </tbody>
      </table>
  </xsl:template>
  
  <xsl:template match="columns/columns">
      <xsl:param name="cols"/>
      <tr>
          <xsl:apply-templates select="*[name() = $cols]"/>
      </tr>
  </xsl:template>

  <xsl:template match="columns/columns/*">
      <td>
          <xsl:value-of select="."/>
      </td>
  </xsl:template>

</xsl:stylesheet>
Online sample at https://xsltfiddle.liberty-development.net/pPzifqw
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog

Last edited by Martin Honnen; August 22nd, 2019 at 06:09 AM.. Reason: adding link to sample
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
msambasiva@gmail.com (August 23rd, 2019)