Re: [xsl] Template for node-set parents

Subject: Re: [xsl] Template for node-set parents
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxx>
Date: Fri, 2 Nov 2012 09:15:37 -0400
Hi,

In the toy problem offered, an XSLT 1.0 solution would simply perform
the filtering for the first item in the sorted list inside the
variable declaration:

That is, instead of

<xsl:variable name="sorted">
   <xsl:apply-templates select="/response/data/result">
     <xsl:sort select="COMPANY_SORT" data-type="text" order="descending"/>
   </xsl:apply-templates>
 </xsl:variable>

we would have

<xsl:variable name="first-sorted">
   <xsl:for-each select="/response/data/result">
     <xsl:sort select="COMPANY_SORT" data-type="text" order="descending"/>
     <xsl:if test="position() = 1">
        <xsl:apply-templates select="."/>
     </xsl:if>
   </xsl:apply-templates>
 </xsl:variable>

But I bet there's something in the real-world case to prevent this (so
we may need to see more).

I agree with Mike that solving such problems in XSLT 1.0 seems
increasingly like a parlor game. Not that I'm against parlor games.

Especially in problems like this one ... processing a sorted list is
usually possible in XSLT 1.0 (harder or easier depending on the sort),
but then you discover you actually have to sort a processed list --
even harder.

Cheers,
Wendell

On Thu, Nov 1, 2012 at 5:14 PM, Darren Oh <darren@xxxxxxx> wrote:
> Thanks for the suggestion. I got stuck when trying to produce a sorted
node-set. Instead of a node-set, I got a result tree fragment, for which no
node-set operations are possible. Here is a simplified example to illustrate
the problem. Whereas it is possible to select a node from $result, it is not
possible to select a node from $sorted. Any ideas?
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
>   <xsl:output method="xml"/>
>   <xsl:template match="*">
>     <xsl:copy>
>       <xsl:apply-templates/>
>     </xsl:copy>
>   </xsl:template>
>   <xsl:variable name="result" select="/response/data/result"/>
>   <xsl:variable name="sorted">
>     <xsl:apply-templates select="/response/data/result">
>       <xsl:sort select="COMPANY_SORT" data-type="text" order="descending"/>
>     </xsl:apply-templates>
>   </xsl:variable>
>   <xsl:template match="/">
>     <xsl:copy-of select="$result[1]"/>
>     <xsl:copy-of select="$sorted[1]"/>
>   </xsl:template>
> </xsl:stylesheet>
>
> On Oct 19, 2012, at 1:27 PM, Michael Kay wrote:
>
>> Try:
>>
>> 1. define a global variable $v1 that selects the result of the path
expression in document order.
>>
>> 2. define another global variable $v2 that selects the sorted result of the
path expression
>>
>> 3. Use a base template rule that's the identity copy
>>
>> 4. Add a template rule that matches nodes in $v1 (match="node()[. intersect
$v1]). In this rule, determine the index position of this node in $v1 (count
($v1[. << $this]) + 1), and output the corresponding node from $v2 (copy-of
select="$v2[$n]").
>>
>> Michael Kay
>> Saxonica
>>
>>
>> On 19/10/2012 18:03, Darren Oh wrote:
>>> I am trying to generate a stylesheet that copies an XML source document.
The only change should be that nodes selected by an XPath expression are
sorted. I want this to work for any XML source document. The only information
available to generate the stylesheet is the XPath expression and the sort
criteria. I think this requires creating a template for the parents of the
nodes selected by the XPath expression. How can I do this?
>



--
Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables
_____oo_________o_o___ooooo____ooooooo_^

Current Thread