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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:59:28+00:00 2026-06-05T11:59:28+00:00

I have to take first 4 comments ordered descending from all comments in this

  • 0

I have to take first 4 comments ordered descending from all comments in this xml, (ordered by date_added):

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="get7Comments.xsl"?>

<products>
<product id="1">
    <comment id="1">
        <username>admin1</username>
        <text>nice</text>
        <date_added>20.06.2005</date_added>
    </comment>
    <comment id="2">
        <username>admin2</username>
        <text>too nice</text>
        <date_added>11.05.2005</date_added>
    </comment>
</product>
<product id="2">
    <comment id="1">
        <username>admin1</username>
        <text>comment1</text>
        <date_added>19.05.2005</date_added>
    </comment>
    <comment id="2">
        <username>daniel</username>
        <text>comment2</text>
        <date_added>06.05.2005</date_added>
    </comment>
    <comment id="3">
        <username>another</username>
        <text>comment3</text>
        <date_added>15.05.2005</date_added>
    </comment>
</product>
</products>

Example of output for the last 4 comments that I want:

admin1 : nice : 20.06.2005
admin1 : comment1  : 19.05.2005
another : comment3 : 15.05.2005
admin2 : too nice : 11.05.2005

Works perfectly if I let them as a list of items -comments-, but not after I separate them under a new tag

<product id=""> comments </product> 

I can’t sort them all and take first 4. How it works if I dont’t have the ‘product’ tag, I mean all coments have as the parent ‘products’ tag :

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match ="products">
<xsl:for-each select="product[position() &lt; 8]">

    <xsl:sort select="normalize-space(substring(date_added,7,4))" order="descending" />
    <xsl:sort select="normalize-space(substring(date_added,4,2))" order="descending" />
    <xsl:sort select="normalize-space(substring(date_added,0,2))" order="descending" />
    <xsl:variable name="commID" select="@id" />

    <a href="index.php?p=comment&amp;id={$commID}">
        <xsl:value-of select="substring(text,0,60)" />
    </a><br/>

</xsl:for-each>

 </xsl:template>
 </xsl:stylesheet>
  • 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-05T11:59:30+00:00Added an answer on June 5, 2026 at 11:59 am

    Not sure what you’re trying to do exactly, but try using an xsl:apply-templates instead of an xsl:for-each.

    Example:

    XSLT 1.0

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" encoding="utf-8"/>
    
        <xsl:template match ="products">
            <xsl:apply-templates select="product/comment">
                <xsl:sort select="concat(
                    normalize-space(substring(date_added,7,4)),
                    normalize-space(substring(date_added,4,2)),
                    normalize-space(substring(date_added,1,2))
                    )" data-type="number" order="descending" />
            </xsl:apply-templates>      
        </xsl:template>
    
        <xsl:template match="comment">
            <!--xsl:if used because it wasn't working on the predicate?-->
            <xsl:if test="4 >= position()">
                <a href="index.php?p=comment&amp;id={@id}">
                    <xsl:value-of select="substring(text,1,60)" />
                </a><br/>
                <xsl:text>&#xA;</xsl:text>                  
            </xsl:if>
        </xsl:template>     
    
    </xsl:stylesheet>
    

    using your XML input produces:

    <a href="index.php?p=comment&amp;id=1">nice</a><br>
    <a href="index.php?p=comment&amp;id=1">comment1</a><br>
    <a href="index.php?p=comment&amp;id=3">comment3</a><br>
    <a href="index.php?p=comment&amp;id=2">too nice</a><br>
    

    Your href’s will need modifying if you have duplicate comment id’s across products (like you have in your example XML).

    (Edit: Missed that you’re trying to return the top 4 comments. Fixed. Also fixed typo in 3rd substring().)

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

Sidebar

Related Questions

This is my first post here. I have a problem. I need to take
I have this function. And its goal is to take the last character first
I have legacy MSVS solution with C++ projects. When I take it from Version
I have a web application where the first request may take a few seconds
I have to take 2 values from DateTimePicker and then compare their values with
I have a requirement to take client side XAML (from Silverlight) and create a
First of all let me apologize for the wall of code. Basically, I have
Okay, I've almost got this thing finished! Many thanks to all who have helped
I am struggling with a problem in assembly, where I have to take the
Is there any way to have to tabcontrol take the size of the largest

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.