[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

learned a lesson about XPath variable evaluations sort

Subject: learned a lesson about XPath variable evaluations sorting in document order
From: "Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 16 Feb 2023 23:34:01 -0000
 learned a lesson about XPath variable evaluations sort
Hi everyone,

Given the following input:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <data>1</data>
  <group>
    <data>2</data>
    <group>
      <data>3</data>
      <results>
        <!-- put ancestor <data> values here, closest first -->
      </results>
    </group>
  </group>
</root>

I wanted the lowest-level <results> element to contain all the ancestor <data>
values, in order of closest ancestor first. By applying reverse() to the
ancestor results, this gives the desired results of "321":

  <xsl:template match="results">  <!-- summarize <data> -->
    <xsl:variable name="all-data" as="text()*"
select="reverse(ancestor::*/data/text())"/>
    <xsl:copy>
      <xsl:value-of select="$all-data"/>
    </xsl:copy>
  </xsl:template>

But if I move the text() evaluation from the <xsl:variable> to the
<xsl:value-of>, then I get "123" instead:

  <xsl:template match="results">  <!-- summarize <data> -->
    <xsl:variable name="all-data" as="element()*"
select="reverse(ancestor::*/data)"/>
    <xsl:copy>
      <xsl:value-of select="$all-data/text()"/>
    </xsl:copy>
  </xsl:template>

The cause is that the "/" in "$all-data/text()" XPath expression causes the
results to be sorted in document order. Somehow I thought that once the
elements were in the variable, they became disassociated with the source
document. But apparently they're not, which updates my mental model.

If I change "/" to the "!" operator (apply an operation to each item in a
sequence), then I get "321" again because the reversed ancestor order is
preserved:

      <xsl:value-of select="$all-data ! text()"/>

Anyway, just passing this along as a friendly reminder to the other novices on
the list. I spent more time than I'd like to admit figuring this out on my
more complicated stylesheet.


  *   Chris

P.S. MANY thanks to Martin for enabling me to fiddle my way to understanding!

-----
Chris Papademetrious
Tech Writer, Implementation Group
(610) 628-9718 home office
(570) 460-6078 cell

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.