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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:43:03+00:00 2026-05-26T15:43:03+00:00

I have an XML containing blog posts. Each post contains a date node. I

  • 0

I have an XML containing blog posts. Each post contains a date node.

I want to extract the list of years in which posts have been made and print them out in a fancy format. The, for each year I want to print out the list of active months in a very similar fashion.

The outer part is working fine for me. Since I couldn’t find a way to achieve this using raw XSLT-1.0, I implemented it using EXSLT. I iterate over the posts, extract the year, put it in a new year node and store this as a temporary XML tree in a variable. Then I use EXSLT to create a nodeset from this variable, iterate over it and remove duplicates.

However, as soon as I call an inner template for each year, even if I copy and paste the variable construction code from the first template and dump it, it does not generate any output. It just refuses to iterate over the same nodeset again.

I’m hitting a wall here and I definitely can’t wrap my head around what exactly is happening.

This is as much as I’ve managed to simplify the stylesheet::

<xsl:param name="myparam" select="''" />

<xsl:template match="/">
    <ul>
        <xsl:call-template name="outer" />
    </ul>
</xsl:template>

<xsl:template name="outer">
    <xsl:variable name="years_tree">
        <xsl:for-each select="//post">
            <xsl:sort select="date" />
            <xsl:element name="year"><xsl:value-of select="substring(date, 1, 4)" /></xsl:element>
        </xsl:for-each>
    </xsl:variable>

    <xsl:comment>
        <xsl:copy-of select="$years_tree" />
    </xsl:comment>

    <xsl:for-each select="exsl:node-set($years_tree)/year[not(.=following::year)]">
        <li>
            <xsl:value-of select="." />
            <xsl:if test="starts-with($myparam, string(.))">
                <ul>
                    <xsl:call-template name="inner" />
                </ul>
            </xsl:if>
        </li>
    </xsl:for-each>
</xsl:template>

<xsl:template name="inner">
    <xsl:variable name="years_tree">
        <xsl:for-each select="//post">
            <xsl:sort select="date" />
            <xsl:element name="year"><xsl:value-of select="substring(date, 1, 4)" /></xsl:element>
        </xsl:for-each>
    </xsl:variable>

    <xsl:copy-of select="$years_tree" />
</xsl:template>

</xsl:stylesheet>

This is my sample input::

<?xml version="1.0" encoding="UTF-8"?>
<blog>
    <post>
        <date>2011-10-22T22:50:26</date>
    </post>
    <post>
        <date>2011-10-02T17:25:14</date>
    </post>
    <post>
        <date>2011-10-14T11:58:58</date>
    </post>
    <post>
        <date>2011-11-21T11:58:58</date>
    </post>
    <post>
        <date>2010-10-14T11:58:58</date>
    </post>
    <post>
        <date>2011-09-14T11:58:58</date>
    </post>
</blog>

and this is the output I’m getting::

<?xml version="1.0"?>
<ul>
  <debug>
    <year>2010</year>
    <year>2011</year>
    <year>2011</year>
    <year>2011</year>
    <year>2011</year>
    <year>2011</year>
  </debug>
  <li>2010<ul><debug/></ul></li>
  <li>2011<ul><debug/></ul></li>
</ul>
  • 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-26T15:43:04+00:00Added an answer on May 26, 2026 at 3:43 pm

    Well the path / selects the document node of the current context node and in your case you are working with two documents, the primary input document and the temporary document you create. When you call the other template the context node is a year element in the temporary document, thus your attempt with //post tries to find post elements in the temporary document and you don’t have any in that document. So you need to make sure you store a global <xsl:variable name="main-doc" select="/"/> and then when you are looking for post elements in the primary input document you can use $main-doc//post.

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

Sidebar

Related Questions

I have an assembly containing very thorough XML-based documentation, which is used through Sandcastle
I have an xml file containing a lists of nodes with a "date" attribute,
I have a fairly large ivy.xml containing a number of configurations which are the
I have an XML file containing one (or more) key/value pairs. For each of
I have problem in parsing XML containing gsm packets through my .Net application.I want
I have an XML document with a bunch of elements, each containing a timestamp
I have an XML file containing some scientific text that I want to display
If I have some xml containing things like the following mediawiki markup: ...collected in
Here's my problem.I have 2 xmlfiles with identical structure, with the second xml containing
I have a string containing XML document using LinqtoXML What is the best way

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.