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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:35:39+00:00 2026-06-09T18:35:39+00:00

I got several XML -elements referring to eachother using a parent subelement. Not all

  • 0

I got several XML -elements referring to eachother using a “parent” subelement. Not all toplevel -elements need copying, so I have a fixed list of (main-)section uniqueNames which do need to be copied.

List of mainsections of which subsections should be copied in my XSL:

<xsl:variable name="mainSections">|news|sport|</xsl:variable>

Source XML Main sections:

<section uniqueName="news">
</section>

<section uniqueName="sport">
</section>

<section uniqueName="travel">
</section>

Source XML Sub sections:

<section uniqueName="national_news">
  <parent uniqueName="news"/>
</section>

<section uniqueName="regional_news">
  <parent uniqueName="national_news"/>
</section>

<section uniqueName="city_news">
  <parent uniqueName="regional_news"/>
</section>

<section uniqueName="travel_europe">
  <parent uniqueName="travel"/>
</section>

<section uniqueName="holland">
  <parent uniqueName="travel_europe"/>
</section>

Etc.

Because the subsections are prone to moving, changing or deletion between development-phase and production of my XSL, I need to determine dynamically if any given subsection is a child of one of the main sections. There are also countless subsections in total. Hardcoding a list of subsections to copy is undoable.

Subsection depth can be really deep, as shown in the example: Section “city_news” has “regional_news” as parent, which has “national_news” as parent which has “news” as parent, so this section needs copying, because it is eventually a child of “news”. The toplevel section “Travel” for section “holland” (under subsection “travel_europe”) is not on the list to be copied, so it needs to be ignored.

At first I tried checking each element if the parent is in the list, but that was just stupid, it only gets me 1 level deep.
My last attempt was to create a javascript first indexing all sections, then analyzing the structure eliminating all sections related to mainsections not on the list.
But calling the script proved to be a challenge as well, since XSL has no means of its own to call javascript.

I am a bit clueless on how to proceed on this… Any solutions or suggestions?

The source XML document is 3500 lines and the info in there does not matter for this question. The question is how do I filter out subsections of subsections of subsections of a mainsection, or: How do I determine if a subsection is a child of on of the specified main-sections a few steps up the tree.
Consider the 5 mentioned subsections to be input, then the output should be:

<section uniqueName="national_news">
  <parent uniqueName="news"/>
</section>

<section uniqueName="regional_news">
  <parent uniqueName="national_news"/>
</section>

<section uniqueName="city_news">
  <parent uniqueName="regional_news"/>
</section>

Since

<section uniqueName="travel_europe">
  <parent uniqueName="travel"/>
</section>

<section uniqueName="holland">
  <parent uniqueName="travel_europe"/>
</section>

relate to the “travel” main-section, which is not on the list to be copied.

  • 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-09T18:35:40+00:00Added an answer on June 9, 2026 at 6:35 pm

    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:param name="pWanted" select="'|news|sport|'"/>
    
     <xsl:key name="kChildren" match="section" use="parent/@uniqueName"/>
    
     <xsl:template match="/*">
         <xsl:call-template name="getSections">
           <xsl:with-param name="pParents" select=
           "section[contains($pWanted, concat('|',@uniqueName, '|'))]"/>
         </xsl:call-template>
     </xsl:template>
    
     <xsl:template name="getSections">
      <xsl:param name="pParents" select="/.."/>
    
      <xsl:if test="$pParents">
        <xsl:copy-of select="$pParents"/>
        <xsl:call-template name="getSections">
         <xsl:with-param name="pParents"
            select="key('kChildren', $pParents/@uniqueName)"/>
        </xsl:call-template>
      </xsl:if>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied to the following XML document (no such was provided!!!):

    <t>
        <section uniqueName="news"></section>
        <section uniqueName="sport"></section>
        <section uniqueName="travel"></section>
        <section uniqueName="national_news">
            <parent uniqueName="news"/>
        </section>
        <section uniqueName="regional_news">
            <parent uniqueName="national_news"/>
        </section>
        <section uniqueName="city_news">
            <parent uniqueName="regional_news"/>
        </section>
        <section uniqueName="travel_europe">
            <parent uniqueName="travel"/>
        </section>
        <section uniqueName="holland">
            <parent uniqueName="travel_europe"/>
        </section>
    </t>
    

    produces the wanted (not exactly specified in the question!!!), correct result:

    <section uniqueName="news"/>
    <section uniqueName="sport"/>
    <section uniqueName="national_news">
       <parent uniqueName="news"/>
    </section>
    <section uniqueName="regional_news">
       <parent uniqueName="national_news"/>
    </section>
    <section uniqueName="city_news">
       <parent uniqueName="regional_news"/>
    </section>
    

    Explanation:

    Proper use of keys and recursion.

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

Sidebar

Related Questions

I've got several HTML5 audio elements on a page and am using jQuery to
I've got several pages on my ASP.NET MVC 3 website (not that the technology
I have got an application, that is getting data via XML-files. During the parsing-part
I have a huge XML that i need to work on, but i only
I've got several hundred warnings due to missing xml comments from a single DataSet
I have got the following XML grammar to detect a number like 1000 or
I'm running JBoss AS 7.1.0.CR1b. I've got several datasources defined in my standalone.xml e.g.
Hi I've got several XSLT 2.0 files. I need to transform these with C#..
I got a XML-document with several values, this is part of it: <result> <element>
My application has got several pages. On some pages I have given a back

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.