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: problem selecting node sets


Paul,

> I have XML like
>
> <agenda>
>     <activity type="flight" time="09">
>     <activity type="meeting" time="11">
>     <activity type="hotel" time="15"/>
>     <activity type="flight" time="18"/>
> </agenda>
>
> and I would like to treat the <activity> elements that have a value of
> 'hotel' for the type attribute differently that the others.

You want to treat the elements activity[@type = 'hotel'] differently.

> I want to grab the value of the 'time' attribute on the next
> <activity> with either 'meeting' or 'flight' for the value of the
> 'type' attribute, but skip any <activity> elements that have a value
> of 'hotel' for the type attribute.

The next activities are:

  following-sibling::activity

Testing whether an element's 'type' attribute is either 'meeting' or
'flight' can be done with the test:

  @type = 'meeting' or @type = 'flight'

So to get any following activities that are of @type 'meeting' or
'flight', you use:

  following-sibling::activity[@type = 'meeting' or @type = 'flight']

To get the first of these, you use the predicate [1]:

  following-sibling::activity[@type = 'meeting' or
                              @type = 'flight'][1]

And to get the 'time' attribute of those, you use the step /@time:

  following-sibling::activity[@type = 'meeting' or
                              @type = 'flight'][1]/@time

> I believe that an appropriately defined <xsl:key> is the answer but I'm
> having trouble with the specifics.

You don't really need to use a key unless you have a large number of
widely-dispersed nodes.  Where you're only looking for local elements,
like here, it's usually a lot easier to use a basic XPath expression.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.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]