[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Matching Question

Subject: Re: Matching Question
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 10 Jun 2002 09:01:53 +0100
matching question
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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.