[xsl] Re: keys and data files

Subject: [xsl] Re: keys and data files
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Fri, 27 Jul 2001 12:53:03 -0700 (PDT)
Richard Emberson wrote:

> I have an input xml to be processed, a xsl file with will be used to
> transform the
> input file, and a xml data file that hold records that map from one string to
> another:
> 
> input file:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <foo>
>     <bar n="one"/>
>     <bar n="three"/>
>     <bar n="four"/>
> </foo>

> xsl file:
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet  ....>
> 
> 
> <xsl:key name="nv" match="test:X/test:nv" use="@name"/>
> 
> <xsl:template match="foo">
>     <xsl:for-each select="child::bar">
>         <xsl:variable name="name" select="@n"/>
>         <xsl:for-each select="document('data.xml')">
>             <xsl:variable name="node" select="key('nv', $name)"/>
>             <OUT2 name="{$name}" value="{$node/@value}"/>
>         </xsl:for-each>
>     </xsl:for-each>
> </xsl:template>

> </xsl:stylesheet>
> 
> data file:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <test:X
>     xmlns:test="http://foo.bar";;
>     >
>     <test:nv name="one" value="1"/>
>     <test:nv name="two" value="2"/>
>     <test:nv name="three" value="3"/>
>     <test:nv name="four" value="4"/>
> </test:X>
> 
> Now this actually works, the output is:
> 
> <OUT2 value="1" name="one"/><OUT2 value="3" name="three"/><OUT2 value="4"
> name="four"/>
> 
> My question is I was under the (mis) impression that a key was generated
> where it
> was declared, not where it is used. But this is clearly wrong in this case
> since the
> document applied to the key is specified after it is declared (infact the
> document name,
> in this case 'data.xml', could actually have been dynamically generated).
> 
> So, how efficient is a key, especially in this case. Is it implementation
> dependent;
> associated with each document is zero or more keys which (being basically a
> static hashtable)
> is generated the first time it is needed and after that is simply re-used?

>From Mike's book -- page 225:
"In practice, however, most implementations are likely to buid an index, so there
will be a one-time cost in building the index (for each document)..."

> 
> BTW I am amazed that the above xsl code works; xsl is truly a freaky
> language.

Shall I feel guilty for posting the original variant of the "lookup" stylesheet?
:o))

After spending some time with this list I believe that in case I were writing
"nicer" code, then this list would not be the precise place for me.

Also, I'm repeating myself for the third time today -- a good XSLT Debugger (e.g.
the Xselerator) is the perfect tool to study the dynamic effects of xsl code.

Having such a tool makes a big difference! 


Cheers,
Dimitre Novatchev.




__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread