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

  • SEARCH
  • Home
  • 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 1015033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:20:28+00:00 2026-05-16T10:20:28+00:00

So I have a really confusing problem on my hands.. It seems as though

  • 0

So I have a really confusing problem on my hands..

It seems as though that the entire XML data hierarchy is not being searched through when using an XPath expression in XSL.

Some dummy XML data:

<pets name="myPets" NUM="2">
    <dog name="allMyDogs" NUM="5">
        <dog name="Frank"  NUM="3"/>
        <dog name="Spot"  NUM="4"/>
        <dog name="Rover"  NUM="1"/>
        <dog name="Rupert" NUM="6"/>
        <cat name="Lucy"  NUM="4"/>
    </dog>
    <cat name="allMyCats" NUM="4">
        <cat name="Simba" NUM="4"/>
        <cat name="Princess"  NUM="5"/>
        <cat name="Fluffy" NUM="1"/>
        <cat name="Lucy"  NUM="3"/>
        <cat name="Lucy"  NUM="35"/>
        <cat name="Lucy"  NUM="6"/>
        <cat name="Lucy" NUM="1"/>
    </cat>
    <cat name="Lucy" NUM="9"/>
</pets>

The following is the portion of XSLT code I believe is causing the issue:

 <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>

The XPath expression used:

//*[@NUM&lt;=4]

(Above should generate many results)

The incorrect results I get:

Element Name    name     NUM
pets            myPets   2

As you can see it seems to stop at the root.

If I change the XPath to:

//*[@NUM=4]

I get these incorrect results:

Element Name  name    NUM
dog        Spot      4


Element Name  name      NUM
cat        Lucy      4


Element Name  name      NUM
cat        allMyCats   4

What appears to happen is that it will stop searching down into the hierarchy once it has found a match. The first two (Spot and Lucy) are correct, but then it stopped at allMyCats, when there is a child node of allMyCats (Simba) that has a NUM of 4.

Can anyone help me fix this code so that it returns the correct results? I’m quite frustrated! 🙁

Thanks!

  • 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-16T10:20:29+00:00Added an answer on May 16, 2026 at 10:20 am

    Just change:

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

    To:

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

    The first (of many) problem is:

    <xsl:apply-templates select="*">
    

    selects all children elements of the current node. The current node is / and it has only one child — the top element pets.

    You actually want to collect the data for all children of pets.

    There are other problems, but I don’t have the space and time to address them here.

    The complete corrected code is below:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    version="1.0">
    
     <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="concat(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=
                              "substring-before(translate(.,'|',''),'=')" />
                          </td>
                        </xsl:for-each>
                      </tr>
                        <tr>
                          <td>
                            <xsl:value-of select="@key" />
                          </td>
                          <xsl:for-each select="*">
                            <td>
                              <xsl:value-of select=
                              "substring-after
                                  (translate(current(),'|',''),
                                   '='
                                   )"/>
                            </td>
                          </xsl:for-each>
                        </tr>
                    </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"  NUM="3"/>
            <dog name="Spot"  NUM="4"/>
            <dog name="Rover"  NUM="1"/>
            <dog name="Rupert" NUM="6"/>
            <cat name="Lucy"  NUM="4"/>
        </dog>
        <cat name="allMyCats" NUM="4">
            <cat name="Simba" NUM="4"/>
            <cat name="Princess"  NUM="5"/>
            <cat name="Fluffy" NUM="1"/>
            <cat name="Lucy"  NUM="3"/>
            <cat name="Lucy"  NUM="35"/>
            <cat name="Lucy"  NUM="6"/>
            <cat name="Lucy" NUM="1"/>
        </cat>
        <cat name="Lucy" NUM="9"/>
    </pets>
    

    the wanted result (multiple animals) is produced:

    <html xmlns:msxsl="urn:schemas-microsoft-com:xslt">
        <body>
            <table>
                <tr>
                    <td>Element Name</td>
                    <td>name</td>
                    <td>NUM</td>
                </tr>
                <tr>
                    <td>cat</td>
                    <td>Lucy</td>
                    <td>4</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>
            </table>
            <p></p>
            <table>
                <tr>
                    <td>Element Name</td>
                    <td>name</td>
                    <td>NUM</td>
                </tr>
                <tr>
                    <td>cat</td>
                    <td>Simba</td>
                    <td>4</td>
                </tr>
            </table>
            <p></p>
            <table>
                <tr>
                    <td>Element Name</td>
                    <td>name</td>
                    <td>NUM</td>
                </tr>
                <tr>
                    <td>cat</td>
                    <td>Fluffy</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>Lucy</td>
                    <td>3</td>
                </tr>
            </table>
            <p></p>
            <table>
                <tr>
                    <td>Element Name</td>
                    <td>name</td>
                    <td>NUM</td>
                </tr>
                <tr>
                    <td>cat</td>
                    <td>Lucy</td>
                    <td>1</td>
                </tr>
            </table>
            <p></p>
            <table>
                <tr>
                    <td>Element Name</td>
                    <td>name</td>
                    <td>NUM</td>
                </tr>
                <tr>
                    <td>dog</td>
                    <td>Frank</td>
                    <td>3</td>
                </tr>
            </table>
            <p></p>
            <table>
                <tr>
                    <td>Element Name</td>
                    <td>name</td>
                    <td>NUM</td>
                </tr>
                <tr>
                    <td>dog</td>
                    <td>Spot</td>
                    <td>4</td>
                </tr>
            </table>
            <p></p>
            <table>
                <tr>
                    <td>Element Name</td>
                    <td>name</td>
                    <td>NUM</td>
                </tr>
                <tr>
                    <td>dog</td>
                    <td>Rover</td>
                    <td>1</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

Related Questions

I'm having a really confusing problem. I have a UIWebView that is contained within
I have a really confusing problem. I have a PHP script that polls another
I have a technical problem and it's really confusing me. I apologise in advance
I right now have a problem that is rather confusing to me: I have
I'm just learning MVC3 now and this is really confusing me. I have a
I have really simple XML (HTML) parsing ANTLR grammar: wiki: ggg+; ggg: tag |
I really have problem with this one. So I have a jar with a
Pardon me for giving you a confusing title on this problem. I'm really confused
I currently ran into a problem I can not really solve by myself: I
I am really confusing, I have a js file with a lot of scripts,

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.