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: Need some help with an expression...


On Tue, 22 Aug 2000, Charles Douthart wrote:

> <box>
>    <category name="someType">
>       <header>
>         <self>
>            <host>myhost</host>
>            <instance>9</instance>
>         </self>
>        <ref>
>           <host>thathost</host>
>           <instance>1010101</instance>
>        </ref>
> </header>
> 
> And the value of the header instance is 9 (passed from a web page to a
> servlet)
> What expression can I use to get the ref elemenent under the same header
> parent?

/box/category[@name='someType']/header is a nice start.

  Just go on you XPath until you test what you want to test:

/box/category[@name='someType']/header/self[instance=$parameter]

  Now use the parent axis to go up to the header element

/box/category[@name='someType']/header/self[instance=$parameter]/parent:header

  Then go on as usual using the implied child axis

/box/category[@name='someType']/header/self[instance=$parameter]/parent:header/ref

  It's done, but it needs to be shortened a bit to stay readable.
  First, as you know that the parent of the self element is a header
element you can use a wildcard element name without changing the meaning
of the XPath:

/box/category[@name='someType']/header/self[instance=$parameter]/parent:*/ref

  Actually using parent axis with an explicit element name is only useful
to test that a parent has a given name...
  Then, if your file structure is regular enough you can put in more
wildcards without change of signification

/*/category[@name='someType']/*/self[instance=$parameter]/parent:*/ref

  I guess you know enough of XSLT to use the context to get rid of any
unnecessary steps at the beginning of the XPath.

  But there is still other approaches, making heavier use of predicates,
and possibly less performant.  But it's mainly a matter of programming
style.

//category[@name='someType']/*/ref[parent::*/self[instance=$parameter]]

//ref[ancestor::category[@name='someType'] and
                                  parent::*/self[instance=$parameter]]

Or even the most perverse and obfuscated:

ancestor-or-self::node()[boolean(count(.|/)-2)]/node()[ancestor::*
[not(descendant-or-self::*/parent::category)]/child::self[instance=
$parameter]][self::ref][generate-id(self::ref)=generate-id(//ref
[ancestor::category[@name='someType'])]

(if someone can find a bug in this one I'll buy him/her a beer) :-) 


 Hope this helps. :-)

                             -- David --


 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]