This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Getting a predicate right
- From: "G. Ken Holman" <gkholman at CraneSoftwrights dot com>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Tue, 30 Jul 2002 15:51:38 -0400
- Subject: Re: [xsl] Getting a predicate right
- Reply-to: xsl-list at lists dot mulberrytech dot com
At 2002-07-30 12:25 -0700, Kuhns Matt wrote:
My XML Sheet looks like this:
...
In my XSL I am going through the messages and trying
to match the attribute grp from mesg with the
attribute id from desc.
I think you should be using keys for this ... an example is below. I load
a key table named "colours" with "desc" elements keyed by their "id"
attribute. I then look up the needed "desc" element using the key()
function and determine the value of the "name" attribute attached to the
element found.
something like this:
//test/colors/desc[@grp]/@name
or
//test/colors/desc[@id = @grp]/@name
You are comparing the id attribute of desc with the grp attribute of desc,
which is why you are not getting what you want.
Note also that using "//" is *very* wasteful. Often the performance of
many stylesheets is optimized by finding and eliminating any use of
"//". Many users of XPath don't realize that looking for "//x" will find
all "x" elements and then continue looking inside those elements for more
"x" elements that might be nested. Using "//x" will search *every* "x"
element node down to the leaves of the node tree, which can take a long
time for the tree of a big instance.
I hope this helps.
................. Ken
T:\ftemp>type matt.xml
<test>
<colors>
<desc id="0" name="blue"/>
<desc id="1" name="red"/>
<desc id="2" name="white"/>
</colors>
<mesg grp="1">Hi how are you?</mesg>
<mesg grp="0">I am fine.</mesg>
<mesg grp="2">That is good.</mesg>
</test>
T:\ftemp>type matt.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="colours" match="desc" use="@id"/>
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="/test/mesg">
<p color="{key('colours',@grp)/@name}"><xsl:apply-templates/></p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
T:\ftemp>saxon -o matt.out matt.xml matt.xsl
T:\ftemp>type matt.out
<?xml version="1.0" encoding="utf-8"?>
<p color="red">Hi how are you?</p>
<p color="blue">I am fine.</p>
<p color="white">That is good.</p>
T:\ftemp>
--
Upcoming hands-on in-depth 3-days XSLT/XPath and/or 2-days XSL-FO:
- North America: Sep 30-Oct 4,2002
- Japan: Oct 7-Oct 11,2002
G. Ken Holman mailto:gkholman@CraneSoftwrights.com
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6 Definitive XSLT and XPath
ISBN 1-894049-08-X Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1 Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books (electronic, printed),
articles, training (instructor-live,Internet-live,web/CD,licensed)
Next public training: 2002-08-05,26,27,09-30,10-03,07,10
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list