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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:19:50+00:00 2026-06-04T12:19:50+00:00

I have been trying to write a style sheet for the following documents which

  • 0

I have been trying to write a style sheet for the following documents which can check for all the users their awards and add the name of that user as subelement to the /awards/award element

<document>
    <users>
        <user identity="1">
            <name>
                <first>abc</first>
                <last>xyz</last>
            </name>
            <achievements>
                <achievement>Award A</achievement>
                <achievement>Award B</achievement>
            </achievements>
        </user>
        <user identity="2">
            <name>
                <first>123</first>
                <last>DEF</last>
            </name>
            <achievements>
                <received>Award A</received>
            </achievements>
        </user>
        <user identity = "3">
            <name>
                <first>aaa</first>
                <last>bbb</last>
            </name>
            <achievements>
                <received>Award B</received>
            </achievements>
        </user>
    </users>
    <!--awards-->
    <awards>
        <award>
            <name>Award A</name>
            <!--here the list of the users who recieved this award has to be included-->
        </award>
        <award>
            <name>Award B</name>
        </award>
    </awards>
</document>

I wrote down the following incomplete stylesheet

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="awards/award">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
                <!--how can i create a list of all users who have this award-->
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

I can not figure out the way of writing a query in the template “awards/award” like “add all those users who have received this award as sub elements called user and the value of added element to be the name of the users” and gives following output. I will much appreciate if someone can guide me to the correct approach.

<document>
    <users>
        <user identity="1">
            <name>
                <first>abc</first>
                <last>xyz</last>
            </name>
            <achievements>
                <achievement>Award A</achievement>
                <achievement>Award B</achievement>
            </achievements>
        </user>
        <user identity="2">
            <name>
                <first>123</first>
                <last>DEF</last>
            </name>
            <achievements>
                <received>Award A</received>
            </achievements>
        </user>
        <user identity="3">
            <name>
                <first>aaa</first>
                <last>bbb</last>
            </name>
            <achievements>
                <received>Award B</received>
            </achievements>
        </user>
    </users>
    <!--awards-->
    <awards>
        <award>
            <name>Award A</name>
            <user identity="1">abc xyz</user>
            <user identity="2">123 DEF</user>    
        </award>
        <award>
            <name>Award B</name>
            <user identity="1">abc xyz</user>
            <user identity="3">aaa bbb</user>
        </award>
    </awards>
</document>

thanks in advance

  • 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-04T12:19:52+00:00Added an answer on June 4, 2026 at 12:19 pm

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kUserByAward" match="user"
              use="achievements/*[self::achievement or self::received]"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="award">
       <award>
        <xsl:apply-templates select="node()"/>
        <xsl:apply-templates select="key('kUserByAward', name)" mode="inclusion"/>
       </award>
     </xsl:template>
    
     <xsl:template match="user" mode="inclusion">
      <user>
        <xsl:copy-of select="name"/>
      </user>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <document>
        <users>
            <user>
                <name>
                    <first>abc</first>
                    <last>xyz</last>
                </name>
                <achievements>
                    <achievement>Award A</achievement>
                    <achievement>Award B</achievement>
                </achievements>
            </user>
            <user>
                <name>
                    <first>dsd</first>
                    <last>sdasdsadsa</last>
                </name>
                <achievements>
                    <received>Award A</received>
                </achievements>
            </user>
            <user>
                <name>
                    <first>aaa</first>
                    <last>bbb</last>
                </name>
                <achievements>
                    <received>Award B</received>
                </achievements>
            </user>
        </users>
        <!--awards-->
        <awards>
            <award>
                <name>Award A</name>
                <!--here the list of the users who recieved this award has to be included-->
            </award>
            <award>
                <name>Award B</name>
            </award>
        </awards>
    </document>
    

    produces (what I guess should be) the wanted, correct result:

    <document>
       <users>
          <user>
             <name>
                <first>abc</first>
                <last>xyz</last>
             </name>
             <achievements>
                <achievement>Award A</achievement>
                <achievement>Award B</achievement>
             </achievements>
          </user>
          <user>
             <name>
                <first>dsd</first>
                <last>sdasdsadsa</last>
             </name>
             <achievements>
                <received>Award A</received>
             </achievements>
          </user>
          <user>
             <name>
                <first>aaa</first>
                <last>bbb</last>
             </name>
             <achievements>
                <received>Award B</received>
             </achievements>
          </user>
       </users><!--awards-->
       <awards>
          <award>
             <name>Award A</name><!--here the list of the users who recieved this award has to be included-->
             <user>
                <name>
                   <first>abc</first>
                   <last>xyz</last>
                </name>
             </user>
             <user>
                <name>
                   <first>dsd</first>
                   <last>sdasdsadsa</last>
                </name>
             </user>
          </award>
          <award>
             <name>Award B</name>
             <user>
                <name>
                   <first>abc</first>
                   <last>xyz</last>
                </name>
             </user>
             <user>
                <name>
                   <first>aaa</first>
                   <last>bbb</last>
                </name>
             </user>
          </award>
       </awards>
    </document>
    

    Explanation: Proper use of keys and overriding the identity rule.

    UPDATE: The OP has shown the actually wanted output format. This is very close to what I guessed. The only modification is to use this template, insted the one in the initial solution:

     <xsl:template match="user" mode="inclusion">
      <user identity="{@identity}">
        <xsl:copy-of select="concat(name/first, ' ', name/last)"/>
      </user>
     </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to write a regex that will remove whitespace following a
I have been trying to write a script where I can post data to
I have been trying to write a bare-bones ping scanner using Perl for internal
I have been trying to write an image on a layer using Quartz but
I have been trying to write my own diff3 wrap script for SVN and
I have been trying to write a custom .screenrc file TEST as follows startup_message
I have been trying to write a shortest path algorithm, dijkstras algorithm, finding the
I am new to programming. I have been trying to write a function in
Right now I have been trying to use Launchpad's API to write a small
I have been trying to create a HTML E-mail, which is just one image

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.