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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:04:07+00:00 2026-06-04T02:04:07+00:00

(I have researched this question extensively but not found an adequate solution yet.) I

  • 0

(I have researched this question extensively but not found an adequate solution yet.)

I am using XSLT to convert a series of XML files to a ‘tree’-like HTML document, where the parent-child relationships between elements is reflected with indenting.

The solution below works fine but it’s too verbose, and we also need to accommodate adding a few new
elements to the schema in the future.

What’s a good general-purpose solution that will do what we need? I’ve seen recursive solutions online where a “depth” variable is used and is incremented and passed in as a parameter, and used to drive the indenting level; this seems to be a common pattern however I could not get it to work for me.

Thanks.

So my data files look like this:

<document>
  <metadata>
    <version>1.0</version>
    <date>1/1/12</date>
    etc...
  </metadata>
  <trackdata>
    <tracks>
      <track>123</track>
      <track>456</track>
      etc...
  </trackdata>
</document>

And the XSLT (Note the different values for the text-indent at the different levels):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no"/>
    <xsl:template match="@*|node()">
        <xsl:apply-templates select="@*|node()" />
    </xsl:template>

        <xsl:template match="metadata">
            Metadata: <xsl:apply-templates/>
        </xsl:template>

        <xsl:template match="version">
          <div style="text-indent:3em">Version: <xsl:value-of select="text()"/></div>
        </xsl:template>

        <xsl:template match="date">
          <div style="text-indent:3em">Date: <xsl:value-of select="text()"/></div>
        </xsl:template>

        <xsl:template match="trackdata">
          Track Data: <xsl:value-of select="text()"/></div>
        </xsl:template>

        <xsl:template match="tracks">
            <div style="text-indent:3em">Tracks: <xsl:apply-templates/>
        </xsl:template>

        <xsl:template match="track">
          <div style="text-indent:6em">Track: <xsl:value-of select="text()"/></div>
        </xsl:template>

etc…

  • 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-04T02:04:08+00:00Added an answer on June 4, 2026 at 2:04 am

    Try creating a variable to hold the text-indent value. Something like this:

      <xsl:template match="version">
        <xsl:variable name="vIndent" select="count(ancestor::*) * 3"/>    
        <div style="text-indent:{$vIndent}em">Version: <xsl:value-of select="text()"/></div>
      </xsl:template>
    

    Here’s another full example of usage…

    XML Input (well-formed)

    <document>
      <metadata>
        <version>1.0</version>
        <date>1/1/12</date>
      </metadata>
      <trackdata>
        <tracks>
          <track>123</track>
          <track>456</track>
        </tracks>
      </trackdata>
    </document>
    

    XSLT 1.0

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="html" indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="text()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="/">
        <html>
          <xsl:apply-templates/>
        </html>
      </xsl:template>
    
      <xsl:template match="*">
        <xsl:variable name="vIndent" select="count(ancestor::*) * 3"/>
        <div style="text-indent:{$vIndent}em"><xsl:value-of select="name()"/>: <xsl:apply-templates/></div>
      </xsl:template>
    
      <xsl:template match="document">
        <xsl:apply-templates/>
      </xsl:template>  
    
    </xsl:stylesheet>
    

    Raw HTML Output

    <html>
       <div style="text-indent:3em">metadata: 
          <div style="text-indent:6em">version: 1.0</div>
          <div style="text-indent:6em">date: 1/1/12</div>
       </div>
       <div style="text-indent:3em">trackdata: 
          <div style="text-indent:6em">tracks: 
             <div style="text-indent:9em">track: 123</div>
             <div style="text-indent:9em">track: 456</div>
          </div>
       </div>
    </html>
    

    If you wanted the first div to not have an indent, you could change the variable to this:

    <xsl:variable name="vIndent" select="(count(ancestor::*) * 3) - 3"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologize in advance for this somewhat ignorant question, but I have researched this
I have researched this question for ages and cannot get it right! I have
Greetings, Apologies in advance that I have not researched this toughly enough to answer
I should start by saying I have researched this question. I can't find any
I have been struggling with this NHibernate issue for hours. I extensively researched on
First, I have researched this question a lot. I learned how to read from
I have researched this for a few hours and I am kind of frustrated.
I have researched enough on this topic sans any luck :-( My requirement is
Ok I am still learning this... I have Googled and done some research but
I have researched and haven't found a way to run INTERSECT and MINUS operations

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.