xsl-list
[Top] [All Lists]

Re: Speeding up processing (with sablotron or saxon)

2004-07-13 08:33:19
Hi Tony,

At 10:57 AM 7/13/2004, you wrote:

So I'd replace the:-
<xsl:for-each
select=".//resource[not(@swgcraft_id=preceding::*/@swgcraft_id)]">

with

<xsl:key name="resource-by-id" match="resource" use="@swgcraft_id"/>
<xsl:variable name="resources" select="//resource"/>
<xsl:variable name="unique-resources"
     select="$resources[not(count(.|key('resources-by-id',@swgcraft_id)[1])
= 1)]"/>

but I guess I still need some form of for-each statement too?

Yeah, sorry I didn't wrap it up completely -- now you can have

<xsl:for-each select="$unique-resources">...</xsl:for-each>

...but it will be much more efficient, since the set has already been collected -- the processor doesn't now have to look at every resource in the document, checking all of their preceding siblings, to establish which are unique.

This finding-the-unique-one business ("deduplicating") is one of the relatively few common tasks -- maybe the most important -- for which XPath 1.0 has no direct provision, requiring us XSLT 1.0 hackers to master a couple of tricky idioms.

Another way to do it you may have just seen in another thread. Instead of

<xsl:variable name="unique-resources"
     select="$resources[not(count(.|key('resources-by-id',@swgcraft_id)[1])
= 1)]"/>

(Hey! isn't there a glitch here! isn't that 'not' erroneous? Oops ... I think I should have offered $resources[count(.|key('resources-by-id',@swgcraft_id)[1]) = 1]) ...)

you can have

<xsl:variable name="unique-resources"
select="$resources[generate-id() = generate-id(key('resources-by-id',@swgcraft_id)[1])]"/>

which does the same thing, but in a different way ... and because of the way a string function is defined to work over a node set, can be obscured further to simply

$resources[generate-id() = generate-id(key('resources-by-id',@swgcraft_id))]

Cheers,
Wendell


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================