Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 22nd, 2019, 02:52 AM
Registered User
Points: 44, Level: 1
Points: 44, Level: 1 Points: 44, Level: 1 Points: 44, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2017
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Default nonempty check for an element at the same level

Hi,

XSLT2.0

I am trying to find the element names and count which are nonempty at any level

Ex:

<custom-data>
<schema>XXXschema>
<object-name>YYYYY</object-name>
<short-name>YYYYY</short-name>
<object-type>TABLE</object-type>
<mls-support-model/>
<object-owner>PPPP</object-owner>
<status/>
<columns>
<columns>
<name>ID</name>
<datatype>NUMBER</datatype>
<length/>
<precision/>
<not-null>Yes</not-null>
<comments>Identifier of the aging bucket.</comments>
<flexfield-mapping/>
<status/>
</columns>
<columns>
<name>LAST_UPDATED_BY</name>
<datatype>VARCHAR2</datatype>
<length>64</length>
<precision/>
<not-null>Yes</not-null>
<comments>Who column: indicates the user who last updated the row.</comments>
<flexfield-mapping/>
<status/>
</columns>
<columns>
<name>ATTRIBUTE8</name>
<datatype>VARCHAR2</datatype>
<length>150</length>
<precision/>
<not-null/>
<comments>Descriptive Flexfield: segment of the user descriptive flexfield.</comments>
<flexfield-mapping>Aging </flexfield-mapping>
<status/>
</columns>
</columns>
</custom-data>

I want to generate a table from the above data. The trick is, if there is no data for a column at any where, we need to exclude that column from the output table.

Ex: status,precision elements is empty for all the columns. SO we don't need to display status and precision column in output table.

How to get the number of elements without data at the same level and there names?

Thanks,

Samba.
Reply With Quote
  #2 (permalink)  
Old August 22nd, 2019, 06:07 AM
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)
  #3 (permalink)  
Old August 23rd, 2019, 02:27 AM
Registered User
Points: 44, Level: 1
Points: 44, Level: 1 Points: 44, Level: 1 Points: 44, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2017
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Its working! Thank you so much for quick response!!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy new element at child level dishant XSLT 1 January 13th, 2012 03:24 AM
<a> element in within block-level elements? steddie1 BOOK: Beginning Web Programming with HTML, XHTML, and CSS, 2nd Edition ISBN: 978-0-470-25931-3 2 March 3rd, 2009 04:43 AM
How to detect the level of an element aliov_85 XSLT 3 April 21st, 2008 01:11 AM
Explicit Variable Check at IIS Level pvasudevan Classic ASP Basics 1 April 26th, 2004 05:07 AM
Check quantity stock level Fehrer Access VBA 17 March 29th, 2004 06:11 PM



All times are GMT -4. The time now is 10:05 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.