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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:54:11+00:00 2026-06-09T12:54:11+00:00

I am using XSLT 1.0 and I’m looking to divide a xml document of

  • 0

I am using XSLT 1.0 and I’m looking to divide a xml document of for example related people in to families. The definition of family being one parent and its children.

So this:

<person name =”A”>
   <person name =”B”>
      <person name =”D” />
      <person name =”E” />
   </person>
   <person name=”C”>
      <person name =”F” />
      <person name =”G” />
      <person name =”H” />
   </person>
</person>

Will result in:

<family id =”1”>
   <person name =”A”>
      <person name =”B”/>
      <person name=”C”/>
   </person>
</family>

<family id =”2”>
   <person name =”B”>
      <person name =”D” />
      <person name =”E” />
   </person>
</family>


<family id =”3”>
   <person name=”C”>
      <person name =”F”/>
      <person name =”G”/>
      <person name =”H”/>
   </person>
</family>

How can I do this in a simple way?

  • 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-09T12:54:13+00:00Added an answer on June 9, 2026 at 12:54 pm

    One way is to firstly look for all person elements that have other person elements as children

    <xsl:apply-templates select="//person[person]"/>
    

    Then, for each such person element, you would copy the element, and its immediate children only

     <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="person" mode="child"/>
     </xsl:copy>
    

    Also, to get the family id, you could count all preceding person elements, and ancestors

    <family id="{count(preceding::person[person]) + count(ancestor-or-self::person)}">
    

    The “child” template would just copy the person and its attributes, but no children.

    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="/">
           <families>
              <xsl:apply-templates select="//person[person]"/>
           </families>
       </xsl:template>
    
       <xsl:template match="person[person]">
          <family id="{count(preceding::person[person]) + count(ancestor-or-self::person)}">
             <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates select="person" mode="child"/>
             </xsl:copy>
          </family>
       </xsl:template>
    
       <xsl:template match="person" mode="child">
          <xsl:copy>
             <xsl:copy-of select="@*"/>
          </xsl:copy>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your sample XML, the following is output

    <families>
       <family id="1">
          <person name="A">
             <person>B</person>
             <person>C</person>
          </person>
       </family>
       <family id="2">
          <person name="B">
             <person>D</person>
             <person>E</person>
          </person>
       </family>
       <family id="3">
          <person name="C">
             <person>F</person>
             <person>G</person>
             <person>H</person>
          </person>
       </family>
    </families>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to sort an XML structure using XSLT using xsltproc: The xml
I am using xslt 2.0 for transforming one xml format to another xml format.But
I am using xslt to transform an xml document to html for use in
Im looking to transform a piece of XML into a Pipe|Seperated|Value format using XSLT.
I've been looking into examples of using XML and XSLT in JavaScript and I
I'm using XSLT 1.0 an want to transform an XML file. It works fine
I'm using xslt 1.0 to transform some xml returned via a Server.Execute on an
I'm looking for a neat and efficient way to replace characters in XML document.
Looking to use XSLT to transform my XML. The sample XML is as follows:
I am using xslt 1.0 stylesheet to worlk on xml file data. I have

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.