Java Reference
In-Depth Information
pressionstartswith
/
,theexpressionrepresentsanabsolutepaththatstartsfromtheroot
Attributes are also handled by location path expressions. To select an element's at-
tribute,specify
@
followedbytheattribute'sname.Forexample,
@qty
selectsthe
qty
attribute node of the context node.
In most cases, you will work with root nodes, element nodes, and attribute nodes.
However,youmightalsoneedtoworkwithnamespacenodes,textnodes,processing-
instruction nodes, and comment nodes. Unlike namespace nodes, which are typically
handled by XSLT, you'll more likely need to process comments, text, and processing
instructions. XPath provides
comment()
,
text()
, and
processing-instruc-
tion()
functions for selecting comment, text, and processing-instruction nodes.
The
comment()
and
text()
functionsdon'trequireargumentsbecausecomment
andtextnodesdon'thavenames.Eachcommentisaseparatecommentnode,andeach
textnodespecifiesthelongestrunoftextnotinterruptedbyatag.The
processing-
instruction()
functionmaybecalledwithanargumentthatidentifiesthetargetof
theprocessinginstruction.Ifcalledwithnoargument,allthecontextnode'sprocessing-
instruction child nodes are selected.
XPathsuppliesthreewildcardsforselectingunknownnodes.The
*
wildcardmatches
anyelementnoderegardlessofthenode'stype.Itdoesnotmatchattributes,textnodes,
comments,orprocessing-instructionnodes.Whenyouplaceanamespaceprefixbefore
the*,onlyelementsbelongingtothatnamespacearematched.The
node()
wildcardis
a function that matches all nodes. Finally, the
@*
wildcard matches all attribute nodes.
Note
XPath lets youperform multiple selections byusing the vertical bar (
|
).For
example,
author/*|publisher/*
selects the children of
author
and the chil-
drenof
publisher
,and
*|@*
matchesallelementsandattributes,butdoesn'tmatch
text, comment, or processing-instruction nodes.
XPath lets youcombine steps into
compound paths
byusingthe
/
character tosep-
arate them. For paths beginning with
/
, the first path step is relative to the root node;
otherwise,thefirstpathstepisrelativetoanothercontextnode.Forexample,
/movie/
name
startswiththerootnode,selectsall
movie
elementchildrenoftherootnode,and
selectsall
name
childrenoftheselected
movie
nodes.Ifyouwantedtoreturnalltext
nodes of the selected
name
elements, you would specify
/movie/name/text()
.
Compoundpathscaninclude
//
toselectnodesfromalldescendentsofthecontext
node(includingthecontextnode).Whenplacedatthestartofanexpression,
//
selects