Sei sulla pagina 1di 1

X Scroogle ssl xslt last sibling

Published on XML.com http://www.xml.com/pub/a/2000/10/04/transforming/trxml5.html


See this if you're having trouble printing code examples

Finding Relatives
By Bob DuCharme
October 04, 2000

When your XSLT processor is processing one node of your source tree, and you want to
get something from another node, you can use an XPath expression and an xsl:value-of
instruction. I'm going to hurry through the general structure of an XPath expression to get
to the really useful stuff: using XPath abbreviations to get at the different "relatives" a
node can have on a source tree -- siblings, a parent, grandparents and so on. This brief
review of the full XPath syntax will put the abbreviations in context.

An XPath expression consists of one or more location steps separated by slashes. Each
location step consists of an axis specifier, a node test, and an optional predicate. You don't
have to include an axis specifier, either; if you leave it out, a default of child is assumed.
This makes a node test name like "product" a perfectly valid one-step XPath location step --
in fact, it's a perfectly valid XPath expression. When you include an axis specifier, it's
separated from the node test with two colons, and a predicate goes inside of square
brackets, so a location step of preceding-sibling::item means "of all the nodes in the
preceding-sibling axis, I want the ones named 'item'." A location step that includes a
predicate, like preceding-sibling::item[1], means "of all the nodes in the preceding-sibling
axis, I want the first one named 'item'."

XPath offers abbreviations that let you use much of its power without spelling out full
location path steps every time you want to use them. For example, @ means the same thing
as attribute:: and .. means the same thing as parent::node(). Let's see how useful these
abbreviations can be and how much more flexibility you have when you use axes that have
no abbreviations.

Parent, Grandparent, Sibling and other Relative Elements: Getting


their Content and Attributes

In the following, the list element is a child of the prices element, which is a child of the wine
element. We're going to see how a template rule that processes that list element can
access many other nodes of an XSLT source tree holding this document.

<wine grape="Chardonnay">
<winery>Lindeman's</winery>
<product>Bin 65</product>
<year>1998</year>
<desc>Youthful, with a cascade of spicy fig.</desc>
<prices>
<list>6.99</list>
<discounted>5.99</discounted>
<case>71.50</case>
</prices>
</wine>

Potrebbero piacerti anche