Subject: RE: [xsl] NodeTest expected here - problem with creating xsl:key fromdocument(url) From: "Macaulay,Malcolm (US)" <Malcolm.Macaulay2@xxxxxxxxx> Date: Tue, 27 Aug 2002 08:15:19 -0500 |
Thanks again Chuck, I've had an epiphany wrt keys. I will do some off-line reading on this topic - I've been blissfully using keys without really understanding them. Conclusions: - the index is not (cannot be) created when an <xsl:key../> element is encountered. <xsl:key../> is just the structure/rules of the key - index is created (based on the current context) when select="key(..)" is (first?) encountered in a particular context - the same key can be used to index multiple trees (in different contexts) cheers Malcolm -----Original Message----- From: Chuck White [mailto:chuckwh@xxxxxxxxxxx] Sent: Tuesday, August 27, 2002 1:34 AM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: Re: [xsl] NodeTest expected here - problem with creating xsl:key fromdocument(url) ----- Original Message ----- From: "Macaulay,Malcolm (US)" <Malcolm.Macaulay2@xxxxxxxxx> > BTW, it's not obvious to me why the key should need to be called in the context of the document on which it is based. Is my understanding of xsl:key flawed? I understand <xsl:key name="..." match="..." use="..."/> to mean: "make an in-memory index of the match nodes, keyed by the use value so I can ask for the nodes by key at some later time". Why does the key need to be called in the context of the document it is based on? Hi Malcolm: Rather than look at keys from a standpoint of this document or that document, think in terms of trees, since that's what the XSLT processor does. So what you really want to do is manipulate the trees, and context nodes, rather than 'documents'. The stylesheet below is illustrative: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:key name="keyedLookupTable" match="Value" use="@key"/> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:variable name="global_lookup" select="document('LookupTable.xml')/LookupTable" /> <xsl:template match="/"> <xsl:apply-templates select="/Data/key" /> </xsl:template> <xsl:template match="/Data/key"> <xsl:apply-templates select="$global_lookup"> <xsl:with-param name="pos" select="."/> </xsl:apply-templates> </xsl:template> <xsl:template match="LookupTable"> <xsl:param name="pos" /> <xsl:variable name="lookup" select="key('keyedLookupTable', $pos)" /> <Output> <xsl:for-each select="$lookup"> <data> <xsl:value-of select="."/> </data> </xsl:for-each> </Output> </xsl:template> </xsl:stylesheet> I'm sort of bouncing around the trees in this stylesheet, and even though the original source document doesn't have, in this example, an element named 'LookupTable', that doesn't stop me from writing a template for it, because I know I can still process the template in the Data/key template. My goal is to deal with my context node, and I always have control of that no matter how many documents I bring into the fray. I do this by using an apply-templates statement and a parameter to hook into the key function. I didn't need to write <xsl:template match="document('LookupTable.xml')/LookupTable"> any more than I need to write xsl:key match="document('LookupTable.xml')/LookupTable". The above stylesheet returns this, based on the inputs you provided: <Output> <data>value keyed by a</data> </Output> <Output> <data>value keyed by b</data> </Output> <Output> <data>value keyed by c</data> </Output> Hope that helps a little, and if I'm only confusing the issue for you, we can try again. :-) Cheers, Charles White The Tumeric Partnership http://www.tumeric.net chuck@xxxxxxxxxxx http://www.javertising.com ________________________________________ Author, Mastering XSLT, Sybex Books Co-Author, Mastering XML, Premium Edition, Sybex Books XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Counting Characters and W, David Carlisle | Thread | [xsl] <xsl:apply-templates select=", Michael Rothwell |
Re: [xsl] NodeTest expected here - , Trevor Nash | Date | Re: [xsl] Counting Characters and W, David Carlisle |
Month |