This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: NodeTest expected here - problem with creating xsl:key fromdocument(url)


----- Original Message -----
From: "Macaulay,Malcolm (US)" <Malcolm.Macaulay2@cnare.com>
> 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@tumeric.net
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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]