Java Reference
In-Depth Information
nodes from the entire tree. For example, //ingredient selects all ingredient
nodes in the tree.
Aswithfilesystemsthatletyouidentifythecurrentdirectorywithasingleperiod( . )
anditsparentdirectorywithadoubleperiod( .. ),youcanspecifyasingleperiodtorep-
resentthecurrentnodeandadoubleperiodtorepresenttheparentofthecurrentnode.
(YouwouldtypicallyuseasingleperiodinXSLTtoindicatethatyouwanttoaccessthe
value of the currently matched element.)
ItmightbenecessarytonarrowtheselectionofnodesreturnedbyanXPathexpres-
sion.Forexample, /recipe/ingredients/ingredient returnsall ingredi-
ent nodes,butperhapsyouonlywanttoreturnthefirst ingredient node.Youcan
narrow the selection by including predicates in the location path.
A predicate is a square bracket-delimited Boolean expression that is tested against
each selected node. If the expression evaluates to true, that node is included in the set
of nodes returned by the XPath expression; otherwise, the node is not included in the
set.Forexample, /recipe/ingredients/ingredient[1] selectsthefirst in-
gredient element that is a child of the ingredients element.
Predicatescanincludepredefinedfunctions(e.g., last() and position() ),op-
erators(e.g., - , < ,and = ),andotheritems.Forexample, /recipe/ingredients/
ingredient[last()] selectsthelast ingredient elementthatisachildofthe
ingredients element, /recipe/ingredients/ingredient[last()-1]
selectsthenext-to-last ingredient elementthatisachildofthe ingredients ele-
ment, /recipe/ingredients/ingredient[position()<3] selectsthefirst
two ingredient elementsthatarechildrenofthe ingredients element, //in-
gredient[@qty] selectsall ingredient elements(nomatterwheretheyareloc-
ated) that have qty attributes, and //ingredient[@qty='1'] or //ingredi-
ent[@qty="1"] selects all ingredient elements (no matter where they are loc-
ated) that have qty attributes with value 1 .
Although predicates are supposed to be Boolean expressions, the predicate might
not evaluate to a Boolean value. For example, it could evaluate to a number or a
string—XPath supports Boolean, number (IEEE 754 double precision floating-point
values),andstringexpressiontypesaswellasalocationpathexpression'snodesettype.
Ifapredicateevaluatestoanumber,XPathconvertsthatnumbertotruewhenitequals
thecontextnode'sposition;otherwise,XPathconvertsthatnumbertofalse.Ifapredic-
ateevaluatestoastring,XPathconvertsthatstringtotruewhenthestringisn'tempty;
otherwise,XPathconvertsthatstringtofalse.Finally,ifapredicateevaluatestoanode-
Search WWH ::




Custom Search