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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:51:36+00:00 2026-05-30T13:51:36+00:00

What would be the best (preferably most efficient) method to do the following: Consider

  • 0

What would be the best (preferably most efficient) method to do the following:

Consider I have an XML document as such:

<comments question_id="123">
    <comment id="1">
       This is the first comment
    </comment>
    <comment id="2">
       This is the second comment
    </comment>
</comments>

Now, given that I specify the “path” to each data-block, i.e. in this case:

path: /comments/comment

I would like to break up the document into n-number of sub-parts, in this case 2:

<comments question_id="123">
    <comment id="1">
       This is the first comment
    </comment>
</comments>

<comments question_id="123">
    <comment id="2">
       This is the second comment
    </comment>
</comments>

So, essentially, what I am trying to do is get each node produced by “/comments/comment”, but also retain all “outer” parent nodes data.

EDIT:

Note: this needs to be dynamic, or generic. I.e. the above data xml is just an example. I want to be able to transform any xml document to this effect, given a “path” representing each data-node. And the rest is the outer body of the xml.

  • 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-30T13:51:37+00:00Added an answer on May 30, 2026 at 1:51 pm

    A generic “shredding” solution can be found in my answer to this question: https://stackoverflow.com/a/8597577/36305

    Here is the complete 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:param name="pLeafNodes" select="//comment"/>
    
         <xsl:template match="/">
          <t>
            <xsl:call-template name="StructRepro"/>
          </t>
         </xsl:template>
    
         <xsl:template name="StructRepro">
           <xsl:param name="pLeaves" select="$pLeafNodes"/>
    
           <xsl:for-each select="$pLeaves">
             <xsl:apply-templates mode="build" select="/*">
              <xsl:with-param name="pChild" select="."/>
              <xsl:with-param name="pLeaves" select="$pLeaves"/>
             </xsl:apply-templates>
           </xsl:for-each>
         </xsl:template>
    
          <xsl:template mode="build" match="node()|@*">
              <xsl:param name="pChild"/>
              <xsl:param name="pLeaves"/>
    
             <xsl:copy>
               <xsl:apply-templates mode="build" select="@*"/>
    
               <xsl:variable name="vLeafChild" select=
                 "*[count(.|$pChild) = count($pChild)]"/>
    
               <xsl:choose>
                <xsl:when test="$vLeafChild">
                 <xsl:apply-templates mode="build"
                     select="$vLeafChild
                            |
                              node()[not(count(.|$pLeaves) = count($pLeaves))]">
                     <xsl:with-param name="pChild" select="$pChild"/>
                     <xsl:with-param name="pLeaves" select="$pLeaves"/>
                 </xsl:apply-templates>
                </xsl:when>
                <xsl:otherwise>
                 <xsl:apply-templates mode="build" select=
                 "node()[not(.//*[count(.|$pLeaves) = count($pLeaves)])
                        or
                         .//*[count(.|$pChild) = count($pChild)]
                        ]
                 ">
    
                     <xsl:with-param name="pChild" select="$pChild"/>
                     <xsl:with-param name="pLeaves" select="$pLeaves"/>
                 </xsl:apply-templates>
                </xsl:otherwise>
               </xsl:choose>
             </xsl:copy>
         </xsl:template>
         <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <comments question_id="123">
        <comment id="1">
          This is the first comment
      </comment>
        <comment id="2">
          This is the second comment
     </comment>
    </comments>
    

    the wanted, correct result is produced:

    <t>
       <comments question_id="123">
          <comment id="1">
          This is the first comment
      </comment>
       </comments>
       <comments question_id="123">
          <comment id="2">
          This is the second comment
     </comment>
       </comments>
    </t>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation I think would be best to show in sample
In Java, what would the best way be to have a constantly listening port
What would be the best method of going about doing this? I want to
What is the best, most preferable (and, if possible, efficient) way to pass an
What would be the best/quickest way to have a IDE-type setup when I have
I have some xml files from an external client and i would like to
Possible Duplicate: jQuery/Javascript collision detection What would be the best way using preferably using
When requesting hardware for a WebLogic server, what hardware would best improve its performance?
Best would be if it performs the highlighting exactly as it is in Visual
What would be best practice to localize your ASP.NET MVC application? I would like

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.