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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:26:02+00:00 2026-05-13T06:26:02+00:00

I’m trying to learn XSLT (for some holiday coding fun). I think I now

  • 0

I’m trying to learn XSLT (for some holiday coding fun). I think I now have a pretty good understanding of the basics (grabbing subtrees, filtering-out elements, and renaming elements). Where I’m having trouble is when it comes to drastically reorganizing XML structures. If you had a deeply nested structure and wanted to flatten it, how would you go about doing so?

For example, let’s say I’m trying to transform a docbook snippet into html…

input (docbook):

<section>
  <title>Title A</title>
  <para>foo</para>
  <para>bar</para>
  <section>
    <title>Title B</title>
    <para>baz</para>
    <para>biz</para>
    <section>
      <title>Title C</title>
      <para>bing</para>
    </section>
  </section>
  <section>
    <title>Title D</title>
    <para>fizzle</para>
  </section>
</section>

output (html):

<h1>Title A</h1>
<p>foo</p>
<p>bar</p>
<h2>Title B</h2>
<p>baz</p>
<p>biz</p>
<h3>Title C</h3>
<p>bing</p>
<h2>Title D</h2>
<p>fizzle</p>

Is this where xsl:param and xsl:call-template come into play?

Thanks!

  • 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-13T06:26:02+00:00Added an answer on May 13, 2026 at 6:26 am

    Carsten’s test case works (with minor adjustments, you need terminate xsl:value-of with a /) , but always uses <h2> as the heading. If you want to use different heading elemenents according to the nesting level of the title, then you need something in addition to it:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
      <xsl:template match="/">
        <html>
          <body>
            <xsl:apply-templates />
          </body>
        </html>
      </xsl:template>
    
      <xsl:template match="title">
        <xsl:choose>
          <xsl:when test="count(ancestor::section) = 1">
            <h1><xsl:value-of select="." /></h1>
          </xsl:when>
          <xsl:when test="count(ancestor::section) = 2">
            <h2><xsl:value-of select="." /></h2>
          </xsl:when>
          <xsl:otherwise>
            <h3><xsl:value-of select="." /></h3>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    
      <xsl:template match="para">
        <p><xsl:value-of select="." /></p>
      </xsl:template>
    
    </xsl:stylesheet>
    

    The XPath function count(ancestor::section) will return a count of all <section> elements that are parents of the current element. In the example I have used <h1> and <h2> for the two outermost levels and <h3> for anything deeper nested, but of course you can use other differentiations at your disgression.

    It would even be possible to generate the number after the heading on the fly, using this expression:

      <xsl:template match="title">
        <xsl:variable name="heading">h<xsl:value-of select="count(ancestor::section)" /></xsl:variable>
        <xsl:element name="{$heading}">
          <xsl:value-of select="." />
        </xsl:element>
      </xsl:template>
    

    The xsl:variable section in there creates a variable with a value of h + nesting level. The variable then can be used as a parameter for the xsl:element element that allows you to dynamically define the name of the element you want to create.

    Followup: If you want to use only the h1-h6 as suggested, you could do it like this:

    <xsl:template match="title">
      <xsl:variable name="hierarchy" select="count(ancestor::section)"/>
      <xsl:variable name="heading">h<xsl:value-of select="$hierarchy" /></xsl:variable>
    
      <xsl:choose>
        <xsl:when test="$hierarchy > 6">
          <h6 class="{$heading}"><xsl:value-of select="." /></h6>
        </xsl:when>
        <xsl:otherwise>
          <xsl:element name="{$heading}">
            <xsl:value-of select="." />
          </xsl:element>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    

    This expression uses <h6 class="h..."> for anything that has a nesting deeper than 6. It uses <h1> through <h6> for all other hierarchy levels.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.