"A key can be used to select nodes in any document, not just the
principle document. This includes a document constructed as a
temporary tree." - Kay XSLT2.0 3ed, pg 334
I am trying to create a key of the elements in a temporary tree. This
temporary tree is the result of a "first phase" processing that adds a
new attribute to the element.
I would assume that the keys are created before starting the "/" template.
The xsl:key element is not allowed in a template, so that can't force
the re-indexing for the key.
Two questions:
1) Is the xsl:variable "objects-with-new" considered a "temporary tree"?
2) Is it possible to access the "object" elements in $objects-with-new
via a key, where the @use is an attribute that was added after parsing
the input document?
Regards,
Tim Lebo
Input:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<object id="a">
</object>
<object id="b">
</object>
<object id="c">
</object>
</root>
XSLT 2.0 (saxon8b):
1 <?xml version="1.0"?>
2 <xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
3 <xsl:output method="xml" indent="yes"/>
4
5 <xsl:key name="objects-by-id" match="object" use="@id"/>
6 <xsl:key name="objects-by-new" match="object" use="@new"/>
7
8 <xsl:template match="/">
9 <xsl:comment>Successful attempt to access input nodes by
key</xsl:comment>
10 <xsl:copy-of select="key('objects-by-id','b')"/>
11
12 <xsl:variable name="objects-with-new">
13 <xsl:apply-templates select="//object" mode="addnew"/>
14 </xsl:variable>
15
16 <xsl:comment>Objects with added attribute</xsl:comment>
17 <xsl:copy-of select="$objects-with-new"/>
18
19 <xsl:comment>Successful attempt to access by more expensive
xpath expression</xsl:comment>
20 <xsl:copy-of select="$objects-with-new//object[(_at_)new=2]"/>
21
22 <xsl:comment>Failed attempt to access keyed by new
attribute</xsl:comment>
23 <xsl:copy-of select="key('objects-by-new','2')"/>
24 </xsl:template>
25
26 <xsl:template match="object" mode="addnew">
27 <xsl:copy>
28 <xsl:copy-of select="@*"/>
29 <xsl:attribute name="new">
30 <xsl:number/>
31 </xsl:attribute>
32 <xsl:apply-templates/>
33 </xsl:copy>
34 </xsl:template>
35
36 </xsl:transform>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!--Successful attempt to access input nodes by key-->
<object id="b"></object>
<!--Objects with added attribute-->
<object id="a" new="1"></object>
<object id="b" new="2"></object>
<object id="c" new="3"> </object>
<!--Successful attempt to access by more expensive xpath expression-->
<object id="b" new="2"></object>
<!--Failed attempt to access keyed by new attribute-->
THERE IS NO ELEMENT HERE, BUT "<object id="b" new="2"></object>" IS DESIRED
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--