Can someone help me interpret this xsl?

Last post 07-02-2009 2:17 PM by hnugz. 2 replies.

Sort Posts:

  • Can someone help me interpret this xsl?

    07-02-2009, 11:06 AM
    • Member
      4 point Member
    • hnugz
    • Member since 08-20-2008, 10:31 AM
    • Posts 12

    Can someone help me interpret this xsl?

    <xsl:sort select="*[name() = $dvt_sortfield] | @*[name() = $dvt_sortfield] | text()[name(ancestor::*[1]) = $dvt_sortfield]" order="{$dvt_sortdir}" data-type="{$dvt_sorttype}" />
    <xsl:sort select="$dvt_sortfield" order="ascending" />

    The page has a 4 column table and I believe the xml is being generated on the fly based upon a database query.  Three of the columns sort correctly however one of the does not.  When I click on the header, it appears to sort descending the first time, but if I click it again nothing happens.  All the other columns sort between ascending/descending with each click.

    Here is what I know, please let me know if I am incorrect:

    The asterisk says to match any node.  The | is a union character saying to include all non-duplicates.  $dvt_sortfield is a parameter defined at the top of the document, <xsl:param name="dvt_sortfield">Title</xsl:param>.  Order is the sort parameter ascending or descending.

    I can't seem to fully understand the name function, the point of the [], or what text()[name(ancestor::*[1]) means.  Any help is appreciated.

  • Re: Can someone help me interpret this xsl?

    07-02-2009, 12:05 PM
    Answer

     Global parameters of a stylesheet can be set before code runs the transformation, with .NET and XslTransform or XslCompiledTransform you use an XsltArgumentList for that. So that way you can pass in an element name or an attribute name for the parameter dvt_sortfield and the first xsl:sort then sorts on the first element or attribute of that name.

    As for your explanation, * does not match any node, rather only any element node. @* selects any attribute node. The predicate [name() = $dvt_sortfield] then filters out those elements or attribute where the name is the same as the passed in parameter. The | is the union operator. The last expression text()[name(ancestor::*[1]) = $dvt_sortfield] could in my view be written as text()[name(..) = $dvt_sortfield] and tries to sort on the text content of an element having the name in the dvt_sortfield parameter. So all that together simply is an attempt to write one sort expression that can cope with elements, attributes and text nodes.

     

    Why the sorting does not work can't be told from that XSLT snippet, we would need to see how all those parameters are set when you click a column header. And seeing the input XML and how the XSLT generates the table is also necessary to debug that problem.

    Martin Honnen --- MVP XML
    My blog
  • Re: Can someone help me interpret this xsl?

    07-02-2009, 2:17 PM
    • Member
      4 point Member
    • hnugz
    • Member since 08-20-2008, 10:31 AM
    • Posts 12

    That helps, thank you.

    As for the actual bug, I'm hoping to figure that out myself.  If necessary I'll make another post later with details.

Page 1 of 1 (3 items)