Altova Mailing List Archives


Re: Need some help with an expression...

From: David Allouche <david@---------------->
To:
Date: 8/22/2000 7:36:00 PM
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

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.