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

  • Home
  • SEARCH
  • 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 4064312
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:50:23+00:00 2026-05-20T15:50:23+00:00

edited in response to comments * Hello, I am an XSLT noob and need

  • 0
  • edited in response to comments *

Hello,

I am an XSLT noob and need some help. I am trying to do an filter/group combination with XSLT 1.0 (can’t use XSLT 2.0 for this application).

Here is an example of the xml

<entry>
  <item>
    <name>Widget 2</name>
    <rank>2</rank>
    <types>
       <type>Wood</type>
       <type>Fixed</type>
       <type>Old</type>
    </types>
  </item>
   <item>
    <name>Widget 1</name>
    <rank>2</rank>
    <types>
       <type>Metal</type>
       <type>Broken</type>
       <type>Old</type>
    </types>
  </item>
  <item>
    <name>Widget 3</name>
    <rank>1</rank>
    <types>
       <type>Metal</type>
       <type>New</type>
    </types>
  </item>
</entry>

Now what I want to do is output html where I get a subset of the XML based on <type> and then group on rank. For example, if the user selects all items with the type Metal, the output should be:

<p class="nospace"><font color="#800000">
<b>Rank 1</b></font></p>
<li id="mylist"><b>Widget 3</b></li>
<br\>

<p class="nospace"><font color="#800000">
<b>Rank 2</b></font></p>
<li id="mylist"><b>Widget 1</b></li>
<br\>

of if the user user chooses the type Old the output would be

<p class="nospace"><font color="#800000">
<b>Rank 2</b></font></p>
<li id="mylist"><b>Widget 1</b></li>
<li id="mylist"><b>Widget 2</b></li>
<br\>

I can group using keys on rank along easily enough, but trying to do both is not working. Here is a sample of the xslt I have tried:

<xsl:param name="typeParam"/>
<xsl:key name="byRank" use="rank" match="item"/>

<xsl:for-each select="item[count(.|key('byRank',rank)[1])=1]">
  <xsl:sort data-type="number" select="rank"/>
  <xsl:for-each select="key('byRank',rank)">
    <xsl:sort select="name"/>
    <xsl:if test="count(rank)&gt;0">
      <p class="nospace"><font color="#800000"><b>Rank<xsl:value-of select="rank"/></b></font></p>
      <xsl:for-each select="types[types=$typeParam]">
        <li id="mylist"><b><xsl:value-of select="../name"/></b></li>
      </xsl:for-each>
    <br/>
  </xsl:if>
</xsl:for-each>
</xsl:for-each>

The result I get from this is I do indeed get the subset of my xml that I want but it also displays all of the various rank values. I want to limit it to just the ranks of the type that is specified in $typeParam.

I have tried moving the for-each statement to earlier in the code as well as modifying the if statement to select for $typeParam but neither works. I have also tried concat-ing my key with rank and type but that doesn’t seem to work either (It only works if the type in $typeParam is the first child under types).

Thanks

jeff

  • 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-20T15:50:23+00:00Added an answer on May 20, 2026 at 3:50 pm

    This stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:key name="kItemByRank" match="item" use="rank"/>
        <xsl:param name="pType" select="'Old'"/>
        <xsl:template match="entry">
            <xsl:for-each select="item[count(.|key('kItemByRank',rank)[1])=1]">
                <xsl:sort select="rank" data-type="number"/>
                <xsl:variable name="vGroup" select="key('kItemByRank',rank)[
                                                       types/type = $pType
                                                    ]"/>
                <xsl:if test="$vGroup">
                    <p class="nospace">
                        <font color="#800000">
                            <b>
                                <xsl:value-of select="concat('Rank ',rank)"/>
                            </b>
                        </font>
                    </p>
                    <xsl:apply-templates select="$vGroup">
                        <xsl:sort select="name"/>
                    </xsl:apply-templates>
                    <br/>
                </xsl:if>
            </xsl:for-each>
        </xsl:template>
        <xsl:template match="item">
            <li id="mylist">
                <b>
                    <xsl:value-of select="name"/>
                </b>
            </li>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    <p class="nospace">
        <font color="#800000">
            <b>Rank 1</b>
        </font>
    </p>
    <li id="mylist">
        <b>Widget 3</b>
    </li>
    <br />
    <p class="nospace">
        <font color="#800000">
            <b>Rank 2</b>
        </font>
    </p>
    <li id="mylist">
        <b>Widget 1</b>
    </li>
    <br />
    

    And whit pType param set to 'Old', output:

    <p class="nospace">
        <font color="#800000">
            <b>Rank 2</b>
        </font>
    </p>
    <li id="mylist">
        <b>Widget 1</b>
    </li>
    <li id="mylist">
        <b>Widget 2</b>
    </li>
    <br />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDITED I'm trying to use jquery/ajax to display data returned from a django method.
In response to the helpful comments, I have edited the original question (where I
(Edited a lot) I've got some classes with Abstracts Members. The concrete type of
[Edited: After cross-testing on a fresh machine and some additional research, this appears to
Edited to Add * I haven't found a solution for this one yet, can
Edited by OP. My program is in need of a lot of cleanup and
edited: This is what i need: sendpost = function(a,b,c){ return jQuery.post('inc/operations.php', {a:b}, c, json);
I am trying to test some code. The main script requires imports from a
Hello and thank you in advance. I know this is total noob question, and
We are developing online schedule application. One schedule can be edited simultaneously by several

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.