Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6856859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:56:28+00:00 2026-05-27T01:56:28+00:00

I am trying to create an html table by querying on an xml document.

  • 0

I am trying to create an html table by querying on an xml document. I am using xslt.

Here is the problem. “parent” node contains many “child” nodes. I have to o/p a table that contains @name of parent and count of “child” nodes in sorted order(descending). So I am doing

 <?xml version="1.0" encoding="ISO-8859-1"?>

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="parent[count(child) &gt; 3]">

  <html>
   <table border="1">
      <xsl:for-each select=".">
      <xsl:sort select="{count(child)}" data-type="number" order="descending"/>
        <tr>
        <td><b><xsl:value-of select="@name" /></b></td>
        <td><xsl:value-of select="count(child)" /></td>
        </tr>
      </xsl:for-each> 
   </table>
   </html>

   </xsl:template>
   <xsl:template match="text()" />
  </xsl:stylesheet>

I get the html however the only problem is I am not getting it in sorted order by count of child elements. I suspect I am using count incorrectly xsl:sort? Can you help?

Input xml

<outer>
<parent name="abc" attr1="22664136" attr2="647500">
<child percentage="11">aaa</child>
<child percentage="35">bbb</child>
<child percentage="50">ccc</child>
</parent>

<parent name="ggg" attr1="3249136" attr2="28750"/>

<parent name="ghi" attr1="29183032" attr2="2381740">
<child2>
<name>ppp</name>
<attr1>1507241</attr1>
</child2>
</parent>


<parent name="qwe" attr1="10342899" attr2="1246700"/>

<parent name="lkj" attr1="65647" attr2="440">
<child percentage="100">jjj</child>
</parent>

</outer>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:56:28+00:00Added an answer on May 27, 2026 at 1:56 am

    There are numerous mistakes in the provided XSLT code!

    The biggest problem is here:

        <xsl:for-each select=".">
          <xsl:sort select="{count(child)}" data-type="number" order="descending"/>
          <tr>
            <td><b><xsl:value-of select="@name" /></b></td>
            <td><xsl:value-of select="count(child)" /></td>
          </tr>
        </xsl:for-each>
    

    This will not perform any meaningful sort, because the node-set of the nodes to be sorted contains only one node — the current node.

    The next problem is here:

    <xsl:sort select="{count(child)}" data-type="number" order="descending"/>
    

    There shouldn’t be any AVT in any select attribute of an XSLT instruction — you need to remove the curly braces.

    The 3rd problem is that the sort is specified too-late — inside the template mathcing parent. A parent doesn’t have any children that themselves have child children.

    Solution: Correcting all major problems, discussed above, one may arrive at the following code:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/*">
            <html>
                <table border="1">
                    <xsl:for-each select="parent">
                        <xsl:sort select="count(child)" data-type="number" order="descending"/>
                        <tr>
                            <td>
                                <b>
                                    <xsl:value-of select="@name" />
                                </b>
                            </td>
                            <td>
                                <xsl:value-of select="count(child)" />
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </html>
        </xsl:template>
        <xsl:template match="text()" />
    </xsl:stylesheet>
    

    when this transformation is applied on the provided XML document:

    <outer>
        <parent name="abc" attr1="22664136" attr2="647500">
            <child percentage="11">aaa</child>
            <child percentage="35">bbb</child>
            <child percentage="50">ccc</child>
        </parent>
        <parent name="ggg" attr1="3249136" attr2="28750"/>
        <parent name="ghi" attr1="29183032" attr2="2381740">
            <child2>
                <name>ppp</name>
                <attr1>1507241</attr1>
            </child2>
        </parent>
        <parent name="qwe" attr1="10342899" attr2="1246700"/>
        <parent name="lkj" attr1="65647" attr2="440">
            <child percentage="100">jjj</child>
        </parent>
    </outer>
    

    the wanted-sorted result is produced:

    <html>
       <table border="1">
          <tr>
             <td><b>abc</b></td>
             <td>3</td>
          </tr>
          <tr>
             <td><b>lkj</b></td>
             <td>1</td>
          </tr>
          <tr>
             <td><b>ggg</b></td>
             <td>0</td>
          </tr>
          <tr>
             <td><b>ghi</b></td>
             <td>0</td>
          </tr>
          <tr>
             <td><b>qwe</b></td>
             <td>0</td>
          </tr>
       </table>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an HTML/CSS-based week calendar using CSS display:table-cell styling on the
I'm trying to create a copy/paste system for a HTML table using jQuery and
I'm trying to create a horizontal 100% stacked bar graph using HTML and CSS.
I am trying to create a checkbox dynamically using following HTML/JavaScript. Any ideas why
I'm trying to create css buttons by using the following html markup: <a href=access.php
I'm currently trying dynamically create an html table in PHP. Further, I want the
I'm trying to create Class Table Inheritance as in ( http://www.castleproject.org/activerecord/documentation/trunk/usersguide/typehierarchy.html ) So let's
I'm trying to create a calendar in an HTML table design with CSS element
I am trying to create a table in an XHTML document shown below (only
I'm trying to create an html table for order logs for customers. A customer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.