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 1023585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:38:49+00:00 2026-05-16T11:38:49+00:00

I have two problems with the below code that I need help fixing: 1)

  • 0

I have two problems with the below code that I need help fixing:

1) It doesn’t include the root node when returning results from the XPath expression. (I’ve tried a couple things, but it messes up the results even more..)

2) I need help fixing the formatting of the results. I need the same nodes with the same attributes to be listed under one heading instead of having a heading for every result. I had a similar question earlier, but now that issues with the code were fixed, I can’t seem to get my headings to work properly without compromising the results.

Besides the issue with missing the root node, I believe this returns the correct results, therefore I do not want to change the code drastically.

Here is some intentionally inconsistent dummy XML I am testing with:

<pets name="myPets" NUM="2">
    <dog name="allMyDogs" NUM="5">
        <dog name="Frank" cute="yes" color"brown" type="Lab" NUM="3"/>
        <dog name="Frank" NUM="3"/>
        <dog name="Spot"  NUM="4"/>
        <dog name="Rover" cute="yes" NUM="1"/>
        <dog name="Rupert" cute="yes" type="Pug" color="black" NUM="6"/>
        <cat name="Lucy" cute="yes" NUM="4"/>
    </dog>
    <cat name="allMyCats" NUM="4">
        <cat name="Simba" cute="yes" NUM="4"/>
        <cat name="Princess" cute="no" color="black" NUM="5"/>
        <cat name="Fluffy" cute="yes" color="grey" NUM="1"/>
        <cat name="Lucy" cute="yes" color="brown" NUM="3">
            <cat name="Lucy" cute="no"  NUM="35"/>
            <cat name="Lucy" cute="yes" purrs="yes" NUM="6"/>
        </cat>
        <cat name="Lucy"cute="no" color="grey" NUM="1"/>
        <dog name="Rover" cute="yes" NUM="24"/>
    </cat>
    <cat name="Lucy" NUM="9"/>
    <dog name="Rupert Jr" cute="yes" type="Pug" color="black" NUM="0"/>
</pets>

And here is the XSLT code:

    <xsl:key name="elem_key" match="elem" use="concat(@key, .)" />

    <xsl:variable name="all_data">
        <xsl:apply-templates select="*//*">
            <xsl:sort select="name()" />
        </xsl:apply-templates>
    </xsl:variable>

    <xsl:template match="//*[@NUM&lt;=4]">
        <elem key="{name()}">
            <xsl:copy-of select="@*" />
            <xsl:for-each select="@*">
                <xsl:sort select="name()" />
                <attribute>|<xsl:value-of select="name()" />|</attribute>
            </xsl:for-each>
        </elem>
    </xsl:template>

    <xsl:template match="/">
        <html>
            <body>
                <xsl:for-each select="msxsl:node-set($all_data)">
                    <xsl:for-each select="*[generate-id()=generate-id(key('elem_key',concat(@key, .))[1])]">
                        <table >
                            <tr>
                                <td>Element Name</td>
                                <xsl:for-each select="*">
                                    <td>
                                        <xsl:value-of select="translate(.,'|','')" />
                                    </td>
                                </xsl:for-each>
                            </tr>
                            <xsl:for-each select="key('elem_key', concat(@key, .))">
                                <xsl:variable name="curr_elem" select="." />
                                <tr>
                                    <td>
                                        <xsl:value-of select="@key" />
                                    </td>
                                    <xsl:for-each select="*">
                                        <td >
                                            <xsl:value-of select="$curr_elem/@*[name()=translate(current(),'|','')]" />
                                        </td>
                                    </xsl:for-each>
                                </tr>
                            </xsl:for-each>
                        </table>
                        <p />
                    </xsl:for-each>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>

And the desired formatting for output:

Element Name  Name    NUM   
pets          myPets  2   

Element Name  Name       Cute  Color  NUM  Type
dog           Frank      yes   brown  3    Lab
dog           Rupert Jr  yes   black  0    Pug

Element Name  Name   NUM   
dog           Frank  3   
dog           Spot   4   

Element Name  Name   Cute  NUM  
dog           Rover  yes   1  

Element Name  Name   Cute  NUM  
cat           Lucy   yes   4  
cat           Simba  yes   4  

Element Name  Name       NUM   
cat           allMyCats  4   

Element Name  Name    Color  Cute  NUM 
cat           Fluffy  grey   yes   1 
cat           Lucy    brown  yes   3 
cat           Lucy    grey   no    1

Thanks! 🙂 Let me know if I need to clear anything up!

  • 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-16T11:38:49+00:00Added an answer on May 16, 2026 at 11:38 am

    This transformation has all the mentioned problems fixed:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
     exclude-result-prefixes="msxsl">
    
        <xsl:key name="elem_key" match="elem" use="." />
    
        <xsl:key name="elem_key2" match="elem"
         use="concat(@key, @name, .)" />
    
        <xsl:variable name="all_data">
            <xsl:apply-templates select="//*[@NUM&lt;=4]">
                <xsl:sort select="name()" />
            </xsl:apply-templates>
        </xsl:variable>
    
        <xsl:template match="*">
            <elem key="{name()}">
                <xsl:copy-of select="@*" />
                <xsl:for-each select="@*">
                    <xsl:sort select="name()" />
                    <attribute>|<xsl:value-of select="name()" />|</attribute>
                </xsl:for-each>
            </elem>
        </xsl:template>
    
        <xsl:template match="/">
            <html>
                <body>
                    <xsl:for-each select="msxsl:node-set($all_data)">
                        <xsl:for-each select=
                        "*[generate-id()
                          =
                          generate-id(key('elem_key',.)[1])
                          ]">
                            <table >
                                <tr>
                                    <td>Element Name</td>
                                    <xsl:for-each select="*">
                                        <td>
                                            <xsl:value-of select=
                                                "translate(.,'|','')" />
                                        </td>
                                    </xsl:for-each>
                                </tr>
                                <xsl:for-each select="key('elem_key',.)">
                                  <xsl:variable name="curr_elem" select="." />
                                    <tr>
                                        <td>
                                            <xsl:value-of select="@key" />
                                        </td>
                                        <xsl:for-each select="*">
                                            <td >
                                                <xsl:value-of select=
                                              "$curr_elem/@*
                                                   [name()
                                                   =
                                                    translate(current(),
                                                             '|',
                                                             ''
                                                             )
                                                   ]" />
                                            </td>
                                        </xsl:for-each>
                                    </tr>
                                </xsl:for-each>
                            </table>
                            <p />
                        </xsl:for-each>
                    </xsl:for-each>
                </body>
            </html>
        </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the provided XML document:

    <pets name="myPets" NUM="2">
        <dog name="allMyDogs" NUM="5">
            <dog name="Frank" cute="yes" color="brown" type="Lab" NUM="3"/>
            <dog name="Frank" NUM="3"/>
            <dog name="Spot"  NUM="4"/>
            <dog name="Rover" cute="yes" NUM="1"/>
            <dog name="Rupert" cute="yes" type="Pug" color="black" NUM="6"/>
            <cat name="Lucy" cute="yes" NUM="4"/>
        </dog>
        <cat name="allMyCats" NUM="4">
            <cat name="Simba" cute="yes" NUM="4"/>
            <cat name="Princess" cute="no" color="black" NUM="5"/>
            <cat name="Fluffy" cute="yes" color="grey" NUM="1"/>
            <cat name="Lucy" cute="yes" color="brown" NUM="3">
                <cat name="Lucy" cute="no"  NUM="35"/>
                <cat name="Lucy" cute="yes" purrs="yes" NUM="6"/>
            </cat>
            <cat name="Lucy" cute="no" color="grey" NUM="1"/>
            <dog name="Rover" cute="yes" NUM="24"/>
        </cat>
        <cat name="Lucy" NUM="9"/>
        <dog name="Rupert Jr" cute="yes" type="Pug" color="black" NUM="0"/>
    </pets>
    

    the wanted result is produced:

    <html>
        <body>
            <table>
                <tr>
                    <td>Element Name</td>
                    <td>cute</td>
                    <td>name</td>
                    <td>NUM</td>
                </tr>
                <tr>
                    <td>cat</td>
                    <td>yes</td>
                    <td>Lucy</td>
                    <td>4</td>
                </tr>
                <tr>
                    <td>cat</td>
                    <td>yes</td>
                    <td>Simba</td>
                    <td>4</td>
                </tr>
                <tr>
                    <td>dog</td>
                    <td>yes</td>
                    <td>Rover</td>
                    <td>1</td>
                </tr>
            </table>
            <p></p>
            <table>
                <tr>
                    <td>Element Name</td>
                    <td>name</td>
                    <td>NUM</td>
                </tr>
                <tr>
                    <td>cat</td>
                    <td>allMyCats</td>
                    <td>4</td>
                </tr>
                <tr>
                    <td>dog</td>
                    <td>Frank</td>
                    <td>3</td>
                </tr>
                <tr>
                    <td>dog</td>
                    <td>Spot</td>
                    <td>4</td>
                </tr>
                <tr>
                    <td>pets</td>
                    <td>myPets</td>
                    <td>2</td>
                </tr>
            </table>
            <p></p>
            <table>
                <tr>
                    <td>Element Name</td>
                    <td>color</td>
                    <td>cute</td>
                    <td>name</td>
                    <td>NUM</td>
                </tr>
                <tr>
                    <td>cat</td>
                    <td>grey</td>
                    <td>yes</td>
                    <td>Fluffy</td>
                    <td>1</td>
                </tr>
                <tr>
                    <td>cat</td>
                    <td>brown</td>
                    <td>yes</td>
                    <td>Lucy</td>
                    <td>3</td>
                </tr>
                <tr>
                    <td>cat</td>
                    <td>grey</td>
                    <td>no</td>
                    <td>Lucy</td>
                    <td>1</td>
                </tr>
            </table>
            <p></p>
            <table>
                <tr>
                    <td>Element Name</td>
                    <td>color</td>
                    <td>cute</td>
                    <td>name</td>
                    <td>NUM</td>
                    <td>type</td>
                </tr>
                <tr>
                    <td>dog</td>
                    <td>brown</td>
                    <td>yes</td>
                    <td>Frank</td>
                    <td>3</td>
                    <td>Lab</td>
                </tr>
                <tr>
                    <td>dog</td>
                    <td>black</td>
                    <td>yes</td>
                    <td>Rupert Jr</td>
                    <td>0</td>
                    <td>Pug</td>
                </tr>
            </table>
            <p></p>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 533k
  • Answers 533k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your requirements essentially rule out the use of the .NET… May 17, 2026 at 12:29 am
  • Editorial Team
    Editorial Team added an answer If you have a DateTime object which represents a value… May 17, 2026 at 12:29 am
  • Editorial Team
    Editorial Team added an answer How about: par(mfrow = c(1, 3)) image(m1, zlim = c(-1,… May 17, 2026 at 12:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a piece of code that I don't know how to improve it.
I have wrote an application that syncs two folders together. The problem with the
I have a two ActiveX servers I need to handle it's events. the first
@Solved The two subquestions I have created have been solved (yay for splitting this
I have been using the following code (with jQuery v1.4.2) to set the 'selected'
Please look at the following test page : sample page showing two images .
I have a string which has a version number. I want to read the
I have developed a website using visual studio 2008. Which uses active index to
I have found some info on the subject ( like this link) , but
EDIT: The drop down menus have the following listed in them: Typing Course Daily

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.