Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Variables in <xsl:sort select=""/>

Tags:

xml

xslt

xpath

Hi am build a generic template to list my content. But the Content may be sorted on different @'s or node()'s. So want to pass the xPath in.

<xsl:variable name="sort" select="@sortBy"/>
<xsl:variable name="order" select="@order"/>

<xsl:for-each select="Content[@type=$contentType]">
  <xsl:sort select="$sort" order="{$order}" data-type="text"/>
  <xsl:sort select="@update" order="{$order}" data-type="text"/>
    <xsl:copy-of select="."/>
</xsl:for-each>

Using a variable to drop in ascending or descending into the order="" WORKS.

Why cannot do this on the select="" ?

I hoping to make this super dynamic the select variable can be xPtah either @publish or Title/node() or any xPath.

There is no error - It just ignores the sort.

like image 317
Will Hancock Avatar asked Feb 24 '10 19:02

Will Hancock


People also ask

How do you set a variable value in XSLT?

Variables in XSLT are not really variables, as their values cannot be changed. They resemble constants from conventional programming languages. The only way in which a variable can be changed is by declaring it inside a for-each loop, in which case its value will be updated for every iteration.

How do you call a variable in XSLT?

XSLT <xsl:variable> The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!

What is select value xsl?

The XSLT <xsl:value-of> element is used to extract the value of selected node. It puts the value of selected node as per XPath expression, as text.

What does xsl sort do?

XSLT's xsl:sort instruction lets you sort a group of similar elements. Attributes for this element let you add details about how you want the sort done -- for example, you can sort using alphabetic or numeric ordering, sort on multiple keys, and reverse the sort order.


1 Answers

This is by design. The select attribute is the only one which doesnt accept AVTs (Attribute - Value Templates).

The usual solution is to define a variable with the name of the child element that should be used as sort key. Below is a small example:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vsortKey" select="'b'"/>
 <xsl:variable name="vsortOrder" select="'descending'"/>

 <xsl:template match="/*">
   <xsl:for-each select="*">
    <xsl:sort select="*[name() = $vsortKey]" order="{$vsortOrder}"/>

    <xsl:copy-of select="."/>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<t>
  <a>
   <b>2</b>
   <c>4</c>
  </a>
  <a>
   <b>5</b>
   <c>6</c>
  </a>
  <a>
   <b>1</b>
   <c>7</c>
  </a>
</t>

the wanted result is produced:

<a>
   <b>5</b>
   <c>6</c>
  </a>
<a>
   <b>2</b>
   <c>4</c>
  </a>
<a>
   <b>1</b>
   <c>7</c>
</a>
like image 118
Dimitre Novatchev Avatar answered Sep 30 '22 04:09

Dimitre Novatchev