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]

Re: keys: repeated nodes from same key value



"Pawson, David" <DPawson@rnib.org.uk> wrote:
>
> One more on keys().
>
> Given two xml documents a.xml b.xml
> How to create a key on b.xml
> then iterate over an element in a.xml
>  [...]


I recently struggled with this problem myself...

The key() function returns "a node-set containing the nodes *in
the same document as the context node* that have a value for the
named key" [XSLT, 12.2].  Keys are defined without reference to
any particular document; the <xsl:key> specification applies
separately to each document that's loaded.  So the trick is
to select a node in 'b.xml', call the 'key()' function, then
go back to the original node.

In my situation, I had an index file that looked something like

    <index>
	<entry name="foo" href="asdf.html#section1"/>
	<entry name="bar" href="qwerty.html#section238"/>
	...
    </index>

To look up a key in the index, something like this worked:
(untested...)

    <xsl:key name="index" match="entry" use="@name" />
    <xsl:variable name="theIndex" select="document('index.xml')" />

    <xsl:template match="reference">
	<xsl:variable name="refname" select="@refname"/>
	<xsl:variable name="sourceNode" select="."/>
	<xsl:for-each select="$theIndex">
	    <xsl:variable name="href" select="key('index', $refname)"/>
	    <xsl:for-each select="$sourceNode">
		<A href="{href}">
		    <xsl:apply-templates/>
		</A>
	    </xsl:for-each>
	</xsl:for-each>
    </xsl:template>


--Joe English

  jenglish@flightlab.com


 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]