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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:18:39+00:00 2026-05-19T03:18:39+00:00

I need to transform XML using xslt. Logic: Split the parent if the parent

  • 0

I need to transform XML using xslt.

Logic:

Split the parent if the parent having different child address and append the sequence number with parent name. Also need line number for the child.
Here we may have number of parent nodes and each parent node may have more number of child nodes.

I have tried numerous ways to achieve this and I am stuck with generating sequence number in the foreach. So can any one try and give a solution for this.

Source XML as below:

<Data>
   <Parent>
      <Name>P1</Name>
      <Child>
        <Name>CName1</Name>
        <Address>Address1</Address>
      </Child>
      <Child>
        <Name>CName2</Name>
        <Address>Address2</Address>
      </Child>
      <Child>
        <Name>CName3</Name>
        <Address>Address1</Address>
      </Child>
   </Parent>

   <Parent>
     <Name>P2</Name>
       <Child>
         <Name>CName1</Name>
         <Address>Address1</Address>
       </Child>
   </Parent>
</Data>

The target XML should be as below:

<Data>
  <Parent>
     <Name>P1_1</Name>
     <Address>Address1</Address>
     <Child>
       <LineNumber>1</LineNumber>
       <Name>CName1</Name>
     </Child>
     <Child>
        <LineNumber>2</LineNumber>
        <Name>CName3</Name>
     </Child>
  </Parent>

  <Parent>
      <Name>P1_2</Name>
      <Address>Address2</Address>
      <Child>
         <LineNumber>1</LineNumber>
         <Name>CName2</Name>
      </Child>
  </Parent>

  <Parent>
      <Name>P2_1</Name>
      <Address>Address1</Address>
      <Child>
          <LineNumber>1</LineNumber>
          <Name>CName1</Name>
       </Child>
  </Parent>
</Data>
  • 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-19T03:18:40+00:00Added an answer on May 19, 2026 at 3:18 am

    This XSLT 1.0 transformation:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:key name="kAddress" match="Child" use="concat(generate-id(..), '|', Address)" />
    
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="Parent">
        <xsl:variable name="parent" select="." />
        <xsl:for-each select="Child[
          generate-id()
          = 
          generate-id(key('kAddress', concat(generate-id($parent), '|', Address))[1])
        ]">
          <Parent>
            <Name><xsl:value-of select="concat(../Name, '_', position())" /></Name>
            <xsl:copy-of select="Address" />
            <xsl:apply-templates select="
              key('kAddress', concat(generate-id($parent), '|', Address))
            " />
          </Parent>
        </xsl:for-each>
      </xsl:template>
    
      <xsl:template match="Child">
        <xsl:copy>
          <xsl:copy-of select="@*" />
          <LineNumber><xsl:value-of select="position()" /></LineNumber>
          <xsl:apply-templates select="node()[not(self::Address)]" />
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    Generates the following output for your sample:

    <Data>
        <Parent>
            <Name>P1_1</Name>
            <Address>Address1</Address>
            <Child>
                <LineNumber>1</LineNumber>
                <Name>CName1</Name>
            </Child>
            <Child>
                <LineNumber>2</LineNumber>
                <Name>CName3</Name>
            </Child>
        </Parent>
        <Parent>
            <Name>P1_2</Name>
            <Address>Address2</Address>
            <Child>
                <LineNumber>1</LineNumber>
                <Name>CName2</Name>
            </Child>
        </Parent>
        <Parent>
            <Name>P2_1</Name>
            <Address>Address1</Address>
            <Child>
                <LineNumber>1</LineNumber>
                <Name>CName1</Name>
            </Child>
        </Parent>
    </Data>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have numerous XML files that are organized into different directories. I need to
I need to transform a valid XML document to the OFX v1.0.2 format .
I need to transform a piece of XML, so that the value of every
How do I encapsulate nodes around my XML blocks using XSLT? For example, I
I've the following xslt file: <?xml version=1.0 encoding=UTF-8?> <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <!-- USDomesticCountryList -
I'm starting using XSLT and write this scipt: <?xml version=1.0 encoding=ISO-8859-1?> <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>
I need to transform an Oracle SQL statement into a Stored Procedure therefore users
I need to transfer a database from a SQL Server instance test server to
I need to transfer DVD image files between a Windows XP computer and a
I am working on an application where i need to transfer mails from a

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.