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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:15:13+00:00 2026-05-14T03:15:13+00:00

How do I apply a parameter to a select and order attribute in a

  • 0

How do I apply a parameter to a select and order attribute in a xsl:sort element? I’ld like to do this dynamic with PHP with something like this:

$xsl = new XSLTProcessor();
$xslDoc = new DOMDocument(); 
$xslDoc->load( $this->_xslFilePath );
$xsl->importStyleSheet( $xslDoc );
$xsl->setParameter( '', 'sortBy', 'viewCount' );
$xsl->setParameter( '', 'order', 'descending' );

But I’ld first have to now how to get this to work. I tried the following, but it gives me a ‘compilation error’ : ‘invalid value $order for order’. $sortBy doesn’t seem to do anything either:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="sortBy" select="viewCount"/>
<xsl:param name="order" select="descending"/>
<xsl:template match="/">
    <media>
    <xsl:for-each select="media/medium">
    <xsl:sort select="$sortBy" order="$order"/>
        // <someoutput>
    </xsl:for-each>
    </media>
</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-05-14T03:15:13+00:00Added an answer on May 14, 2026 at 3:15 am

    You are close to the correct solution, but there are a few issues:

    1. <xsl:param name="sortBy" select="viewCount"/>
      This defines the $sortBy parameter as the value of the viewCount child of the current node (the document node). Because the top element is not named viewCount, the $sortBy parameter so defined has no value at all.

    2. <xsl:param name="order" select="descending"/>
      Ditto.

    3. <xsl:sort select="$sortBy" order="$order"/>
      Even if issues 1. and 2. above are fixed, this xslt instruction is still problematic. It specifies the value of the order attribute as the literal string '$order' — not as the value of the parameter $order. The way to do this in XSLT is by using AVT (Attribute Value Template). Whenever we want a to specify that within an attribute value we want a particular string to be evaluated as an XPath expression, then this string must be surrounded by curly braces.

    So, the order attribute should be specified as: order = '{$order}'.

    Unfortunately, AVTs cannot be used for the select attribute (another rule from the XSLT spec).

    The way to specify the value of the select attribute is a little-bit more tricky:

    select='*[name()=$sortBy]' This says: sort by the child element, whose name is the same as the value of the variable $sortBy.

    To put all this together, here is the corrected transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="sortBy" select="'viewCount'"/>
     <xsl:param name="order" select="'descending'"/>
    
     <xsl:template match="/">
        <media>
          <xsl:for-each select="media/medium">
            <xsl:sort select="*[name()=$sortBy]" order="{$order}"/>
    
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </media>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the following XML document:

    <media>
     <medium>
       <viewCount>2</viewCount>
     </medium>
     <medium>
       <viewCount>1</viewCount>
     </medium>
     <medium>
       <viewCount>5</viewCount>
     </medium>
    </media>
    

    The correct result is produced:

    <media>
       <medium>
          <viewCount>5</viewCount>
       </medium>
       <medium>
          <viewCount>2</viewCount>
       </medium>
       <medium>
          <viewCount>1</viewCount>
       </medium>
    </media>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 443k
  • Answers 443k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There isn't a good way to sanity check it. The… May 15, 2026 at 6:16 pm
  • Editorial Team
    Editorial Team added an answer There's really no need for a workaround, it's as simple… May 15, 2026 at 6:16 pm
  • Editorial Team
    Editorial Team added an answer You cannot pass a value by reference in JS. You… May 15, 2026 at 6:16 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.