This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Matching Question
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: Michael Zehrer <michael at ftp dot optimal-systems dot de>
- Cc: xsl-list at lists dot mulberrytech dot com
- Date: Mon, 10 Jun 2002 09:01:53 +0100
- Subject: Re: [xsl] Matching Question
- Organization: Jeni Tennison Consulting Ltd
- References: <E17HJe2-0007CB-00@ftp.optimal-systems.de>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hi Michael,
> I have a matching problem, probably somone can explain this to me.
> Given this XML:
>
> <parent>
> <child>foo bla</child>
> <onemorechild>42</onemorechild>
> </parent>
> <parent>
> ...
> </parent>
>
> I want to match for all parent/onemorechild that have a specific
> value, say 42, but then get the content of parent/child and this
> match should fit in one <xsl:value-of />, is this possible?
If the "match" should fit in a xsl:value-of, then it's not a "match"
(pattern), it's a "select" (expression). You want to *select* the
child element of the parent element whose onemorechild child element
has the value '42'.
To get the parent element whose onemorechild child has the value '42',
use a predicate:
parent[onemorechild = '42']
then step down from that parent element to the child child element:
parent[onemorechild = '42']/child
In an xsl:value-of:
<xsl:value-of select="parent[onemorechild = '42']/child" />
There are other ways as well, for example:
parent/onemorechild[. = '42']/preceding-sibling::child
parent/onemorechild[. = '42']/../child
parent/child[../onemorechild = '42']
but the above is probably simplest and visits least nodes.
If you *know* that there is only one parent element whose onemorechild
child element has the value '42', then you should probably use:
parent[onemorechild = '42'][1]/child
to help the processor optimise (it means that it can stop when it
finds the suitable parent element, rather than going through all the
rest of them).
If you're picking out the child elements based on their sibling
onemorechild elements' values a lot within the same stylesheet, then
it might be worth constructing a key to index the child elements
directly:
<xsl:key name="children" match="child" use="../onemorechild" />
and then just using:
key('children', '42')
for example.
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list