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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:40:26+00:00 2026-06-05T12:40:26+00:00

I’ve just started learning XML & XSLT and have a quick question regarding Xpath.

  • 0

I’ve just started learning XML & XSLT and have a quick question regarding Xpath.

Here’s the XML code:

<root>

<shop>
    <person> 
        <employee> 
            <name> Alexis </name> 
            <role> Manager </role> 
            <task> Sales </task> 
        </employee> 
    </person>
</shop>

<person> 
    <employee> 
        <role> Supervisor </role> 
        <name> Blake </name> 
        <task> Control </task> 
    </employee> 
</person>


</root>

and here’s the XSLT code:

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

<xsl:template match="root"> 
<html><head></head> 
<body><xsl:apply-templates/> 
</body> 
</html> 
</xsl:template> 


<xsl:template match="shop"> 
<xsl:apply-templates select="/root/*/*"/> 
</xsl:template> 

<xsl:template match="employee"> 
<u> <xsl:apply-templates select="name"/> </u> 
(Task: <xsl:apply-templates select="task"/>) 
<br></br> 
</xsl:template> 

<xsl:template match="person2"> 
<xsl:apply-templates /> 
</xsl:template> 

</xsl:stylesheet>

The output is:

Alexis (Task: Sales )
Blake (Task: Control )
Blake (Task: Control ) 

What I don’t understand is why the last part is duplicated? I know that it’s due to this part of the XSLT code:

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

but that’s only because I was fiddling around with the code and displaying it in Firefox. I don’t understand why though.

From what I understand, it’s selecting all the grandchildren elements of “root”, like so:

root/shop/person

but why isn’t Alexis repeated as well? Only Blake is repeated…

  • 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-06-05T12:40:28+00:00Added an answer on June 5, 2026 at 12:40 pm

    In your root matching template, you do <xsl:apply-templates/> which will pick the first shop and person elements. With respect to the person element, there isn’t a specific template for this, and so XSLT will continue to match its child elements and pick up the employee for ‘Blake’

    However, there is a matching template for shop and the issue is indeed with what you do in the template that matches it.

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

    Because you have started the xpath expression with /root this will start mathing relative to the root element of the document, and not the shop element you are currently positioned on. This means it will select the elements /root/shop/person and /root/person/employee which leads to your duplicate ‘Blake’. However, as you are not matching the employee element for ‘Alexis’ elsewhere, this is only output once.

    You probably need to do just this instead, to match the employee element

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

    This will match all grand-children of the current element. * will match the child element, and so */* will match the child elements of the child elements.

    However, if the intent is to output just the employee elements, you can simplify your XSLT by taking advantage of the fact that the default template matching behaviour for an element is to process its child elements. Try this XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="html" indent="yes" />
    
       <xsl:template match="root">
          <html>
             <head/>
             <body>
                <xsl:apply-templates/>
             </body>
          </html>
       </xsl:template>
    
       <xsl:template match="employee">
          <u>
             <xsl:value-of select="name"/>
          </u>
          <xsl:value-of select="concat(' (Task:', task, ')')"/>
          <br/>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your XML, the following is output

    <html>
       <head>
       </head>
       <body>
          <u> Alexis </u> (Task: Sales )<br>
          <u> Blake </u> (Task: Control )<br>
       </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.