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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:30:54+00:00 2026-06-15T13:30:54+00:00

Ok so I have gone through many related posts but have not been able

  • 0

Ok so I have gone through many related posts but have not been able to pinpoint an answer to my problem. I need to write an XSLT to transform an XML in the following format

<Message>
   <Receiver>
      <name>123</name>
      <address>111</address>
      <phone>1000</phone> 
   </Receiver>
   <List>
      <item>
         <no>1</no>
         <desc>one</desc>
      </item>
      <item>
         <no>2</no>
         <desc>two</desc>
      </item>
   </List>
<Message>

to this –

<Message>    
   <Receiver name=123>
       <address>111</address>
       <phone>1000</phone> 
   </Receiver>
   <List>
       <item no=1>
          <desc>one</desc>
       </item>
       <item no=2>
          <desc>two</desc>
       </item>
   </List>
</Message>

I have tried using the xsl template match tag.
but has failed miserably. Any ideas or help is much appreciated/

**note i have edited the post to show the actual problem – i initially posted just a part as to avoid confusion but as it seems the problem needs to be stated as a whole. Apologies for inconveniences.

  • 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-15T13:30:55+00:00Added an answer on June 15, 2026 at 1:30 pm

    This can be done by building upon the identity template. First you need a template to match the Receiver element, copy it but adding the name attribute at the same time

    <xsl:template match="Receiver">
        <Receiver name="{name}">
            <xsl:apply-templates select="@*|node()"/>
        </Receiver>
    </xsl:template>
    

    You can do a similar one for the item element. Note how this makes use of “Attribute Value Templates” to create the name attribute from the value of the name element.

    Then, you just need a template to match the name and no elements and ignore them, so they are not output.

    <xsl:template match="name|no" />
    

    Here is the full XSLT

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

    When applied to your XML the following is output

    <Message>
       <Receiver name="123">
          <address>111</address>
          <phone>1000</phone>
       </Receiver>
       <List>
          <item no="1">
             <desc>one</desc>
          </item>
          <item no="2">
             <desc>two</desc>
          </item>
      </List>
    </Message>
    

    Now, if you wanted to be more generic, and have a rule where the first ‘leaf’ element of any parent element is turned into an attribute, then try this XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:template match="*[*[1][not(*)]]">
            <xsl:copy>
                <xsl:attribute name="{name(*[1])}">
                    <xsl:value-of select="*"/>
                </xsl:attribute>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="*/*[1][not(*)]"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    This should also output the same results. I’ll leave it as an exercise to the reader how it works….

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have gone through many post but did not got any clear answer. I
I have gone through many articles but I am still not clear about the
Folks I have gone through many links/blogs. I see most of them not able
I have gone through many questions already asked about this problem but i didn't
Hi I have gone through many question on SO this too but its not
I have gone through many posts on SO regarding this issue: Tried everything in
I have gone through Google and Stack Overflow search, but nowhere I was able
I have gone through many links on this site and others but I could
I have gone through many related SO threads and got some basic info. Already
How to write joins in django,i have gone through below django documentation ,but joins

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.