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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:34:04+00:00 2026-06-18T13:34:04+00:00

I am completely new to xslt. Please help me to write style sheet.I have

  • 0

I am completely new to xslt. Please help me to write style sheet.I have input xml like this

Input XML:

    <elements>
     <e1>
       <pid>1</pid>
       <cid>2</cid>
     </e1>

     <e1>
      <pid>1</pid>
      <cid>3</cid>
     </e1>

     <e1>
      <pid>2</pid>
      <cid>4</cid>
    </e1>
    </elements>

Desired XML:

    <tree>
      <unit id="1">
        <unit id="2">
           <unit id="4">
             <data></data>
           </unit>
           <data></data>
        </unit>

        <unit id="3">
           <data></data>
        </unit>

        <data></data>

      </unit>
    </tree>

I feel this should be really easy but I’m struggling to find information about how to do this. My XSLT knowledge isn’t great.

  • 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-18T13:34:06+00:00Added an answer on June 18, 2026 at 1:34 pm

    I’m not 100% sure how you want the XSLT to determine from that input that the top id is 1 (is it because it’s the only pid value with no corresponding cid values, or is it always 1?). Nonetheless, this should do the job:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
      <xsl:key name="kItemsByC" match="e1" use="cid" />
      <xsl:key name="kItemsByP" match="e1" use="pid" />
    
      <xsl:template match="/">
        <tree>
          <xsl:call-template name="Unit">
            <!-- This will be the value of the <pid> that has no <cid> references to
                 it (assuming there is only one top-level <pid>) -->
            <xsl:with-param name="id" 
                            select="string(/elements/e1/pid[not(key('kItemsByC', .))])" />
          </xsl:call-template>
        </tree>
      </xsl:template>
    
      <xsl:template match="e1" name="Unit">
        <xsl:param name="id" select="cid" />
    
        <unit id="{$id}">
          <xsl:apply-templates select="key('kItemsByP', $id)" />
          <data />
        </unit>
      </xsl:template>
    </xsl:stylesheet>
    

    When this is run on your sample input, this produces:

    <tree>
      <unit id="1">
        <unit id="2">
          <unit id="4">
            <data />
          </unit>
          <data />
        </unit>
        <unit id="3">
          <data />
        </unit>
        <data />
      </unit>
    </tree>
    

    Note: The above XSLT has logic to attempt to dynamically locate the top-level ID. If it can be assumed that the top-level unit will always have ID 1, then one key and the above XSLT’s (somewhat) complicated formula can be eliminated:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
      <xsl:key name="kItemsByP" match="e1" use="pid" />
    
      <xsl:template match="/">
        <tree>
          <xsl:call-template name="Unit">
            <xsl:with-param name="id" select="1" />
          </xsl:call-template>
        </tree>
      </xsl:template>
    
      <xsl:template match="e1" name="Unit">
        <xsl:param name="id" select="cid" />
    
        <unit id="{$id}">
          <xsl:apply-templates select="key('kItemsByP', $id)" />
          <data />
        </unit>
      </xsl:template>
    </xsl:stylesheet>
    

    This also produces the requested output when run on your sample input.

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

Sidebar

Related Questions

Im completely new to XML/XSL/XSLT, and while i have been digging msdn, 3schools.com and
I am completely new to XSLT so please bear with me. I have two
Completely new to python i'm having problems finding this with version 2.4 I have
I am completely new to this, so please forgive me if I don't get
Completely new to using simple XML library in PHP, and have been using the
I am completely new to C and need help with this badly. Im reading
Completely new to most of this stuff, but basically Im playing around with the
Completely new to java and I have been playing around with regex in a
I am completely new to HTML5 and have been reading about it for the
I am completely new to the world of PostgreSQL, please be patient with me.

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.