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

The Archive Base Latest Questions

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

Given this XML <Xml> <Thing id=1 > <Foo id=11 parentId=12/> <Foo id=12/> </Thing> <Thing

  • 0

Given this XML

<Xml>
    <Thing id="1"  >
        <Foo id="11" parentId="12"/>
        <Foo id="12"/>
    </Thing>
    <Thing id="2" parentId="1" />
    <Thing id="3" parentId="2" />
    <Thing id="4">
        <Foo id="11" parentId="15"/>
        <Foo id="12" parentId="14"/>
        <Foo id="13" parentId="11"/>
        <Foo id="14" parentId="15"/>
        <Foo id="15"/>      
    </Thing>
</Xml>

I want to grab every collection of siblings, and assemble them into their own hierarchy.

Every “Thing” node with a parentId value, should nest under the corresponding Thing node.
Every “Foo” node with a parentId value, should nest under the corresponding Foo node — but only within its siblings. The example has two sets of Foo siblings.

I’m trying to create this:

<Xml>
    <Thing id="1" >
        <Foo id="12">
            <Foo id="11" parentId="12"/>
        </Foo>        
        <Thing id="2" parentId="1" >
            <Thing id="3" parentId="2" />
        </Thing>
    </Thing>            
    <Thing id="4" >
        <Foo id="14" parentId="12">
            <Foo id="12" parentId="14"/>
        </Foo>
        <Foo id="15">
            <Foo id="11" parentId="15">
                <Foo id="13" parentId="11"/>
            </Foo>
        </Foo>
    </Thing>
</Xml>

This example comes close:
How can I use XSLT 1.0 to add structure to a non-heirarchal XML file?

I’ve used the identity template to retain all nodes and attributes. Then I want an overriding template to match on all nodes that have a sibling (following or preceding) such that the sibling’s @parentId value equals my @id value. The closest I could come was hard coding an id/parentId value to match on.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output indent="yes"/>

    <!-- override identity rule with template to match on
        a node who has siblings, where sibling/@parentId == ./@id
    -->
    <xsl:template match="node()[@id='1' and (preceding-sibling::*[@parentId = 1] or following-sibling::*[@parentId = 1])]">
       <captured>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
       </captured>
    </xsl:template>  


    <!-- identity rule -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>


</xsl:stylesheet>

I can’t see how to grab the current nodes @id value to use in the predicate of the Xpath matching siblings based on parentId value.

And then I want to nest the current nodes siblings underneath it, where the siblings @ParentId equals my @id.

  • 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-04T13:54:20+00:00Added an answer on June 4, 2026 at 1:54 pm

    First of all do notice an error in the provided XML document:

        <Foo id="12" parentId="14"/>
        <Foo id="13" parentId="11"/>
        <Foo id="14" parentId="12"/>
    

    There is a circular relation between Foo with id 12 and Foo with 14. This makes a cycle and isn’t “hierarchy”. Also, these two Foo elements are unreachable from the top of the hierarchy! Please, correct.

    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="kElemById" match="*"
       use="concat(generate-id(..), '+', @id)"/>
     <xsl:key match="*" name="kDescendants"
      use="concat(generate-id(key('kElemById', 
                                  concat(generate-id(..), '+',@parentId))),
                 '+', @parentId)"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*">
      <Xml>
       <xsl:apply-templates select="*[not(@parentId)]"/>
      </Xml>
     </xsl:template>
    
     <xsl:template match="*/*">
       <xsl:copy>
        <xsl:apply-templates select="@*"/>
    
        <xsl:apply-templates select="*[not(@parentId)]"/>
    
        <xsl:apply-templates select=
           "key('kDescendants', concat(generate-id(), '+',  @id))"/>
      </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <Xml>
        <Thing id="1"  >
            <Foo id="11" parentId="12"/>
            <Foo id="12"/>
        </Thing>
        <Thing id="2" parentId="1" />
        <Thing id="3" parentId="2" />
        <Thing id="4">
            <Foo id="11" parentId="15"/>
            <Foo id="12" parentId="14"/>
            <Foo id="13" parentId="11"/>
            <Foo id="14" parentId="12"/>
            <Foo id="15"/>
        </Thing>
    </Xml>
    

    produces correct result, excluding any unreachable elements:

    <Xml>
       <Thing id="1">
          <Foo id="12">
             <Foo id="11" parentId="12"/>
          </Foo>
          <Thing id="2" parentId="1">
             <Thing id="3" parentId="2"/>
          </Thing>
       </Thing>
       <Thing id="4">
          <Foo id="15">
             <Foo id="11" parentId="15">
                <Foo id="13" parentId="11"/>
             </Foo>
          </Foo>
       </Thing>
    </Xml>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

given this xml: <root> <list> <!-- foo's comment --> <item name=foo /> <item name=bar
Given an XML structure like this: <garage> <car>Firebird</car> <car>Altima</car> <car>Prius</car> </garage> I want to
Given this XML: <?xml version=1.0 encoding=utf-8?> <content dataType=XML> <item type=Promotion name=Sample Promotion expires=04/01/2009> <![CDATA[
Given this XML: <Items> <Item>a</Item> <Item>b</Item> </Items> How can I deserialize this using XmlSerializer
Given this example XML file: <doc> <tag> Hello ! </tag> <tag> My name is
This is my XML file <BADFM> <Given> <Ord> <Bag IDC=DM /> </Ord> </Given> </BADFM>
Given an XML document like this: <!DOCTYPE doc SYSTEM 'http://www.blabla.com/mydoc.dtd'> <author>john</author> <doc> <title>&title;</title> </doc>
given an xml string like this: <some><nested><xml>value</xml></nested></some> what's the best option (using ruby) to
Given an xml structure like this <gesmes:Envelope> <gesmes:subject>Reference rates</gesmes:subject> <gesmes:Sender> <gesmes:name>European Central Bank</gesmes:name> </gesmes:Sender>
I want to parse the date for entries given by SVN: svn list --xml

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.