1 2<!DOCTYPE html 3 PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 4 5<html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 8 9 <title>XML Path Language (XPath)</title> 10 <link rel="stylesheet" type="text/css" href="http://www.w3.org/StyleSheets/TR/W3C-REC"><style type="text/css">code { font-family: monospace }</style></head> 11 <body> 12 13 <div class="head"><a href="http://www.w3.org/"><img src="http://www.w3.org/Icons/WWW/w3c_home" alt="W3C" height="48" width="72"></a><h1>XML Path Language (XPath)<br>Version 1.0 14 </h1> 15 <h2>W3C Recommendation 16 November 1999</h2> 16 <dl> 17 <dt>This version:</dt> 18 <dd> 19 <a href="http://www.w3.org/TR/1999/REC-xpath-19991116">http://www.w3.org/TR/1999/REC-xpath-19991116</a><br> 20 (available in <a href="http://www.w3.org/TR/1999/REC-xpath-19991116.xml">XML</a> or 21 <a href="http://www.w3.org/TR/1999/REC-xpath-19991116.html">HTML</a>) 22 23 24 </dd> 25 <dt>Latest version:</dt> 26 <dd> 27 <a href="http://www.w3.org/TR/xpath">http://www.w3.org/TR/xpath</a><br> 28 29 </dd> 30 <dt>Previous versions:</dt> 31 <dd> 32 <a href="http://www.w3.org/TR/1999/PR-xpath-19991008">http://www.w3.org/TR/1999/PR-xpath-19991008</a><br> 33 <a href="http://www.w3.org/1999/08/WD-xpath-19990813">http://www.w3.org/1999/08/WD-xpath-19990813</a><br> 34 <a href="http://www.w3.org/1999/07/WD-xpath-19990709">http://www.w3.org/1999/07/WD-xpath-19990709</a><br> 35 <a href="http://www.w3.org/TR/1999/WD-xslt-19990421">http://www.w3.org/TR/1999/WD-xslt-19990421</a><br> 36 37 </dd> 38 <dt>Editors:</dt> 39 <dd> 40 41 James Clark 42 <a href="mailto:[email protected]"><[email protected]></a> 43 <br> 44 45 Steve DeRose 46 (Inso Corp. and Brown University) 47 <a href="mailto:[email protected]"><[email protected]></a> 48 <br> 49 50 </dd> 51 </dl> 52 <p class="copyright"><a href="http://www.w3.org/Consortium/Legal/ipr-notice.html#Copyright"> 53 Copyright 54 </a> © 1999 <a href="http://www.w3.org">W3C</a> 55 (<a href="http://www.lcs.mit.edu">MIT</a>, 56 <a href="http://www.inria.fr/">INRIA</a>, 57 <a href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved. W3C 58 <a href="http://www.w3.org/Consortium/Legal/ipr-notice.html#Legal_Disclaimer">liability</a>, 59 <a href="http://www.w3.org/Consortium/Legal/ipr-notice.html#W3C_Trademarks">trademark</a>, 60 <a href="http://www.w3.org/Consortium/Legal/copyright-documents.html">document use</a> and 61 <a href="http://www.w3.org/Consortium/Legal/copyright-software.html">software licensing</a> rules apply. 62 63 </p> 64 <hr title="Separator for header"> 65 </div> 66 <h2><a name="abstract">Abstract</a></h2> 67 <p>XPath is a language for addressing parts of an XML 68 document, designed to be used by both XSLT and 69 XPointer. 70 </p> 71 <h2><a name="status">Status of this document</a></h2> 72 73 74 <p>This document has been reviewed by W3C Members and other interested 75 parties and has been endorsed by the Director as a W3C <a href="http://www.w3.org/Consortium/Process/#RecsW3C">Recommendation</a>. It 76 is a stable document and may be used as reference material or cited as 77 a normative reference from other documents. W3C's role in making the 78 Recommendation is to draw attention to the specification and to 79 promote its widespread deployment. This enhances the functionality and 80 interoperability of the Web. 81 </p> 82 83 84 <p>The list of known errors in this specification is available at 85 <a href="http://www.w3.org/1999/11/REC-xpath-19991116-errata">http://www.w3.org/1999/11/REC-xpath-19991116-errata</a>. 86 </p> 87 88 89 <p>Comments on this specification may be sent to <a href="mailto:[email protected]">[email protected]</a>; <a href="http://lists.w3.org/Archives/Public/www-xpath-comments">archives</a> 90 of the comments are available. 91 </p> 92 93 94 <p>The English version of this specification is the only normative 95 version. However, for translations of this document, see <a href="http://www.w3.org/Style/XSL/translations.html">http://www.w3.org/Style/XSL/translations.html</a>. 96 </p> 97 98 99 <p>A list of current W3C Recommendations and other technical documents 100 can be found at <a href="http://www.w3.org/TR">http://www.w3.org/TR</a>. 101 </p> 102 103 104 <p>This specification is joint work of the XSL Working Group and the 105 XML Linking Working Group and so is part of the <a href="http://www.w3.org/Style/Activity">W3C Style activity</a> and 106 of the <a href="http://www.w3.org/XML/Activity">W3C XML 107 activity 108 </a>. 109 </p> 110 111 112 113 <h2><a name="contents">Table of contents</a></h2>1 <a href="#section-Introduction">Introduction</a><br>2 <a href="#location-paths">Location Paths</a><br> 2.1 <a href="#section-Location-Steps">Location Steps</a><br> 2.2 <a href="#axes">Axes</a><br> 2.3 <a href="#node-tests">Node Tests</a><br> 2.4 <a href="#predicates">Predicates</a><br> 2.5 <a href="#path-abbrev">Abbreviated Syntax</a><br>3 <a href="#section-Expressions">Expressions</a><br> 3.1 <a href="#section-Basics">Basics</a><br> 3.2 <a href="#section-Function-Calls">Function Calls</a><br> 3.3 <a href="#node-sets">Node-sets</a><br> 3.4 <a href="#booleans">Booleans</a><br> 3.5 <a href="#numbers">Numbers</a><br> 3.6 <a href="#strings">Strings</a><br> 3.7 <a href="#exprlex">Lexical Structure</a><br>4 <a href="#corelib">Core Function Library</a><br> 4.1 <a href="#section-Node-Set-Functions">Node Set Functions</a><br> 4.2 <a href="#section-String-Functions">String Functions</a><br> 4.3 <a href="#section-Boolean-Functions">Boolean Functions</a><br> 4.4 <a href="#section-Number-Functions">Number Functions</a><br>5 <a href="#data-model">Data Model</a><br> 5.1 <a href="#root-node">Root Node</a><br> 5.2 <a href="#element-nodes">Element Nodes</a><br> 5.2.1 <a href="#unique-id">Unique IDs</a><br> 5.3 <a href="#attribute-nodes">Attribute Nodes</a><br> 5.4 <a href="#namespace-nodes">Namespace Nodes</a><br> 5.5 <a href="#section-Processing-Instruction-Nodes">Processing Instruction Nodes</a><br> 5.6 <a href="#section-Comment-Nodes">Comment Nodes</a><br> 5.7 <a href="#section-Text-Nodes">Text Nodes</a><br>6 <a href="#section-Conformance">Conformance</a><br><h3>Appendices</h3>A <a href="#section-References">References</a><br> A.1 <a href="#section-Normative-References">Normative References</a><br> A.2 <a href="#section-Other-References">Other References</a><br>B <a href="#infoset">XML Information Set Mapping</a> (Non-Normative)<br><hr> 114 115 116 117 <h2><a name="section-Introduction"></a>1 Introduction 118 </h2> 119 120 121 <p>XPath is the result of an effort to provide a common syntax and 122 semantics for functionality shared between XSL Transformations <a href="#XSLT">[XSLT]</a> and XPointer <a href="#XPTR">[XPointer]</a>. The primary purpose 123 of XPath is to address parts of an XML <a href="#XML">[XML]</a> document. 124 In support of this primary purpose, it also provides basic facilities 125 for manipulation of strings, numbers and booleans. XPath uses a 126 compact, non-XML syntax to facilitate use of XPath within URIs and XML 127 attribute values. XPath operates on the abstract, logical structure 128 of an XML document, rather than its surface syntax. XPath gets its 129 name from its use of a path notation as in URLs for navigating through 130 the hierarchical structure of an XML document. 131 </p> 132 133 134 <p>In addition to its use for addressing, XPath is also designed so 135 that it has a natural subset that can be used for matching (testing 136 whether or not a node matches a pattern); this use of XPath is 137 described in <a href="http://www.w3.org/TR/WD-xslt#patterns">XSLT</a>. 138 </p> 139 140 141 <p>XPath models an XML document as a tree of nodes. There are 142 different types of nodes, including element nodes, attribute nodes and 143 text nodes. XPath defines a way to compute a <a href="#dt-string-value">string-value</a> for each type of node. 144 Some types of nodes also have names. XPath fully supports XML 145 Namespaces <a href="#XMLNAMES">[XML Names]</a>. Thus, the name of a node is 146 modeled as a pair consisting of a local part and a possibly null 147 namespace URI; this is called an <a href="#dt-expanded-name">expanded-name</a>. The data model is 148 described in detail in <a href="#data-model">[<b>5 Data Model</b>] 149 </a>. 150 </p> 151 152 153 <p>The primary syntactic construct in XPath is the expression. An 154 expression matches the production <a href="#NT-Expr">Expr</a>. An 155 expression is evaluated to yield an object, which has one of the 156 following four basic types: 157 </p> 158 159 160 <ul> 161 162 163 <li>node-set (an unordered collection of nodes without duplicates)</li> 164 165 166 <li>boolean (true or false)</li> 167 168 169 <li>number (a floating-point number)</li> 170 171 172 <li>string (a sequence of UCS characters)</li> 173 174 175 </ul> 176 177 178 <p>Expression evaluation occurs with respect to a context. XSLT and 179 XPointer specify how the context is determined for XPath expressions 180 used in XSLT and XPointer respectively. The context consists of: 181 </p> 182 183 184 <ul> 185 186 187 <li>a node (<a name="dt-context-node"></a>the 188 <b>context node</b>) 189 </li> 190 191 192 <li>a pair of non-zero positive integers (<a name="dt-context-position"></a>the <b>context 193 position 194 </b> and <a name="dt-context-size"></a>the <b>context size</b>) 195 </li> 196 197 198 <li>a set of variable bindings</li> 199 200 201 <li>a function library</li> 202 203 204 <li>the set of namespace declarations in scope for the 205 expression 206 </li> 207 208 209 </ul> 210 211 212 <p>The context position is always less than or equal to the 213 context size. 214 </p> 215 216 217 <p>The variable bindings consist of a mapping from variable names to 218 variable values. The value of a variable is an object, which can be of 219 any of the types that are possible for the value of an expression, 220 and may also be of additional types not specified here. 221 </p> 222 223 224 <p>The function library consists of a mapping from function names to 225 functions. Each function takes zero or more arguments and returns a 226 single result. This document defines a core function library that all 227 XPath implementations must support (see <a href="#corelib">[<b>4 Core Function Library</b>] 228 </a>). 229 For a function in the core function library, arguments and result are 230 of the four basic types. Both XSLT and XPointer extend XPath by 231 defining additional functions; some of these functions operate on the 232 four basic types; others operate on additional data types defined by 233 XSLT and XPointer. 234 </p> 235 236 237 <p>The namespace declarations consist of a mapping from prefixes to 238 namespace URIs. 239 </p> 240 241 242 <p>The variable bindings, function library and namespace declarations 243 used to evaluate a subexpression are always the same as those used to 244 evaluate the containing expression. The context node, context 245 position, and context size used to evaluate a subexpression are 246 sometimes different from those used to evaluate the containing 247 expression. Several kinds of expressions change the context node; only 248 predicates change the context position and context size (see <a href="#predicates">[<b>2.4 Predicates</b>] 249 </a>). When the evaluation of a kind of expression is 250 described, it will always be explicitly stated if the context node, 251 context position, and context size change for the evaluation of 252 subexpressions; if nothing is said about the context node, context 253 position, and context size, they remain unchanged for the 254 evaluation of subexpressions of that kind of expression. 255 </p> 256 257 258 <p>XPath expressions often occur in XML attributes. The grammar 259 specified in this section applies to the attribute value after XML 1.0 260 normalization. So, for example, if the grammar uses the character 261 <code><</code>, this must not appear in the XML source as 262 <code><</code> but must be quoted according to XML 1.0 rules by, 263 for example, entering it as <code>&lt;</code>. Within expressions, 264 literal strings are delimited by single or double quotation marks, 265 which are also used to delimit XML attributes. To avoid a quotation 266 mark in an expression being interpreted by the XML processor as 267 terminating the attribute value the quotation mark can be entered as a 268 character reference (<code>&quot;</code> or 269 <code>&apos;</code>). Alternatively, the expression can use single 270 quotation marks if the XML attribute is delimited with double 271 quotation marks or vice-versa. 272 </p> 273 274 275 <p>One important kind of expression is a location path. A location 276 path selects a set of nodes relative to the context node. The result 277 of evaluating an expression that is a location path is the node-set 278 containing the nodes selected by the location path. Location paths 279 can recursively contain expressions that are used to filter sets of 280 nodes. A location path matches the production <a href="#NT-LocationPath">LocationPath</a>. 281 </p> 282 283 284 <p>In the following grammar, the non-terminals <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> and <a href="http://www.w3.org/TR/REC-xml-names#NT-NCName">NCName</a> are defined in <a href="#XMLNAMES">[XML Names]</a>, and <a href="http://www.w3.org/TR/REC-xml#NT-S">S</a> is defined in 285 <a href="#XML">[XML]</a>. The grammar uses the same EBNF notation as 286 <a href="#XML">[XML]</a> (except that grammar symbols always have initial 287 capital letters). 288 </p> 289 290 291 <p>Expressions are parsed by first dividing the character string to be 292 parsed into tokens and then parsing the resulting sequence of tokens. 293 Whitespace can be freely used between tokens. The tokenization 294 process is described in <a href="#exprlex">[<b>3.7 Lexical Structure</b>] 295 </a>. 296 </p> 297 298 299 300 301 302 <h2><a name="location-paths"></a>2 Location Paths 303 </h2> 304 305 306 <p>Although location paths are not the most general grammatical 307 construct in the language (a <a href="#NT-LocationPath">LocationPath</a> is a special case of an <a href="#NT-Expr">Expr</a>), they are the most important construct and 308 will therefore be described first. 309 </p> 310 311 312 <p>Every location path can be expressed using a straightforward but 313 rather verbose syntax. There are also a number of syntactic 314 abbreviations that allow common cases to be expressed concisely. This 315 section will explain the semantics of location paths using the 316 unabbreviated syntax. The abbreviated syntax will then be explained 317 by showing how it expands into the unabbreviated syntax (see <a href="#path-abbrev">[<b>2.5 Abbreviated Syntax</b>] 318 </a>). 319 </p> 320 321 322 <p>Here are some examples of location paths using the unabbreviated 323 syntax: 324 </p> 325 326 327 <ul> 328 329 330 <li> 331 <p><code>child::para</code> selects the 332 <code>para</code> element children of the context node 333 </p> 334 </li> 335 336 337 <li> 338 <p><code>child::*</code> selects all element 339 children of the context node 340 </p> 341 </li> 342 343 344 <li> 345 <p><code>child::text()</code> selects all text 346 node children of the context node 347 </p> 348 </li> 349 350 351 <li> 352 <p><code>child::node()</code> selects all the 353 children of the context node, whatever their node type 354 </p> 355 </li> 356 357 358 <li> 359 <p><code>attribute::name</code> selects the 360 <code>name</code> attribute of the context node 361 </p> 362 </li> 363 364 365 <li> 366 <p><code>attribute::*</code> selects all the 367 attributes of the context node 368 </p> 369 </li> 370 371 372 <li> 373 <p><code>descendant::para</code> selects the 374 <code>para</code> element descendants of the context node 375 </p> 376 </li> 377 378 379 <li> 380 <p><code>ancestor::div</code> selects all <code>div</code> 381 ancestors of the context node 382 </p> 383 </li> 384 385 386 <li> 387 <p><code>ancestor-or-self::div</code> selects the 388 <code>div</code> ancestors of the context node and, if the context node is a 389 <code>div</code> element, the context node as well 390 </p> 391 </li> 392 393 394 <li> 395 <p><code>descendant-or-self::para</code> selects the 396 <code>para</code> element descendants of the context node and, if the context node is 397 a <code>para</code> element, the context node as well 398 </p> 399 </li> 400 401 402 <li> 403 <p><code>self::para</code> selects the context node if it is a 404 <code>para</code> element, and otherwise selects nothing 405 </p> 406 </li> 407 408 409 <li> 410 <p><code>child::chapter/descendant::para</code> 411 selects the <code>para</code> element descendants of the 412 <code>chapter</code> element children of the context node 413 </p> 414 </li> 415 416 417 <li> 418 <p><code>child::*/child::para</code> selects 419 all <code>para</code> grandchildren of the context node 420 </p> 421 </li> 422 423 424 <li> 425 <p><code>/</code> selects the document root (which is 426 always the parent of the document element) 427 </p> 428 </li> 429 430 431 <li> 432 <p><code>/descendant::para</code> selects all the 433 <code>para</code> elements in the same document as the context node 434 </p> 435 </li> 436 437 438 <li> 439 <p><code>/descendant::olist/child::item</code> selects all the 440 <code>item</code> elements that have an <code>olist</code> parent and 441 that are in the same document as the context node 442 </p> 443 </li> 444 445 446 <li> 447 <p><code>child::para[position()=1]</code> selects the first 448 <code>para</code> child of the context node 449 </p> 450 </li> 451 452 453 <li> 454 <p><code>child::para[position()=last()]</code> selects the last 455 <code>para</code> child of the context node 456 </p> 457 </li> 458 459 460 <li> 461 <p><code>child::para[position()=last()-1]</code> selects 462 the last but one <code>para</code> child of the context node 463 </p> 464 </li> 465 466 467 <li> 468 <p><code>child::para[position()>1]</code> selects all 469 the <code>para</code> children of the context node other than the 470 first <code>para</code> child of the context node 471 </p> 472 </li> 473 474 475 <li> 476 <p><code>following-sibling::chapter[position()=1]</code> 477 selects the next <code>chapter</code> sibling of the context node 478 </p> 479 </li> 480 481 482 <li> 483 <p><code>preceding-sibling::chapter[position()=1]</code> 484 selects the previous <code>chapter</code> sibling of the context 485 node 486 </p> 487 </li> 488 489 490 <li> 491 <p><code>/descendant::figure[position()=42]</code> selects 492 the forty-second <code>figure</code> element in the 493 document 494 </p> 495 </li> 496 497 498 <li> 499 <p><code>/child::doc/child::chapter[position()=5]/child::section[position()=2]</code> 500 selects the second <code>section</code> of the fifth 501 <code>chapter</code> of the <code>doc</code> document 502 element 503 </p> 504 </li> 505 506 507 <li> 508 <p><code>child::para[attribute::type="warning"]</code> 509 selects all <code>para</code> children of the context node that have a 510 <code>type</code> attribute with value <code>warning</code></p> 511 </li> 512 513 514 <li> 515 <p><code>child::para[attribute::type='warning'][position()=5]</code> 516 selects the fifth <code>para</code> child of the context node that has 517 a <code>type</code> attribute with value 518 <code>warning</code></p> 519 </li> 520 521 522 <li> 523 <p><code>child::para[position()=5][attribute::type="warning"]</code> 524 selects the fifth <code>para</code> child of the context node if that 525 child has a <code>type</code> attribute with value 526 <code>warning</code></p> 527 </li> 528 529 530 <li> 531 <p><code>child::chapter[child::title='Introduction']</code> 532 selects the <code>chapter</code> children of the context node that 533 have one or more <code>title</code> children with <a href="#dt-string-value">string-value</a> equal to 534 <code>Introduction</code></p> 535 </li> 536 537 538 <li> 539 <p><code>child::chapter[child::title]</code> selects the 540 <code>chapter</code> children of the context node that have one or 541 more <code>title</code> children 542 </p> 543 </li> 544 545 546 <li> 547 <p><code>child::*[self::chapter or self::appendix]</code> 548 selects the <code>chapter</code> and <code>appendix</code> children of 549 the context node 550 </p> 551 </li> 552 553 554 <li> 555 <p><code>child::*[self::chapter or 556 self::appendix][position()=last()] 557 </code> selects the last 558 <code>chapter</code> or <code>appendix</code> child of the context 559 node 560 </p> 561 </li> 562 563 564 </ul> 565 566 567 <p>There are two kinds of location path: relative location paths 568 and absolute location paths. 569 </p> 570 571 572 <p>A relative location path consists of a sequence of one or more 573 location steps separated by <code>/</code>. The steps in a relative 574 location path are composed together from left to right. Each step in 575 turn selects a set of nodes relative to a context node. An initial 576 sequence of steps is composed together with a following step as 577 follows. The initial sequence of steps selects a set of nodes 578 relative to a context node. Each node in that set is used as a 579 context node for the following step. The sets of nodes identified by 580 that step are unioned together. The set of nodes identified by 581 the composition of the steps is this union. For example, 582 <code>child::div/child::para</code> selects the 583 <code>para</code> element children of the <code>div</code> element 584 children of the context node, or, in other words, the 585 <code>para</code> element grandchildren that have <code>div</code> 586 parents. 587 </p> 588 589 590 <p>An absolute location path consists of <code>/</code> optionally 591 followed by a relative location path. A <code>/</code> by itself 592 selects the root node of the document containing the context node. If 593 it is followed by a relative location path, then the location path 594 selects the set of nodes that would be selected by the relative 595 location path relative to the root node of the document containing the 596 context node. 597 </p> 598 599 600 <h5>Location Paths</h5> 601 <table class="scrap"> 602 <tbody> 603 604 <tr valign="baseline"> 605 <td><a name="NT-LocationPath"></a>[1] 606 </td> 607 <td>LocationPath</td> 608 <td> ::= </td> 609 <td><a href="#NT-RelativeLocationPath">RelativeLocationPath</a></td> 610 <td></td> 611 </tr> 612 <tr valign="baseline"> 613 <td></td> 614 <td></td> 615 <td></td> 616 <td>| <a href="#NT-AbsoluteLocationPath">AbsoluteLocationPath</a></td> 617 <td></td> 618 </tr> 619 620 <tr valign="baseline"> 621 <td><a name="NT-AbsoluteLocationPath"></a>[2] 622 </td> 623 <td>AbsoluteLocationPath</td> 624 <td> ::= </td> 625 <td>'/' <a href="#NT-RelativeLocationPath">RelativeLocationPath</a>? 626 </td> 627 <td></td> 628 </tr> 629 <tr valign="baseline"> 630 <td></td> 631 <td></td> 632 <td></td> 633 <td>| <a href="#NT-AbbreviatedAbsoluteLocationPath">AbbreviatedAbsoluteLocationPath</a></td> 634 <td></td> 635 </tr> 636 637 <tr valign="baseline"> 638 <td><a name="NT-RelativeLocationPath"></a>[3] 639 </td> 640 <td>RelativeLocationPath</td> 641 <td> ::= </td> 642 <td><a href="#NT-Step">Step</a></td> 643 <td></td> 644 </tr> 645 <tr valign="baseline"> 646 <td></td> 647 <td></td> 648 <td></td> 649 <td>| <a href="#NT-RelativeLocationPath">RelativeLocationPath</a> '/' <a href="#NT-Step">Step</a></td> 650 <td></td> 651 </tr> 652 <tr valign="baseline"> 653 <td></td> 654 <td></td> 655 <td></td> 656 <td>| <a href="#NT-AbbreviatedRelativeLocationPath">AbbreviatedRelativeLocationPath</a></td> 657 <td></td> 658 </tr> 659 660 </tbody> 661 </table> 662 663 664 665 <h3><a name="section-Location-Steps"></a>2.1 Location Steps 666 </h3> 667 668 669 <p>A location step has three parts:</p> 670 671 672 <ul> 673 674 675 <li> 676 <p>an axis, which specifies the tree relationship between the 677 nodes selected by the location step and the context node, 678 </p> 679 </li> 680 681 682 <li> 683 <p>a node test, which specifies the node type and <a href="#dt-expanded-name">expanded-name</a> of the nodes selected 684 by the location step, and 685 </p> 686 </li> 687 688 689 <li> 690 <p>zero or more predicates, which use arbitrary expressions to 691 further refine the set of nodes selected by the location 692 step. 693 </p> 694 </li> 695 696 697 </ul> 698 699 700 <p>The syntax for a location step is the axis name and node test 701 separated by a double colon, followed by zero or more expressions each 702 in square brackets. For example, in 703 <code>child::para[position()=1]</code>, <code>child</code> is the name 704 of the axis, <code>para</code> is the node test and 705 <code>[position()=1]</code> is a predicate. 706 </p> 707 708 709 <p>The node-set selected by the location step is the node-set that 710 results from generating an initial node-set from the axis and 711 node-test, and then filtering that node-set by each of the predicates 712 in turn. 713 </p> 714 715 716 <p>The initial node-set consists of the nodes having the relationship 717 to the context node specified by the axis, and having the node type 718 and <a href="#dt-expanded-name">expanded-name</a> specified 719 by the node test. For example, a location step 720 <code>descendant::para</code> selects the <code>para</code> element 721 descendants of the context node: <code>descendant</code> specifies 722 that each node in the initial node-set must be a descendant of the 723 context; <code>para</code> specifies that each node in the initial 724 node-set must be an element named <code>para</code>. The available 725 axes are described in <a href="#axes">[<b>2.2 Axes</b>] 726 </a>. The available node tests 727 are described in <a href="#node-tests">[<b>2.3 Node Tests</b>] 728 </a>. The meaning of some 729 node tests is dependent on the axis. 730 </p> 731 732 733 <p>The initial node-set is filtered by the first predicate to generate 734 a new node-set; this new node-set is then filtered using the second 735 predicate, and so on. The final node-set is the node-set selected by 736 the location step. The axis affects how the expression in each 737 predicate is evaluated and so the semantics of a predicate is defined 738 with respect to an axis. See <a href="#predicates">[<b>2.4 Predicates</b>] 739 </a>. 740 </p> 741 742 743 <h5>Location Steps</h5> 744 <table class="scrap"> 745 <tbody> 746 747 <tr valign="baseline"> 748 <td><a name="NT-Step"></a>[4] 749 </td> 750 <td>Step</td> 751 <td> ::= </td> 752 <td><a href="#NT-AxisSpecifier">AxisSpecifier</a> 753 <a href="#NT-NodeTest">NodeTest</a> 754 <a href="#NT-Predicate">Predicate</a>* 755 </td> 756 <td></td> 757 </tr> 758 <tr valign="baseline"> 759 <td></td> 760 <td></td> 761 <td></td> 762 <td>| <a href="#NT-AbbreviatedStep">AbbreviatedStep</a></td> 763 <td></td> 764 </tr> 765 766 <tr valign="baseline"> 767 <td><a name="NT-AxisSpecifier"></a>[5] 768 </td> 769 <td>AxisSpecifier</td> 770 <td> ::= </td> 771 <td><a href="#NT-AxisName">AxisName</a> '::' 772 </td> 773 <td></td> 774 </tr> 775 <tr valign="baseline"> 776 <td></td> 777 <td></td> 778 <td></td> 779 <td>| <a href="#NT-AbbreviatedAxisSpecifier">AbbreviatedAxisSpecifier</a> 780 781 </td> 782 <td></td> 783 </tr> 784 785 </tbody> 786 </table> 787 788 789 790 791 792 <h3><a name="axes"></a>2.2 Axes 793 </h3> 794 795 796 <p>The following axes are available:</p> 797 798 799 <ul> 800 801 802 <li> 803 <p>the <code>child</code> axis contains the children of the 804 context node 805 </p> 806 </li> 807 808 809 <li> 810 <p>the <code>descendant</code> axis contains the descendants of 811 the context node; a descendant is a child or a child of a child and so 812 on; thus the descendant axis never contains attribute or namespace 813 nodes 814 </p> 815 </li> 816 817 818 <li> 819 <p>the <code>parent</code> axis contains the <a href="#dt-parent">parent</a> of the context node, if there is 820 one 821 </p> 822 </li> 823 824 825 <li> 826 <p>the <code>ancestor</code> axis contains the ancestors of the 827 context node; the ancestors of the context node consist of the 828 <a href="#dt-parent">parent</a> of context node and the 829 parent's parent and so on; thus, the ancestor axis will always include 830 the root node, unless the context node is the root node 831 </p> 832 </li> 833 834 835 <li> 836 <p>the <code>following-sibling</code> axis contains all the 837 following siblings of the context node; if the 838 context node is an attribute node or namespace node, the 839 <code>following-sibling</code> axis is empty 840 </p> 841 </li> 842 843 844 <li> 845 <p>the <code>preceding-sibling</code> axis contains all the 846 preceding siblings of the context node; if the context node is an 847 attribute node or namespace node, the <code>preceding-sibling</code> 848 axis is empty 849 </p> 850 </li> 851 852 853 <li> 854 <p>the <code>following</code> axis contains all nodes in the 855 same document as the context node that are after the context node in 856 document order, excluding any descendants and excluding attribute 857 nodes and namespace nodes 858 </p> 859 </li> 860 861 862 <li> 863 <p>the <code>preceding</code> axis contains all nodes in the 864 same document as the context node that are before the context node in 865 document order, excluding any ancestors and excluding attribute nodes 866 and namespace nodes 867 </p> 868 </li> 869 870 871 <li> 872 <p>the <code>attribute</code> axis contains the attributes of 873 the context node; the axis will be empty unless the context node is an 874 element 875 </p> 876 </li> 877 878 879 <li> 880 <p>the <code>namespace</code> axis contains the namespace nodes 881 of the context node; the axis will be empty unless the context node 882 is an element 883 </p> 884 </li> 885 886 887 <li> 888 <p>the <code>self</code> axis contains just the context node 889 itself 890 </p> 891 </li> 892 893 894 <li> 895 <p>the <code>descendant-or-self</code> axis contains the context 896 node and the descendants of the context node 897 </p> 898 </li> 899 900 901 <li> 902 <p>the <code>ancestor-or-self</code> axis contains the context 903 node and the ancestors of the context node; thus, the ancestor axis 904 will always include the root node 905 </p> 906 </li> 907 908 909 </ul> 910 911 912 <blockquote><b>NOTE: </b>The <code>ancestor</code>, <code>descendant</code>, 913 <code>following</code>, <code>preceding</code> and <code>self</code> 914 axes partition a document (ignoring attribute and namespace nodes): 915 they do not overlap and together they contain all the nodes in the 916 document. 917 </blockquote> 918 919 920 <h5>Axes</h5> 921 <table class="scrap"> 922 <tbody> 923 <tr valign="baseline"> 924 <td><a name="NT-AxisName"></a>[6] 925 </td> 926 <td>AxisName</td> 927 <td> ::= </td> 928 <td>'ancestor'</td> 929 <td></td> 930 </tr> 931 <tr valign="baseline"> 932 <td></td> 933 <td></td> 934 <td></td> 935 <td>| 'ancestor-or-self'</td> 936 <td></td> 937 </tr> 938 <tr valign="baseline"> 939 <td></td> 940 <td></td> 941 <td></td> 942 <td>| 'attribute'</td> 943 <td></td> 944 </tr> 945 <tr valign="baseline"> 946 <td></td> 947 <td></td> 948 <td></td> 949 <td>| 'child'</td> 950 <td></td> 951 </tr> 952 <tr valign="baseline"> 953 <td></td> 954 <td></td> 955 <td></td> 956 <td>| 'descendant'</td> 957 <td></td> 958 </tr> 959 <tr valign="baseline"> 960 <td></td> 961 <td></td> 962 <td></td> 963 <td>| 'descendant-or-self'</td> 964 <td></td> 965 </tr> 966 <tr valign="baseline"> 967 <td></td> 968 <td></td> 969 <td></td> 970 <td>| 'following'</td> 971 <td></td> 972 </tr> 973 <tr valign="baseline"> 974 <td></td> 975 <td></td> 976 <td></td> 977 <td>| 'following-sibling'</td> 978 <td></td> 979 </tr> 980 <tr valign="baseline"> 981 <td></td> 982 <td></td> 983 <td></td> 984 <td>| 'namespace'</td> 985 <td></td> 986 </tr> 987 <tr valign="baseline"> 988 <td></td> 989 <td></td> 990 <td></td> 991 <td>| 'parent'</td> 992 <td></td> 993 </tr> 994 <tr valign="baseline"> 995 <td></td> 996 <td></td> 997 <td></td> 998 <td>| 'preceding'</td> 999 <td></td> 1000 </tr> 1001 <tr valign="baseline"> 1002 <td></td> 1003 <td></td> 1004 <td></td> 1005 <td>| 'preceding-sibling'</td> 1006 <td></td> 1007 </tr> 1008 <tr valign="baseline"> 1009 <td></td> 1010 <td></td> 1011 <td></td> 1012 <td>| 'self'</td> 1013 <td></td> 1014 </tr> 1015 </tbody> 1016 </table> 1017 1018 1019 1020 1021 1022 <h3><a name="node-tests"></a>2.3 Node Tests 1023 </h3> 1024 1025 1026 <p><a name="dt-principal-node-type"></a>Every axis has a <b>principal node type</b>. If an axis 1027 can contain elements, then the principal node type is element; 1028 otherwise, it is the type of the nodes that the axis can 1029 contain. Thus, 1030 </p> 1031 1032 1033 <ul> 1034 1035 1036 <li>For the attribute axis, the principal node type is attribute.</li> 1037 1038 1039 <li>For the namespace axis, the principal node type is namespace.</li> 1040 1041 1042 <li>For other axes, the principal node type is element.</li> 1043 1044 1045 </ul> 1046 1047 1048 <p>A node test that is a <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> 1049 is true if and only if the type of the node (see <a href="#data-model">[<b>5 Data Model</b>] 1050 </a>) 1051 is the principal node type and has 1052 an <a href="#dt-expanded-name">expanded-name</a> equal to 1053 the <a href="#dt-expanded-name">expanded-name</a> specified 1054 by the <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a>. For example, 1055 <code>child::para</code> selects the <code>para</code> element 1056 children of the context node; if the context node has no 1057 <code>para</code> children, it will select an empty set of nodes. 1058 <code>attribute::href</code> selects the <code>href</code> attribute 1059 of the context node; if the context node has no <code>href</code> 1060 attribute, it will select an empty set of nodes. 1061 </p> 1062 1063 1064 <p>A <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> in the node test is 1065 expanded into an <a href="#dt-expanded-name">expanded-name</a> using the namespace 1066 declarations from the expression context. This is the same way 1067 expansion is done for element type names in start and end-tags except 1068 that the default namespace declared with <code>xmlns</code> is not 1069 used: if the <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> does not have 1070 a prefix, then the namespace URI is null (this is the same way 1071 attribute names are expanded). It is an error if the <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> has a prefix for which there is 1072 no namespace declaration in the expression context. 1073 </p> 1074 1075 1076 <p>A node test <code>*</code> is true for any node of the principal 1077 node type. For example, <code>child::*</code> will select all element 1078 children of the context node, and <code>attribute::*</code> will 1079 select all attributes of the context node. 1080 </p> 1081 1082 1083 <p>A node test can have the form <a href="http://www.w3.org/TR/REC-xml-names#NT-NCName">NCName</a><code>:*</code>. In this 1084 case, the prefix is expanded in the same way as with a <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a>, using the context namespace 1085 declarations. It is an error if there is no namespace declaration for 1086 the prefix in the expression context. The node test will be true for 1087 any node of the principal type whose <a href="#dt-expanded-name">expanded-name</a> has the namespace URI 1088 to which the prefix expands, regardless of the local part of the 1089 name. 1090 </p> 1091 1092 1093 <p>The node test <code>text()</code> is true for any text node. For 1094 example, <code>child::text()</code> will select the text node 1095 children of the context node. Similarly, the node test 1096 <code>comment()</code> is true for any comment node, and the node test 1097 <code>processing-instruction()</code> is true for any processing 1098 instruction. The <code>processing-instruction()</code> test may have 1099 an argument that is <a href="#NT-Literal">Literal</a>; in this case, it 1100 is true for any processing instruction that has a name equal to the 1101 value of the <a href="#NT-Literal">Literal</a>. 1102 </p> 1103 1104 1105 <p>A node test <code>node()</code> is true for any node of any type 1106 whatsoever. 1107 </p> 1108 1109 1110 <table class="scrap"> 1111 <tbody> 1112 <tr valign="baseline"> 1113 <td><a name="NT-NodeTest"></a>[7] 1114 </td> 1115 <td>NodeTest</td> 1116 <td> ::= </td> 1117 <td><a href="#NT-NameTest">NameTest</a></td> 1118 <td></td> 1119 </tr> 1120 <tr valign="baseline"> 1121 <td></td> 1122 <td></td> 1123 <td></td> 1124 <td>| <a href="#NT-NodeType">NodeType</a> '(' ')' 1125 </td> 1126 <td></td> 1127 </tr> 1128 <tr valign="baseline"> 1129 <td></td> 1130 <td></td> 1131 <td></td> 1132 <td>| 'processing-instruction' '(' <a href="#NT-Literal">Literal</a> ')' 1133 </td> 1134 <td></td> 1135 </tr> 1136 </tbody> 1137 </table> 1138 1139 1140 1141 1142 1143 <h3><a name="predicates"></a>2.4 Predicates 1144 </h3> 1145 1146 1147 <p>An axis is either a forward axis or a reverse axis. An axis that 1148 only ever contains the context node or nodes that are after the 1149 context node in <a href="#dt-document-order">document 1150 order 1151 </a> is a forward axis. An axis that only ever contains 1152 the context node or nodes that are before the context node in <a href="#dt-document-order">document order</a> is a reverse axis. 1153 Thus, the ancestor, ancestor-or-self, preceding, and preceding-sibling 1154 axes are reverse axes; all other axes are forward axes. Since the self 1155 axis always contains at most one node, it makes no difference whether 1156 it is a forward or reverse axis. <a name="dt-proximity-position"></a>The <b>proximity position</b> of a 1157 member of a node-set with respect to an axis is defined to be the 1158 position of the node in the node-set ordered in document order if the 1159 axis is a forward axis and ordered in reverse document order if the 1160 axis is a reverse axis. The first position is 1. 1161 </p> 1162 1163 1164 <p>A predicate filters a node-set with respect to an axis to produce a 1165 new node-set. For each node in the node-set to be filtered, the <a href="#NT-PredicateExpr">PredicateExpr</a> is evaluated with that node 1166 as the context node, with the number of nodes in the node-set as the 1167 context size, and with the <a href="#dt-proximity-position">proximity position</a> of the node 1168 in the node-set with respect to the axis as the context position; if 1169 <a href="#NT-PredicateExpr">PredicateExpr</a> evaluates to true for 1170 that node, the node is included in the new node-set; otherwise, it is 1171 not included. 1172 </p> 1173 1174 1175 <p>A <a href="#NT-PredicateExpr">PredicateExpr</a> is evaluated by 1176 evaluating the <a href="#NT-Expr">Expr</a> and converting the result 1177 to a boolean. If the result is a number, the result will be converted 1178 to true if the number is equal to the context position and will be 1179 converted to false otherwise; if the result is not a number, then the 1180 result will be converted as if by a call to the 1181 <b><a href="#function-boolean">boolean</a></b> function. Thus a location path 1182 <code>para[3]</code> is equivalent to 1183 <code>para[position()=3]</code>. 1184 </p> 1185 1186 1187 <h5>Predicates</h5> 1188 <table class="scrap"> 1189 <tbody> 1190 <tr valign="baseline"> 1191 <td><a name="NT-Predicate"></a>[8] 1192 </td> 1193 <td>Predicate</td> 1194 <td> ::= </td> 1195 <td>'[' <a href="#NT-PredicateExpr">PredicateExpr</a> ']' 1196 </td> 1197 <td></td> 1198 </tr> 1199 <tr valign="baseline"> 1200 <td><a name="NT-PredicateExpr"></a>[9] 1201 </td> 1202 <td>PredicateExpr</td> 1203 <td> ::= </td> 1204 <td><a href="#NT-Expr">Expr</a></td> 1205 <td></td> 1206 </tr> 1207 </tbody> 1208 </table> 1209 1210 1211 1212 1213 1214 <h3><a name="path-abbrev"></a>2.5 Abbreviated Syntax 1215 </h3> 1216 1217 1218 <p>Here are some examples of location paths using abbreviated 1219 syntax: 1220 </p> 1221 1222 1223 <ul> 1224 1225 1226 <li> 1227 <p><code>para</code> selects the <code>para</code> element children of 1228 the context node 1229 </p> 1230 </li> 1231 1232 1233 <li> 1234 <p><code>*</code> selects all element children of the 1235 context node 1236 </p> 1237 </li> 1238 1239 1240 <li> 1241 <p><code>text()</code> selects all text node children of the 1242 context node 1243 </p> 1244 </li> 1245 1246 1247 <li> 1248 <p><code>@name</code> selects the <code>name</code> attribute of 1249 the context node 1250 </p> 1251 </li> 1252 1253 1254 <li> 1255 <p><code>@*</code> selects all the attributes of the 1256 context node 1257 </p> 1258 </li> 1259 1260 1261 <li> 1262 <p><code>para[1]</code> selects the first <code>para</code> child of 1263 the context node 1264 </p> 1265 </li> 1266 1267 1268 <li> 1269 <p><code>para[last()]</code> selects the last <code>para</code> child 1270 of the context node 1271 </p> 1272 </li> 1273 1274 1275 <li> 1276 <p><code>*/para</code> selects all <code>para</code> grandchildren of 1277 the context node 1278 </p> 1279 </li> 1280 1281 1282 <li> 1283 <p><code>/doc/chapter[5]/section[2]</code> selects the second 1284 <code>section</code> of the fifth <code>chapter</code> of the 1285 <code>doc</code></p> 1286 </li> 1287 1288 1289 <li> 1290 <p><code>chapter//para</code> selects the <code>para</code> element 1291 descendants of the <code>chapter</code> element children of the 1292 context node 1293 </p> 1294 </li> 1295 1296 1297 <li> 1298 <p><code>//para</code> selects all the <code>para</code> descendants of 1299 the document root and thus selects all <code>para</code> elements in the 1300 same document as the context node 1301 </p> 1302 </li> 1303 1304 1305 <li> 1306 <p><code>//olist/item</code> selects all the <code>item</code> 1307 elements in the same document as the context node that have an 1308 <code>olist</code> parent 1309 </p> 1310 </li> 1311 1312 1313 <li> 1314 <p><code>.</code> selects the context node 1315 </p> 1316 </li> 1317 1318 1319 <li> 1320 <p><code>.//para</code> selects the <code>para</code> element 1321 descendants of the context node 1322 </p> 1323 </li> 1324 1325 1326 <li> 1327 <p><code>..</code> selects the parent of the context node 1328 </p> 1329 </li> 1330 1331 1332 <li> 1333 <p><code>../@lang</code> selects the <code>lang</code> attribute 1334 of the parent of the context node 1335 </p> 1336 </li> 1337 1338 1339 <li> 1340 <p><code>para[@type="warning"]</code> selects all <code>para</code> 1341 children of the context node that have a <code>type</code> attribute with 1342 value <code>warning</code></p> 1343 </li> 1344 1345 1346 <li> 1347 <p><code>para[@type="warning"][5]</code> selects the fifth 1348 <code>para</code> child of the context node that has a <code>type</code> 1349 attribute with value <code>warning</code></p> 1350 </li> 1351 1352 1353 <li> 1354 <p><code>para[5][@type="warning"]</code> selects the fifth 1355 <code>para</code> child of the context node if that child has a 1356 <code>type</code> attribute with value <code>warning</code></p> 1357 </li> 1358 1359 1360 <li> 1361 <p><code>chapter[title="Introduction"]</code> selects the 1362 <code>chapter</code> children of the context node that have one or 1363 more <code>title</code> children with <a href="#dt-string-value">string-value</a> equal to 1364 <code>Introduction</code></p> 1365 </li> 1366 1367 1368 <li> 1369 <p><code>chapter[title]</code> selects the <code>chapter</code> 1370 children of the context node that have one or more <code>title</code> 1371 children 1372 </p> 1373 </li> 1374 1375 1376 <li> 1377 <p><code>employee[@secretary and @assistant]</code> selects all 1378 the <code>employee</code> children of the context node that have both a 1379 <code>secretary</code> attribute and an <code>assistant</code> 1380 attribute 1381 </p> 1382 </li> 1383 1384 1385 </ul> 1386 1387 1388 <p>The most important abbreviation is that <code>child::</code> can be 1389 omitted from a location step. In effect, <code>child</code> is the 1390 default axis. For example, a location path <code>div/para</code> is 1391 short for <code>child::div/child::para</code>. 1392 </p> 1393 1394 1395 <p>There is also an abbreviation for attributes: 1396 <code>attribute::</code> can be abbreviated to <code>@</code>. For 1397 example, a location path <code>para[@type="warning"]</code> is short 1398 for <code>child::para[attribute::type="warning"]</code> and so selects 1399 <code>para</code> children with a <code>type</code> attribute with 1400 value equal to <code>warning</code>. 1401 </p> 1402 1403 1404 <p><code>//</code> is short for 1405 <code>/descendant-or-self::node()/</code>. For example, 1406 <code>//para</code> is short for 1407 <code>/descendant-or-self::node()/child::para</code> and so will 1408 select any <code>para</code> element in the document (even a 1409 <code>para</code> element that is a document element will be selected 1410 by <code>//para</code> since the document element node is a child of 1411 the root node); <code>div//para</code> is short for 1412 <code>div/descendant-or-self::node()/child::para</code> and so 1413 will select all <code>para</code> descendants of <code>div</code> 1414 children. 1415 </p> 1416 1417 1418 <blockquote><b>NOTE: </b>The location path <code>//para[1]</code> does 1419 <i>not</i> mean the same as the location path 1420 <code>/descendant::para[1]</code>. The latter selects the first 1421 descendant <code>para</code> element; the former selects all descendant 1422 <code>para</code> elements that are the first <code>para</code> 1423 children of their parents. 1424 </blockquote> 1425 1426 1427 <p>A location step of <code>.</code> is short for 1428 <code>self::node()</code>. This is particularly useful in 1429 conjunction with <code>//</code>. For example, the location path 1430 <code>.//para</code> is short for 1431 </p> 1432 1433 <pre>self::node()/descendant-or-self::node()/child::para</pre> 1434 1435 <p>and so will select all <code>para</code> descendant elements of the 1436 context node. 1437 </p> 1438 1439 1440 <p>Similarly, a location step of <code>..</code> is short for 1441 <code>parent::node()</code>. For example, <code>../title</code> is 1442 short for <code>parent::node()/child::title</code> and so will 1443 select the <code>title</code> children of the parent of the context 1444 node. 1445 </p> 1446 1447 1448 <h5>Abbreviations</h5> 1449 <table class="scrap"> 1450 <tbody> 1451 1452 <tr valign="baseline"> 1453 <td><a name="NT-AbbreviatedAbsoluteLocationPath"></a>[10] 1454 </td> 1455 <td>AbbreviatedAbsoluteLocationPath</td> 1456 <td> ::= </td> 1457 <td>'//' <a href="#NT-RelativeLocationPath">RelativeLocationPath</a></td> 1458 <td></td> 1459 </tr> 1460 1461 <tr valign="baseline"> 1462 <td><a name="NT-AbbreviatedRelativeLocationPath"></a>[11] 1463 </td> 1464 <td>AbbreviatedRelativeLocationPath</td> 1465 <td> ::= </td> 1466 <td><a href="#NT-RelativeLocationPath">RelativeLocationPath</a> '//' <a href="#NT-Step">Step</a></td> 1467 <td></td> 1468 </tr> 1469 1470 <tr valign="baseline"> 1471 <td><a name="NT-AbbreviatedStep"></a>[12] 1472 </td> 1473 <td>AbbreviatedStep</td> 1474 <td> ::= </td> 1475 <td>'.'</td> 1476 <td></td> 1477 </tr> 1478 <tr valign="baseline"> 1479 <td></td> 1480 <td></td> 1481 <td></td> 1482 <td>| '..'</td> 1483 <td></td> 1484 </tr> 1485 1486 <tr valign="baseline"> 1487 <td><a name="NT-AbbreviatedAxisSpecifier"></a>[13] 1488 </td> 1489 <td>AbbreviatedAxisSpecifier</td> 1490 <td> ::= </td> 1491 <td>'@'?</td> 1492 <td></td> 1493 </tr> 1494 1495 </tbody> 1496 </table> 1497 1498 1499 1500 1501 1502 1503 1504 <h2><a name="section-Expressions"></a>3 Expressions 1505 </h2> 1506 1507 1508 1509 <h3><a name="section-Basics"></a>3.1 Basics 1510 </h3> 1511 1512 1513 <p>A <a href="#NT-VariableReference">VariableReference</a> evaluates 1514 to the value to which the variable name is bound in the set of 1515 variable bindings in the context. It is an error if the variable name 1516 is not bound to any value in the set of variable bindings in the 1517 expression context. 1518 </p> 1519 1520 1521 <p>Parentheses may be used for grouping.</p> 1522 1523 1524 <table class="scrap"> 1525 <tbody> 1526 <tr valign="baseline"> 1527 <td><a name="NT-Expr"></a>[14] 1528 </td> 1529 <td>Expr</td> 1530 <td> ::= </td> 1531 <td><a href="#NT-OrExpr">OrExpr</a></td> 1532 <td></td> 1533 </tr> 1534 <tr valign="baseline"> 1535 <td><a name="NT-PrimaryExpr"></a>[15] 1536 </td> 1537 <td>PrimaryExpr</td> 1538 <td> ::= </td> 1539 <td><a href="#NT-VariableReference">VariableReference</a></td> 1540 <td></td> 1541 </tr> 1542 <tr valign="baseline"> 1543 <td></td> 1544 <td></td> 1545 <td></td> 1546 <td>| '(' <a href="#NT-Expr">Expr</a> ')' 1547 </td> 1548 <td></td> 1549 </tr> 1550 <tr valign="baseline"> 1551 <td></td> 1552 <td></td> 1553 <td></td> 1554 <td>| <a href="#NT-Literal">Literal</a></td> 1555 <td></td> 1556 </tr> 1557 <tr valign="baseline"> 1558 <td></td> 1559 <td></td> 1560 <td></td> 1561 <td>| <a href="#NT-Number">Number</a></td> 1562 <td></td> 1563 </tr> 1564 <tr valign="baseline"> 1565 <td></td> 1566 <td></td> 1567 <td></td> 1568 <td>| <a href="#NT-FunctionCall">FunctionCall</a></td> 1569 <td></td> 1570 </tr> 1571 </tbody> 1572 </table> 1573 1574 1575 1576 1577 1578 <h3><a name="section-Function-Calls"></a>3.2 Function Calls 1579 </h3> 1580 1581 1582 <p>A <a href="#NT-FunctionCall">FunctionCall</a> expression is 1583 evaluated by using the <a href="#NT-FunctionName">FunctionName</a> to 1584 identify a function in the expression evaluation context function 1585 library, evaluating each of the <a href="#NT-Argument">Argument</a>s, 1586 converting each argument to the type required by the function, and 1587 finally calling the function, passing it the converted arguments. It 1588 is an error if the number of arguments is wrong or if an argument 1589 cannot be converted to the required type. The result of the <a href="#NT-FunctionCall">FunctionCall</a> expression is the result 1590 returned by the function. 1591 </p> 1592 1593 1594 <p>An argument is converted to type string as if by calling the 1595 <b><a href="#function-string">string</a></b> function. An argument is converted to 1596 type number as if by calling the <b><a href="#function-number">number</a></b> function. 1597 An argument is converted to type boolean as if by calling the 1598 <b><a href="#function-boolean">boolean</a></b> function. An argument that is not of 1599 type node-set cannot be converted to a node-set. 1600 </p> 1601 1602 1603 <table class="scrap"> 1604 <tbody> 1605 <tr valign="baseline"> 1606 <td><a name="NT-FunctionCall"></a>[16] 1607 </td> 1608 <td>FunctionCall</td> 1609 <td> ::= </td> 1610 <td><a href="#NT-FunctionName">FunctionName</a> '(' ( <a href="#NT-Argument">Argument</a> ( ',' <a href="#NT-Argument">Argument</a> )* )? ')' 1611 </td> 1612 <td></td> 1613 </tr> 1614 <tr valign="baseline"> 1615 <td><a name="NT-Argument"></a>[17] 1616 </td> 1617 <td>Argument</td> 1618 <td> ::= </td> 1619 <td><a href="#NT-Expr">Expr</a></td> 1620 <td></td> 1621 </tr> 1622 </tbody> 1623 </table> 1624 1625 1626 1627 1628 1629 <h3><a name="node-sets"></a>3.3 Node-sets 1630 </h3> 1631 1632 1633 <p>A location path can be used as an expression. The expression 1634 returns the set of nodes selected by the path. 1635 </p> 1636 1637 1638 <p>The <code>|</code> operator computes the union of its operands, 1639 which must be node-sets. 1640 </p> 1641 1642 1643 <p><a href="#NT-Predicate">Predicate</a>s are used to filter 1644 expressions in the same way that they are used in location paths. It 1645 is an error if the expression to be filtered does not evaluate to a 1646 node-set. The <a href="#NT-Predicate">Predicate</a> filters the 1647 node-set with respect to the child axis. 1648 </p> 1649 1650 1651 <blockquote><b>NOTE: </b>The meaning of a <a href="#NT-Predicate">Predicate</a> 1652 depends crucially on which axis applies. For example, 1653 <code>preceding::foo[1]</code> returns the first <code>foo</code> 1654 element in <i>reverse document order</i>, because the axis that 1655 applies to the <code>[1]</code> predicate is the preceding axis; by 1656 contrast, <code>(preceding::foo)[1]</code> returns the first 1657 <code>foo</code> element in <i>document order</i>, because the 1658 axis that applies to the <code>[1]</code> predicate is the child 1659 axis. 1660 </blockquote> 1661 1662 1663 <p>The <code>/</code> and <code>//</code> operators compose an 1664 expression and a relative location path. It is an error if the 1665 expression does not evaluate to a node-set. The <code>/</code> 1666 operator does composition in the same way as when <code>/</code> is 1667 used in a location path. As in location paths, <code>//</code> is 1668 short for <code>/descendant-or-self::node()/</code>. 1669 </p> 1670 1671 1672 <p>There are no types of objects that can be converted to node-sets.</p> 1673 1674 1675 <table class="scrap"> 1676 <tbody> 1677 <tr valign="baseline"> 1678 <td><a name="NT-UnionExpr"></a>[18] 1679 </td> 1680 <td>UnionExpr</td> 1681 <td> ::= </td> 1682 <td><a href="#NT-PathExpr">PathExpr</a></td> 1683 <td></td> 1684 </tr> 1685 <tr valign="baseline"> 1686 <td></td> 1687 <td></td> 1688 <td></td> 1689 <td>| <a href="#NT-UnionExpr">UnionExpr</a> '|' <a href="#NT-PathExpr">PathExpr</a></td> 1690 <td></td> 1691 </tr> 1692 <tr valign="baseline"> 1693 <td><a name="NT-PathExpr"></a>[19] 1694 </td> 1695 <td>PathExpr</td> 1696 <td> ::= </td> 1697 <td><a href="#NT-LocationPath">LocationPath</a></td> 1698 <td></td> 1699 </tr> 1700 <tr valign="baseline"> 1701 <td></td> 1702 <td></td> 1703 <td></td> 1704 <td>| <a href="#NT-FilterExpr">FilterExpr</a></td> 1705 <td></td> 1706 </tr> 1707 <tr valign="baseline"> 1708 <td></td> 1709 <td></td> 1710 <td></td> 1711 <td>| <a href="#NT-FilterExpr">FilterExpr</a> '/' <a href="#NT-RelativeLocationPath">RelativeLocationPath</a></td> 1712 <td></td> 1713 </tr> 1714 <tr valign="baseline"> 1715 <td></td> 1716 <td></td> 1717 <td></td> 1718 <td>| <a href="#NT-FilterExpr">FilterExpr</a> '//' <a href="#NT-RelativeLocationPath">RelativeLocationPath</a></td> 1719 <td></td> 1720 </tr> 1721 <tr valign="baseline"> 1722 <td><a name="NT-FilterExpr"></a>[20] 1723 </td> 1724 <td>FilterExpr</td> 1725 <td> ::= </td> 1726 <td><a href="#NT-PrimaryExpr">PrimaryExpr</a></td> 1727 <td></td> 1728 </tr> 1729 <tr valign="baseline"> 1730 <td></td> 1731 <td></td> 1732 <td></td> 1733 <td>| <a href="#NT-FilterExpr">FilterExpr</a> <a href="#NT-Predicate">Predicate</a></td> 1734 <td></td> 1735 </tr> 1736 </tbody> 1737 </table> 1738 1739 1740 1741 1742 1743 <h3><a name="booleans"></a>3.4 Booleans 1744 </h3> 1745 1746 1747 <p>An object of type boolean can have one of two values, true and 1748 false. 1749 </p> 1750 1751 1752 <p>An <code>or</code> expression is evaluated by evaluating each 1753 operand and converting its value to a boolean as if by a call to the 1754 <b><a href="#function-boolean">boolean</a></b> function. The result is true if either 1755 value is true and false otherwise. The right operand is not evaluated 1756 if the left operand evaluates to true. 1757 </p> 1758 1759 1760 <p>An <code>and</code> expression is evaluated by evaluating each 1761 operand and converting its value to a boolean as if by a call to the 1762 <b><a href="#function-boolean">boolean</a></b> function. The result is true if both 1763 values are true and false otherwise. The right operand is not 1764 evaluated if the left operand evaluates to false. 1765 </p> 1766 1767 1768 <p>An <a href="#NT-EqualityExpr">EqualityExpr</a> (that is not just 1769 a <a href="#NT-RelationalExpr">RelationalExpr</a>) or a <a href="#NT-RelationalExpr">RelationalExpr</a> (that is not just an <a href="#NT-AdditiveExpr">AdditiveExpr</a>) is evaluated by comparing the 1770 objects that result from evaluating the two operands. Comparison of 1771 the resulting objects is defined in the following three paragraphs. 1772 First, comparisons that involve node-sets are defined in terms of 1773 comparisons that do not involve node-sets; this is defined uniformly 1774 for <code>=</code>, <code>!=</code>, <code><=</code>, 1775 <code><</code>, <code>>=</code> and <code>></code>. Second, 1776 comparisons that do not involve node-sets are defined for 1777 <code>=</code> and <code>!=</code>. Third, comparisons that do not 1778 involve node-sets are defined for <code><=</code>, 1779 <code><</code>, <code>>=</code> and <code>></code>. 1780 </p> 1781 1782 1783 <p>If both objects to be compared are node-sets, then the comparison 1784 will be true if and only if there is a node in the first node-set and 1785 a node in the second node-set such that the result of performing the 1786 comparison on the <a href="#dt-string-value">string-value</a>s of the two nodes is 1787 true. If one object to be compared is a node-set and the other is a 1788 number, then the comparison will be true if and only if there is a 1789 node in the node-set such that the result of performing the comparison 1790 on the number to be compared and on the result of converting the 1791 <a href="#dt-string-value">string-value</a> of that node to 1792 a number using the <b><a href="#function-number">number</a></b> function is true. If 1793 one object to be compared is a node-set and the other is a string, 1794 then the comparison will be true if and only if there is a node in the 1795 node-set such that the result of performing the comparison on the 1796 <a href="#dt-string-value">string-value</a> of the node and 1797 the other string is true. If one object to be compared is a node-set 1798 and the other is a boolean, then the comparison will be true if and 1799 only if the result of performing the comparison on the boolean and on 1800 the result of converting the node-set to a boolean using the 1801 <b><a href="#function-boolean">boolean</a></b> function is true. 1802 </p> 1803 1804 1805 <p>When neither object to be compared is a node-set and the operator 1806 is <code>=</code> or <code>!=</code>, then the objects are compared by 1807 converting them to a common type as follows and then comparing them. 1808 If at least one object to be compared is a boolean, then each object 1809 to be compared is converted to a boolean as if by applying the 1810 <b><a href="#function-boolean">boolean</a></b> function. Otherwise, if at least one 1811 object to be compared is a number, then each object to be compared is 1812 converted to a number as if by applying the 1813 <b><a href="#function-number">number</a></b> function. Otherwise, both objects to be 1814 compared are converted to strings as if by applying the 1815 <b><a href="#function-string">string</a></b> function. The <code>=</code> comparison 1816 will be true if and only if the objects are equal; the <code>!=</code> 1817 comparison will be true if and only if the objects are not equal. 1818 Numbers are compared for equality according to IEEE 754 <a href="#IEEE754">[IEEE 754]</a>. Two booleans are equal if either both are true or 1819 both are false. Two strings are equal if and only if they consist of 1820 the same sequence of UCS characters. 1821 </p> 1822 1823 1824 <blockquote><b>NOTE: </b>If <code>$x</code> is bound to a node-set, then 1825 <code>$x="foo"</code> does not mean the same as 1826 <code>not($x!="foo")</code>: the former is true if and only if 1827 <i>some</i> node in <code>$x</code> has the string-value 1828 <code>foo</code>; the latter is true if and only if <i>all</i> 1829 nodes in <code>$x</code> have the string-value 1830 <code>foo</code>. 1831 </blockquote> 1832 1833 1834 <p>When neither object to be compared is a node-set and the operator 1835 is <code><=</code>, <code><</code>, <code>>=</code> or 1836 <code>></code>, then the objects are compared by converting both 1837 objects to numbers and comparing the numbers according to IEEE 754. 1838 The <code><</code> comparison will be true if and only if the first 1839 number is less than the second number. The <code><=</code> 1840 comparison will be true if and only if the first number is less than 1841 or equal to the second number. The <code>></code> comparison will 1842 be true if and only if the first number is greater than the second 1843 number. The <code>>=</code> comparison will be true if and only if 1844 the first number is greater than or equal to the second number. 1845 </p> 1846 1847 1848 <blockquote><b>NOTE: </b> 1849 1850 When an XPath expression occurs in an XML document, any 1851 <code><</code> and <code><=</code> operators must be quoted 1852 according to XML 1.0 rules by using, for example, 1853 <code>&lt;</code> and <code>&lt;=</code>. In the following 1854 example the value of the <code>test</code> attribute is an XPath 1855 expression: 1856 1857 <pre><xsl:if test="@value &lt; 10">...</xsl:if></pre> 1858 1859 </blockquote> 1860 1861 1862 <table class="scrap"> 1863 <tbody> 1864 <tr valign="baseline"> 1865 <td><a name="NT-OrExpr"></a>[21] 1866 </td> 1867 <td>OrExpr</td> 1868 <td> ::= </td> 1869 <td><a href="#NT-AndExpr">AndExpr</a></td> 1870 <td></td> 1871 </tr> 1872 <tr valign="baseline"> 1873 <td></td> 1874 <td></td> 1875 <td></td> 1876 <td>| <a href="#NT-OrExpr">OrExpr</a> 'or' <a href="#NT-AndExpr">AndExpr</a></td> 1877 <td></td> 1878 </tr> 1879 <tr valign="baseline"> 1880 <td><a name="NT-AndExpr"></a>[22] 1881 </td> 1882 <td>AndExpr</td> 1883 <td> ::= </td> 1884 <td><a href="#NT-EqualityExpr">EqualityExpr</a></td> 1885 <td></td> 1886 </tr> 1887 <tr valign="baseline"> 1888 <td></td> 1889 <td></td> 1890 <td></td> 1891 <td>| <a href="#NT-AndExpr">AndExpr</a> 'and' <a href="#NT-EqualityExpr">EqualityExpr</a></td> 1892 <td></td> 1893 </tr> 1894 <tr valign="baseline"> 1895 <td><a name="NT-EqualityExpr"></a>[23] 1896 </td> 1897 <td>EqualityExpr</td> 1898 <td> ::= </td> 1899 <td><a href="#NT-RelationalExpr">RelationalExpr</a></td> 1900 <td></td> 1901 </tr> 1902 <tr valign="baseline"> 1903 <td></td> 1904 <td></td> 1905 <td></td> 1906 <td>| <a href="#NT-EqualityExpr">EqualityExpr</a> '=' <a href="#NT-RelationalExpr">RelationalExpr</a></td> 1907 <td></td> 1908 </tr> 1909 <tr valign="baseline"> 1910 <td></td> 1911 <td></td> 1912 <td></td> 1913 <td>| <a href="#NT-EqualityExpr">EqualityExpr</a> '!=' <a href="#NT-RelationalExpr">RelationalExpr</a></td> 1914 <td></td> 1915 </tr> 1916 <tr valign="baseline"> 1917 <td><a name="NT-RelationalExpr"></a>[24] 1918 </td> 1919 <td>RelationalExpr</td> 1920 <td> ::= </td> 1921 <td><a href="#NT-AdditiveExpr">AdditiveExpr</a></td> 1922 <td></td> 1923 </tr> 1924 <tr valign="baseline"> 1925 <td></td> 1926 <td></td> 1927 <td></td> 1928 <td>| <a href="#NT-RelationalExpr">RelationalExpr</a> '<' <a href="#NT-AdditiveExpr">AdditiveExpr</a></td> 1929 <td></td> 1930 </tr> 1931 <tr valign="baseline"> 1932 <td></td> 1933 <td></td> 1934 <td></td> 1935 <td>| <a href="#NT-RelationalExpr">RelationalExpr</a> '>' <a href="#NT-AdditiveExpr">AdditiveExpr</a></td> 1936 <td></td> 1937 </tr> 1938 <tr valign="baseline"> 1939 <td></td> 1940 <td></td> 1941 <td></td> 1942 <td>| <a href="#NT-RelationalExpr">RelationalExpr</a> '<=' <a href="#NT-AdditiveExpr">AdditiveExpr</a></td> 1943 <td></td> 1944 </tr> 1945 <tr valign="baseline"> 1946 <td></td> 1947 <td></td> 1948 <td></td> 1949 <td>| <a href="#NT-RelationalExpr">RelationalExpr</a> '>=' <a href="#NT-AdditiveExpr">AdditiveExpr</a></td> 1950 <td></td> 1951 </tr> 1952 </tbody> 1953 </table> 1954 1955 1956 <blockquote><b>NOTE: </b>The effect of the above grammar is that the order of 1957 precedence is (lowest precedence first): 1958 1959 1960 <ul> 1961 1962 1963 <li> 1964 <p><code>or</code></p> 1965 </li> 1966 1967 1968 <li> 1969 <p><code>and</code></p> 1970 </li> 1971 1972 1973 <li> 1974 <p><code>=</code>, <code>!=</code></p> 1975 </li> 1976 1977 1978 <li> 1979 <p><code><=</code>, <code><</code>, <code>>=</code>, 1980 <code>></code></p> 1981 </li> 1982 1983 1984 </ul> 1985 1986 and the operators are all left associative. 1987 1988 For example, <code>3 > 2 > 1</code> is equivalent to <code>(3 1989 > 2) > 1 1990 </code>, which evaluates to false. 1991 1992 1993 </blockquote> 1994 1995 1996 1997 1998 1999 <h3><a name="numbers"></a>3.5 Numbers 2000 </h3> 2001 2002 2003 <p>A number represents a floating-point number. A number can have any 2004 double-precision 64-bit format IEEE 754 value <a href="#IEEE754">[IEEE 754]</a>. 2005 These include a special "Not-a-Number" (NaN) value, 2006 positive and negative infinity, and positive and negative zero. See 2007 <a href="http://java.sun.com/docs/books/jls/html/4.doc.html#9208">Section 4.2.3</a> of <a href="#JLS">[JLS]</a> for a summary of the key 2008 rules of the IEEE 754 standard. 2009 </p> 2010 2011 2012 <p>The numeric operators convert their operands to numbers as if by 2013 calling the <b><a href="#function-number">number</a></b> function. 2014 </p> 2015 2016 2017 <p>The <code>+</code> operator performs addition. 2018 </p> 2019 2020 2021 <p>The <code>-</code> operator performs subtraction. 2022 </p> 2023 2024 2025 <blockquote><b>NOTE: </b>Since XML allows <code>-</code> in names, the <code>-</code> 2026 operator typically needs to be preceded by whitespace. For example, 2027 <code>foo-bar</code> evaluates to a node-set containing the child 2028 elements named <code>foo-bar</code>; <code>foo - bar</code> evaluates 2029 to the difference of the result of converting the <a href="#dt-string-value">string-value</a> of the first 2030 <code>foo</code> child element to a number and the result of 2031 converting the <a href="#dt-string-value">string-value</a> 2032 of the first <code>bar</code> child to a number. 2033 </blockquote> 2034 2035 2036 <p>The <code>div</code> operator performs floating-point division 2037 according to IEEE 754. 2038 </p> 2039 2040 2041 <p>The <code>mod</code> operator returns the remainder from a 2042 truncating division. For example, 2043 </p> 2044 2045 2046 <ul> 2047 2048 <li> 2049 <p><code>5 mod 2</code> returns <code>1</code></p> 2050 </li> 2051 2052 <li> 2053 <p><code>5 mod -2</code> returns <code>1</code></p> 2054 </li> 2055 2056 <li> 2057 <p><code>-5 mod 2</code> returns <code>-1</code></p> 2058 </li> 2059 2060 <li> 2061 <p><code>-5 mod -2</code> returns <code>-1</code></p> 2062 </li> 2063 2064 </ul> 2065 2066 2067 <blockquote><b>NOTE: </b>This is the same as the <code>%</code> operator in Java and 2068 ECMAScript. 2069 </blockquote> 2070 2071 2072 <blockquote><b>NOTE: </b>This is not the same as the IEEE 754 remainder operation, which 2073 returns the remainder from a rounding division. 2074 </blockquote> 2075 2076 2077 <h5>Numeric Expressions</h5> 2078 <table class="scrap"> 2079 <tbody> 2080 2081 <tr valign="baseline"> 2082 <td><a name="NT-AdditiveExpr"></a>[25] 2083 </td> 2084 <td>AdditiveExpr</td> 2085 <td> ::= </td> 2086 <td><a href="#NT-MultiplicativeExpr">MultiplicativeExpr</a></td> 2087 <td></td> 2088 </tr> 2089 <tr valign="baseline"> 2090 <td></td> 2091 <td></td> 2092 <td></td> 2093 <td>| <a href="#NT-AdditiveExpr">AdditiveExpr</a> '+' <a href="#NT-MultiplicativeExpr">MultiplicativeExpr</a></td> 2094 <td></td> 2095 </tr> 2096 <tr valign="baseline"> 2097 <td></td> 2098 <td></td> 2099 <td></td> 2100 <td>| <a href="#NT-AdditiveExpr">AdditiveExpr</a> '-' <a href="#NT-MultiplicativeExpr">MultiplicativeExpr</a></td> 2101 <td></td> 2102 </tr> 2103 2104 <tr valign="baseline"> 2105 <td><a name="NT-MultiplicativeExpr"></a>[26] 2106 </td> 2107 <td>MultiplicativeExpr</td> 2108 <td> ::= </td> 2109 <td><a href="#NT-UnaryExpr">UnaryExpr</a></td> 2110 <td></td> 2111 </tr> 2112 <tr valign="baseline"> 2113 <td></td> 2114 <td></td> 2115 <td></td> 2116 <td>| <a href="#NT-MultiplicativeExpr">MultiplicativeExpr</a> <a href="#NT-MultiplyOperator">MultiplyOperator</a> <a href="#NT-UnaryExpr">UnaryExpr</a></td> 2117 <td></td> 2118 </tr> 2119 <tr valign="baseline"> 2120 <td></td> 2121 <td></td> 2122 <td></td> 2123 <td>| <a href="#NT-MultiplicativeExpr">MultiplicativeExpr</a> 'div' <a href="#NT-UnaryExpr">UnaryExpr</a></td> 2124 <td></td> 2125 </tr> 2126 <tr valign="baseline"> 2127 <td></td> 2128 <td></td> 2129 <td></td> 2130 <td>| <a href="#NT-MultiplicativeExpr">MultiplicativeExpr</a> 'mod' <a href="#NT-UnaryExpr">UnaryExpr</a></td> 2131 <td></td> 2132 </tr> 2133 2134 <tr valign="baseline"> 2135 <td><a name="NT-UnaryExpr"></a>[27] 2136 </td> 2137 <td>UnaryExpr</td> 2138 <td> ::= </td> 2139 <td><a href="#NT-UnionExpr">UnionExpr</a></td> 2140 <td></td> 2141 </tr> 2142 <tr valign="baseline"> 2143 <td></td> 2144 <td></td> 2145 <td></td> 2146 <td>| '-' <a href="#NT-UnaryExpr">UnaryExpr</a></td> 2147 <td></td> 2148 </tr> 2149 2150 </tbody> 2151 </table> 2152 2153 2154 2155 2156 2157 <h3><a name="strings"></a>3.6 Strings 2158 </h3> 2159 2160 2161 <p>Strings consist of a sequence of zero or more characters, where a 2162 character is defined as in the XML Recommendation <a href="#XML">[XML]</a>. 2163 A single character in XPath thus corresponds to a single Unicode 2164 abstract character with a single corresponding Unicode scalar value 2165 (see <a href="#UNICODE">[Unicode]</a>); this is not the same thing as a 16-bit 2166 Unicode code value: the Unicode coded character representation for an 2167 abstract character with Unicode scalar value greater that U+FFFF is a 2168 pair of 16-bit Unicode code values (a surrogate pair). In many 2169 programming languages, a string is represented by a sequence of 16-bit 2170 Unicode code values; implementations of XPath in such languages must 2171 take care to ensure that a surrogate pair is correctly treated as a 2172 single XPath character. 2173 </p> 2174 2175 2176 <blockquote><b>NOTE: </b>It is possible in Unicode for there to be two strings that 2177 should be treated as identical even though they consist of the 2178 distinct sequences of Unicode abstract characters. For example, some 2179 accented characters may be represented in either a precomposed or 2180 decomposed form. Therefore, XPath expressions may return unexpected 2181 results unless both the characters in the XPath expression and in the 2182 XML document have been normalized into a canonical form. See <a href="#CHARMOD">[Character Model]</a>. 2183 </blockquote> 2184 2185 2186 2187 2188 2189 <h3><a name="exprlex"></a>3.7 Lexical Structure 2190 </h3> 2191 2192 2193 <p>When tokenizing, the longest possible token is always returned.</p> 2194 2195 2196 <p>For readability, whitespace may be used in expressions even though not 2197 explicitly allowed by the grammar: <a href="#NT-ExprWhitespace">ExprWhitespace</a> may be freely added within 2198 patterns before or after any <a href="#NT-ExprToken">ExprToken</a>. 2199 </p> 2200 2201 2202 <p>The following special tokenization rules must be applied in the 2203 order specified to disambiguate the <a href="#NT-ExprToken">ExprToken</a> grammar: 2204 </p> 2205 2206 2207 <ul> 2208 2209 2210 <li> 2211 <p>If there is a preceding token and the preceding token is not 2212 one of <code>@</code>, <code>::</code>, <code>(</code>, 2213 <code>[</code>, <code>,</code> or an <a href="#NT-Operator">Operator</a>, then a <code>*</code> must be 2214 recognized as a <a href="#NT-MultiplyOperator">MultiplyOperator</a> 2215 and an <a href="http://www.w3.org/TR/REC-xml-names#NT-NCName">NCName</a> must be 2216 recognized as an <a href="#NT-OperatorName">OperatorName</a>. 2217 </p> 2218 </li> 2219 2220 2221 <li> 2222 <p>If the character following an <a href="http://www.w3.org/TR/REC-xml-names#NT-NCName">NCName</a> (possibly after intervening 2223 <a href="#NT-ExprWhitespace">ExprWhitespace</a>) is <code>(</code>, 2224 then the token must be recognized as a <a href="#NT-NodeType">NodeType</a> or a <a href="#NT-FunctionName">FunctionName</a>. 2225 </p> 2226 </li> 2227 2228 2229 <li> 2230 <p>If the two characters following an <a href="http://www.w3.org/TR/REC-xml-names#NT-NCName">NCName</a> (possibly after intervening 2231 <a href="#NT-ExprWhitespace">ExprWhitespace</a>) are <code>::</code>, 2232 then the token must be recognized as an <a href="#NT-AxisName">AxisName</a>. 2233 </p> 2234 </li> 2235 2236 2237 <li> 2238 <p>Otherwise, the token must not be recognized as a <a href="#NT-MultiplyOperator">MultiplyOperator</a>, an <a href="#NT-OperatorName">OperatorName</a>, a <a href="#NT-NodeType">NodeType</a>, a <a href="#NT-FunctionName">FunctionName</a>, or an <a href="#NT-AxisName">AxisName</a>. 2239 </p> 2240 </li> 2241 2242 2243 </ul> 2244 2245 2246 <h5>Expression Lexical Structure</h5> 2247 <table class="scrap"> 2248 <tbody> 2249 2250 <tr valign="baseline"> 2251 <td><a name="NT-ExprToken"></a>[28] 2252 </td> 2253 <td>ExprToken</td> 2254 <td> ::= </td> 2255 <td>'(' | ')' | '[' | ']' | '.' | '..' | '@' | ',' | '::'</td> 2256 <td></td> 2257 </tr> 2258 <tr valign="baseline"> 2259 <td></td> 2260 <td></td> 2261 <td></td> 2262 <td>| <a href="#NT-NameTest">NameTest</a></td> 2263 <td></td> 2264 </tr> 2265 <tr valign="baseline"> 2266 <td></td> 2267 <td></td> 2268 <td></td> 2269 <td>| <a href="#NT-NodeType">NodeType</a></td> 2270 <td></td> 2271 </tr> 2272 <tr valign="baseline"> 2273 <td></td> 2274 <td></td> 2275 <td></td> 2276 <td>| <a href="#NT-Operator">Operator</a></td> 2277 <td></td> 2278 </tr> 2279 <tr valign="baseline"> 2280 <td></td> 2281 <td></td> 2282 <td></td> 2283 <td>| <a href="#NT-FunctionName">FunctionName</a></td> 2284 <td></td> 2285 </tr> 2286 <tr valign="baseline"> 2287 <td></td> 2288 <td></td> 2289 <td></td> 2290 <td>| <a href="#NT-AxisName">AxisName</a></td> 2291 <td></td> 2292 </tr> 2293 <tr valign="baseline"> 2294 <td></td> 2295 <td></td> 2296 <td></td> 2297 <td>| <a href="#NT-Literal">Literal</a></td> 2298 <td></td> 2299 </tr> 2300 <tr valign="baseline"> 2301 <td></td> 2302 <td></td> 2303 <td></td> 2304 <td>| <a href="#NT-Number">Number</a></td> 2305 <td></td> 2306 </tr> 2307 <tr valign="baseline"> 2308 <td></td> 2309 <td></td> 2310 <td></td> 2311 <td>| <a href="#NT-VariableReference">VariableReference</a></td> 2312 <td></td> 2313 </tr> 2314 2315 <tr valign="baseline"> 2316 <td><a name="NT-Literal"></a>[29] 2317 </td> 2318 <td>Literal</td> 2319 <td> ::= </td> 2320 <td>'"' [^"]* '"'</td> 2321 <td></td> 2322 </tr> 2323 <tr valign="baseline"> 2324 <td></td> 2325 <td></td> 2326 <td></td> 2327 <td>| "'" [^']* "'"</td> 2328 <td></td> 2329 </tr> 2330 2331 <tr valign="baseline"> 2332 <td><a name="NT-Number"></a>[30] 2333 </td> 2334 <td>Number</td> 2335 <td> ::= </td> 2336 <td><a href="#NT-Digits">Digits</a> ('.' <a href="#NT-Digits">Digits</a>?)? 2337 </td> 2338 <td></td> 2339 </tr> 2340 <tr valign="baseline"> 2341 <td></td> 2342 <td></td> 2343 <td></td> 2344 <td>| '.' <a href="#NT-Digits">Digits</a></td> 2345 <td></td> 2346 </tr> 2347 2348 <tr valign="baseline"> 2349 <td><a name="NT-Digits"></a>[31] 2350 </td> 2351 <td>Digits</td> 2352 <td> ::= </td> 2353 <td>[0-9]+</td> 2354 <td></td> 2355 </tr> 2356 2357 <tr valign="baseline"> 2358 <td><a name="NT-Operator"></a>[32] 2359 </td> 2360 <td>Operator</td> 2361 <td> ::= </td> 2362 <td><a href="#NT-OperatorName">OperatorName</a></td> 2363 <td></td> 2364 </tr> 2365 <tr valign="baseline"> 2366 <td></td> 2367 <td></td> 2368 <td></td> 2369 <td>| <a href="#NT-MultiplyOperator">MultiplyOperator</a></td> 2370 <td></td> 2371 </tr> 2372 <tr valign="baseline"> 2373 <td></td> 2374 <td></td> 2375 <td></td> 2376 <td>| '/' | '//' | '|' | '+' | '-' | '=' | '!=' | '<' | '<=' | '>' | '>='</td> 2377 <td></td> 2378 </tr> 2379 2380 <tr valign="baseline"> 2381 <td><a name="NT-OperatorName"></a>[33] 2382 </td> 2383 <td>OperatorName</td> 2384 <td> ::= </td> 2385 <td>'and' | 'or' | 'mod' | 'div'</td> 2386 <td></td> 2387 </tr> 2388 2389 <tr valign="baseline"> 2390 <td><a name="NT-MultiplyOperator"></a>[34] 2391 </td> 2392 <td>MultiplyOperator</td> 2393 <td> ::= </td> 2394 <td>'*'</td> 2395 <td></td> 2396 </tr> 2397 2398 <tr valign="baseline"> 2399 <td><a name="NT-FunctionName"></a>[35] 2400 </td> 2401 <td>FunctionName</td> 2402 <td> ::= </td> 2403 <td> 2404 <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> 2405 - <a href="#NT-NodeType">NodeType</a> 2406 2407 </td> 2408 <td></td> 2409 </tr> 2410 2411 <tr valign="baseline"> 2412 <td><a name="NT-VariableReference"></a>[36] 2413 </td> 2414 <td>VariableReference</td> 2415 <td> ::= </td> 2416 <td>'$' <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a></td> 2417 <td></td> 2418 </tr> 2419 2420 <tr valign="baseline"> 2421 <td><a name="NT-NameTest"></a>[37] 2422 </td> 2423 <td>NameTest</td> 2424 <td> ::= </td> 2425 <td>'*'</td> 2426 <td></td> 2427 </tr> 2428 <tr valign="baseline"> 2429 <td></td> 2430 <td></td> 2431 <td></td> 2432 <td>| <a href="http://www.w3.org/TR/REC-xml-names#NT-NCName">NCName</a> ':' '*' 2433 </td> 2434 <td></td> 2435 </tr> 2436 <tr valign="baseline"> 2437 <td></td> 2438 <td></td> 2439 <td></td> 2440 <td>| <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a></td> 2441 <td></td> 2442 </tr> 2443 2444 <tr valign="baseline"> 2445 <td><a name="NT-NodeType"></a>[38] 2446 </td> 2447 <td>NodeType</td> 2448 <td> ::= </td> 2449 <td>'comment'</td> 2450 <td></td> 2451 </tr> 2452 <tr valign="baseline"> 2453 <td></td> 2454 <td></td> 2455 <td></td> 2456 <td>| 'text'</td> 2457 <td></td> 2458 </tr> 2459 <tr valign="baseline"> 2460 <td></td> 2461 <td></td> 2462 <td></td> 2463 <td>| 'processing-instruction'</td> 2464 <td></td> 2465 </tr> 2466 <tr valign="baseline"> 2467 <td></td> 2468 <td></td> 2469 <td></td> 2470 <td>| 'node'</td> 2471 <td></td> 2472 </tr> 2473 2474 <tr valign="baseline"> 2475 <td><a name="NT-ExprWhitespace"></a>[39] 2476 </td> 2477 <td>ExprWhitespace</td> 2478 <td> ::= </td> 2479 <td><a href="http://www.w3.org/TR/REC-xml#NT-S">S</a></td> 2480 <td></td> 2481 </tr> 2482 2483 </tbody> 2484 </table> 2485 2486 2487 2488 2489 2490 2491 2492 <h2><a name="corelib"></a>4 Core Function Library 2493 </h2> 2494 2495 2496 <p>This section describes functions that XPath implementations must 2497 always include in the function library that is used to evaluate 2498 expressions. 2499 </p> 2500 2501 2502 <p>Each function in the function library is specified using a function 2503 prototype, which gives the return type, the name of the function, and 2504 the type of the arguments. If an argument type is followed by a 2505 question mark, then the argument is optional; otherwise, the argument 2506 is required. 2507 </p> 2508 2509 2510 2511 <h3><a name="section-Node-Set-Functions"></a>4.1 Node Set Functions 2512 </h3> 2513 2514 2515 <p><a name="function-last"><b>Function: </b><i>number</i> <b>last</b>() 2516 </a> 2517 </p> 2518 2519 2520 <p>The <b><a href="#function-last">last</a></b> function returns a number equal to 2521 the <a href="#dt-context-size">context size</a> from the 2522 expression evaluation context. 2523 </p> 2524 2525 2526 <p><a name="function-position"><b>Function: </b><i>number</i> <b>position</b>() 2527 </a> 2528 </p> 2529 2530 2531 <p>The <b><a href="#function-position">position</a></b> function returns a number equal to 2532 the <a href="#dt-context-position">context position</a> from 2533 the expression evaluation context. 2534 </p> 2535 2536 2537 <p><a name="function-count"><b>Function: </b><i>number</i> <b>count</b>(<i>node-set</i>) 2538 </a> 2539 </p> 2540 2541 2542 <p>The <b><a href="#function-count">count</a></b> function returns the number of nodes in the 2543 argument node-set. 2544 </p> 2545 2546 2547 <p><a name="function-id"><b>Function: </b><i>node-set</i> <b>id</b>(<i>object</i>) 2548 </a> 2549 </p> 2550 2551 2552 <p>The <b><a href="#function-id">id</a></b> function selects elements by their 2553 unique ID (see <a href="#unique-id">[<b>5.2.1 Unique IDs</b>] 2554 </a>). When the argument to 2555 <b><a href="#function-id">id</a></b> is of type node-set, then the result is the 2556 union of the result of applying <b><a href="#function-id">id</a></b> to the 2557 <a href="#dt-string-value">string-value</a> of each of the 2558 nodes in the argument node-set. When the argument to 2559 <b><a href="#function-id">id</a></b> is of any other type, the argument is 2560 converted to a string as if by a call to the 2561 <b><a href="#function-string">string</a></b> function; the string is split into a 2562 whitespace-separated list of tokens (whitespace is any sequence of 2563 characters matching the production <a href="http://www.w3.org/TR/REC-xml#NT-S">S</a>); 2564 the result is a node-set containing the elements in the same document 2565 as the context node that have a unique ID equal to any of the tokens 2566 in the list. 2567 </p> 2568 2569 2570 <ul> 2571 2572 <li> 2573 <p><code>id("foo")</code> selects the element with unique ID 2574 <code>foo</code></p> 2575 </li> 2576 2577 <li> 2578 <p><code>id("foo")/child::para[position()=5]</code> selects 2579 the fifth <code>para</code> child of the element with unique ID 2580 <code>foo</code></p> 2581 </li> 2582 2583 </ul> 2584 2585 2586 <p><a name="function-local-name"><b>Function: </b><i>string</i> <b>local-name</b>(<i>node-set</i>?) 2587 </a> 2588 </p> 2589 2590 2591 <p>The <b><a href="#function-local-name">local-name</a></b> function returns the local part 2592 of the <a href="#dt-expanded-name">expanded-name</a> of the 2593 node in the argument node-set that is first in <a href="#dt-document-order">document order</a>. If the argument 2594 node-set is empty or the first node has no <a href="#dt-expanded-name">expanded-name</a>, an empty string is 2595 returned. If the argument is omitted, it defaults to a node-set with 2596 the context node as its only member. 2597 </p> 2598 2599 2600 <p><a name="function-namespace-uri"><b>Function: </b><i>string</i> <b>namespace-uri</b>(<i>node-set</i>?) 2601 </a> 2602 </p> 2603 2604 2605 <p>The <b><a href="#function-namespace-uri">namespace-uri</a></b> function returns the 2606 namespace URI of the <a href="#dt-expanded-name">expanded-name</a> of the node in the 2607 argument node-set that is first in <a href="#dt-document-order">document order</a>. If the argument 2608 node-set is empty, the first node has no <a href="#dt-expanded-name">expanded-name</a>, or the namespace URI 2609 of the <a href="#dt-expanded-name">expanded-name</a> is 2610 null, an empty string is returned. If the argument is omitted, it 2611 defaults to a node-set with the context node as its only member. 2612 </p> 2613 2614 2615 <blockquote><b>NOTE: </b>The string returned by the 2616 <b><a href="#function-namespace-uri">namespace-uri</a></b> function will be empty except for 2617 element nodes and attribute nodes. 2618 </blockquote> 2619 2620 2621 <p><a name="function-name"><b>Function: </b><i>string</i> <b>name</b>(<i>node-set</i>?) 2622 </a> 2623 </p> 2624 2625 2626 <p>The <b><a href="#function-name">name</a></b> function returns a string containing 2627 a <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> representing the 2628 <a href="#dt-expanded-name">expanded-name</a> of the node in 2629 the argument node-set that is first in <a href="#dt-document-order">document order</a>. The <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> must represent the <a href="#dt-expanded-name">expanded-name</a> with respect to the 2630 namespace declarations in effect on the node whose <a href="#dt-expanded-name">expanded-name</a> is being represented. 2631 Typically, this will be the <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> that occurred in the XML 2632 source. This need not be the case if there are namespace declarations 2633 in effect on the node that associate multiple prefixes with the same 2634 namespace. However, an implementation may include information about 2635 the original prefix in its representation of nodes; in this case, an 2636 implementation can ensure that the returned string is always the same 2637 as the <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> used in the XML 2638 source. If the argument node-set is empty or the first node has no 2639 <a href="#dt-expanded-name">expanded-name</a>, an empty 2640 string is returned. If the argument it omitted, it defaults to a 2641 node-set with the context node as its only member. 2642 </p> 2643 2644 2645 <blockquote><b>NOTE: </b>The string returned by the <b><a href="#function-name">name</a></b> function 2646 will be the same as the string returned by the 2647 <b><a href="#function-local-name">local-name</a></b> function except for element nodes and 2648 attribute nodes. 2649 </blockquote> 2650 2651 2652 2653 2654 2655 <h3><a name="section-String-Functions"></a>4.2 String Functions 2656 </h3> 2657 2658 2659 <p><a name="function-string"><b>Function: </b><i>string</i> <b>string</b>(<i>object</i>?) 2660 </a> 2661 </p> 2662 2663 2664 <p>The <b><a href="#function-string">string</a></b> function converts an object to a string 2665 as follows: 2666 </p> 2667 2668 2669 <ul> 2670 2671 2672 <li> 2673 <p>A node-set is converted to a string by returning the <a href="#dt-string-value">string-value</a> of the node in the 2674 node-set that is first in <a href="#dt-document-order">document 2675 order 2676 </a>. If the node-set is empty, an empty string is 2677 returned. 2678 </p> 2679 </li> 2680 2681 2682 <li> 2683 <p>A number is converted to a string as follows</p> 2684 2685 2686 <ul> 2687 2688 2689 <li> 2690 <p>NaN is converted to the string <code>NaN</code></p> 2691 </li> 2692 2693 2694 <li> 2695 <p>positive zero is converted to the string 2696 <code>0</code></p> 2697 </li> 2698 2699 2700 <li> 2701 <p>negative zero is converted to the string 2702 <code>0</code></p> 2703 </li> 2704 2705 2706 <li> 2707 <p>positive infinity is converted to the string 2708 <code>Infinity</code></p> 2709 </li> 2710 2711 2712 <li> 2713 <p>negative infinity is converted to the string 2714 <code>-Infinity</code></p> 2715 </li> 2716 2717 2718 <li> 2719 <p>if the number is an integer, the number is represented in 2720 decimal form as a <a href="#NT-Number">Number</a> with no decimal 2721 point and no leading zeros, preceded by a minus sign (<code>-</code>) 2722 if the number is negative 2723 </p> 2724 </li> 2725 2726 2727 <li> 2728 <p>otherwise, the number is represented in decimal form as a <a href="#NT-Number">Number</a> including a decimal point with at least 2729 one digit before the decimal point and at least one digit after the 2730 decimal point, preceded by a minus sign (<code>-</code>) if the number 2731 is negative; there must be no leading zeros before the decimal point 2732 apart possibly from the one required digit immediately before the 2733 decimal point; beyond the one required digit after the decimal point 2734 there must be as many, but only as many, more digits as are needed to 2735 uniquely distinguish the number from all other IEEE 754 numeric 2736 values. 2737 </p> 2738 </li> 2739 2740 2741 </ul> 2742 2743 2744 </li> 2745 2746 2747 <li> 2748 <p>The boolean false value is converted to the string 2749 <code>false</code>. The boolean true value is converted to the 2750 string <code>true</code>. 2751 </p> 2752 </li> 2753 2754 2755 <li> 2756 <p>An object of a type other than the four basic types is 2757 converted to a string in a way that is dependent on that 2758 type. 2759 </p> 2760 </li> 2761 2762 2763 </ul> 2764 2765 2766 <p>If the argument is omitted, it defaults to a node-set with the 2767 context node as its only member. 2768 </p> 2769 2770 2771 <blockquote><b>NOTE: </b>The <code>string</code> function is not intended for 2772 converting numbers into strings for presentation to users. The 2773 <code>format-number</code> function and <code>xsl:number</code> 2774 element in <a href="#XSLT">[XSLT]</a> provide this 2775 functionality. 2776 </blockquote> 2777 2778 2779 <p><a name="function-concat"><b>Function: </b><i>string</i> <b>concat</b>(<i>string</i>, <i>string</i>, <i>string</i>*) 2780 </a> 2781 </p> 2782 2783 2784 <p>The <b><a href="#function-concat">concat</a></b> function returns the concatenation of its 2785 arguments. 2786 </p> 2787 2788 2789 <p><a name="function-starts-with"><b>Function: </b><i>boolean</i> <b>starts-with</b>(<i>string</i>, <i>string</i>) 2790 </a> 2791 </p> 2792 2793 2794 <p>The <b><a href="#function-starts-with">starts-with</a></b> function returns true if the 2795 first argument string starts with the second argument string, and 2796 otherwise returns false. 2797 </p> 2798 2799 2800 <p><a name="function-contains"><b>Function: </b><i>boolean</i> <b>contains</b>(<i>string</i>, <i>string</i>) 2801 </a> 2802 </p> 2803 2804 2805 <p>The <b><a href="#function-contains">contains</a></b> function returns true if the first 2806 argument string contains the second argument string, and otherwise 2807 returns false. 2808 </p> 2809 2810 2811 <p><a name="function-substring-before"><b>Function: </b><i>string</i> <b>substring-before</b>(<i>string</i>, <i>string</i>) 2812 </a> 2813 </p> 2814 2815 2816 <p>The <b><a href="#function-substring-before">substring-before</a></b> function returns the substring 2817 of the first argument string that precedes the first occurrence of the 2818 second argument string in the first argument string, or the empty 2819 string if the first argument string does not contain the second 2820 argument string. For example, 2821 <code>substring-before("1999/04/01","/")</code> returns 2822 <code>1999</code>. 2823 </p> 2824 2825 2826 <p><a name="function-substring-after"><b>Function: </b><i>string</i> <b>substring-after</b>(<i>string</i>, <i>string</i>) 2827 </a> 2828 </p> 2829 2830 2831 <p>The <b><a href="#function-substring-after">substring-after</a></b> function returns the 2832 substring of the first argument string that follows the first 2833 occurrence of the second argument string in the first argument string, 2834 or the empty string if the first argument string does not contain the 2835 second argument string. For example, 2836 <code>substring-after("1999/04/01","/")</code> returns 2837 <code>04/01</code>, and 2838 <code>substring-after("1999/04/01","19")</code> returns 2839 <code>99/04/01</code>. 2840 </p> 2841 2842 2843 <p><a name="function-substring"><b>Function: </b><i>string</i> <b>substring</b>(<i>string</i>, <i>number</i>, <i>number</i>?) 2844 </a> 2845 </p> 2846 2847 2848 <p>The <b><a href="#function-substring">substring</a></b> function returns the substring of the 2849 first argument starting at the position specified in the second 2850 argument with length specified in the third argument. For example, 2851 <code>substring("12345",2,3)</code> returns <code>"234"</code>. 2852 If the third argument is not specified, it returns 2853 the substring starting at the position specified in the second 2854 argument and continuing to the end of the string. For example, 2855 <code>substring("12345",2)</code> returns <code>"2345"</code>. 2856 </p> 2857 2858 2859 <p>More precisely, each character in the string (see <a href="#strings">[<b>3.6 Strings</b>] 2860 </a>) is considered to have a numeric position: the 2861 position of the first character is 1, the position of the second 2862 character is 2 and so on. 2863 </p> 2864 2865 2866 <blockquote><b>NOTE: </b>This differs from Java and ECMAScript, in which the 2867 <code>String.substring</code> method treats the position of the first 2868 character as 0. 2869 </blockquote> 2870 2871 2872 <p>The returned substring contains those 2873 characters for which the position of the character is greater than or 2874 equal to the rounded value of the second argument and, if the third 2875 argument is specified, less than the sum of the rounded value of the 2876 second argument and the rounded value of the third argument; the 2877 comparisons and addition used for the above follow the standard IEEE 2878 754 rules; rounding is done as if by a call to the 2879 <b><a href="#function-round">round</a></b> function. The following examples illustrate 2880 various unusual cases: 2881 </p> 2882 2883 2884 <ul> 2885 2886 2887 <li> 2888 <p><code>substring("12345", 1.5, 2.6)</code> returns 2889 <code>"234"</code></p> 2890 </li> 2891 2892 2893 <li> 2894 <p><code>substring("12345", 0, 3)</code> returns 2895 <code>"12"</code></p> 2896 </li> 2897 2898 2899 <li> 2900 <p><code>substring("12345", 0 div 0, 3)</code> returns 2901 <code>""</code></p> 2902 </li> 2903 2904 2905 <li> 2906 <p><code>substring("12345", 1, 0 div 0)</code> returns 2907 <code>""</code></p> 2908 </li> 2909 2910 2911 <li> 2912 <p><code>substring("12345", -42, 1 div 0)</code> returns 2913 <code>"12345"</code></p> 2914 </li> 2915 2916 2917 <li> 2918 <p><code>substring("12345", -1 div 0, 1 div 0)</code> returns 2919 <code>""</code></p> 2920 </li> 2921 2922 2923 </ul> 2924 2925 2926 <p><a name="function-string-length"><b>Function: </b><i>number</i> <b>string-length</b>(<i>string</i>?) 2927 </a> 2928 </p> 2929 2930 2931 <p>The <b><a href="#function-string-length">string-length</a></b> returns the number of 2932 characters in the string (see <a href="#strings">[<b>3.6 Strings</b>] 2933 </a>). If the 2934 argument is omitted, it defaults to the context node converted to a 2935 string, in other words the <a href="#dt-string-value">string-value</a> of the context node. 2936 </p> 2937 2938 2939 <p><a name="function-normalize-space"><b>Function: </b><i>string</i> <b>normalize-space</b>(<i>string</i>?) 2940 </a> 2941 </p> 2942 2943 2944 <p>The <b><a href="#function-normalize-space">normalize-space</a></b> function returns the argument 2945 string with whitespace normalized by stripping leading and trailing 2946 whitespace and replacing sequences of whitespace characters by a 2947 single space. Whitespace characters are the same as those allowed by the <a href="http://www.w3.org/TR/REC-xml#NT-S">S</a> production in XML. If the argument is 2948 omitted, it defaults to the context node converted to a string, in 2949 other words the <a href="#dt-string-value">string-value</a> 2950 of the context node. 2951 </p> 2952 2953 2954 <p><a name="function-translate"><b>Function: </b><i>string</i> <b>translate</b>(<i>string</i>, <i>string</i>, <i>string</i>) 2955 </a> 2956 </p> 2957 2958 2959 <p>The <b><a href="#function-translate">translate</a></b> function returns the first 2960 argument string with occurrences of characters in the second argument 2961 string replaced by the character at the corresponding position in the 2962 third argument string. For example, 2963 <code>translate("bar","abc","ABC")</code> returns the string 2964 <code>BAr</code>. If there is a character in the second argument 2965 string with no character at a corresponding position in the third 2966 argument string (because the second argument string is longer than the 2967 third argument string), then occurrences of that character in the 2968 first argument string are removed. For example, 2969 <code>translate("--aaa--","abc-","ABC")</code> returns 2970 <code>"AAA"</code>. If a character occurs more than once in the second 2971 argument string, then the first occurrence determines the replacement 2972 character. If the third argument string is longer than the second 2973 argument string, then excess characters are ignored. 2974 </p> 2975 2976 2977 <blockquote><b>NOTE: </b>The <b><a href="#function-translate">translate</a></b> function is not a sufficient 2978 solution for case conversion in all languages. A future version of 2979 XPath may provide additional functions for case conversion. 2980 </blockquote> 2981 2982 2983 2984 2985 2986 <h3><a name="section-Boolean-Functions"></a>4.3 Boolean Functions 2987 </h3> 2988 2989 2990 <p><a name="function-boolean"><b>Function: </b><i>boolean</i> <b>boolean</b>(<i>object</i>) 2991 </a> 2992 </p> 2993 2994 2995 <p>The <b><a href="#function-boolean">boolean</a></b> function converts its argument to a 2996 boolean as follows: 2997 </p> 2998 2999 3000 <ul> 3001 3002 3003 <li> 3004 <p>a number is true if and only if it is neither positive or 3005 negative zero nor NaN 3006 </p> 3007 </li> 3008 3009 3010 <li> 3011 <p>a node-set is true if and only if it is non-empty</p> 3012 </li> 3013 3014 3015 <li> 3016 <p>a string is true if and only if its length is non-zero</p> 3017 </li> 3018 3019 3020 <li> 3021 <p>an object of a type other than the four basic types is 3022 converted to a boolean in a way that is dependent on that 3023 type 3024 </p> 3025 </li> 3026 3027 3028 </ul> 3029 3030 3031 <p><a name="function-not"><b>Function: </b><i>boolean</i> <b>not</b>(<i>boolean</i>) 3032 </a> 3033 </p> 3034 3035 3036 <p>The <b><a href="#function-not">not</a></b> function returns true if its argument is 3037 false, and false otherwise. 3038 </p> 3039 3040 3041 <p><a name="function-true"><b>Function: </b><i>boolean</i> <b>true</b>() 3042 </a> 3043 </p> 3044 3045 3046 <p>The <b><a href="#function-true">true</a></b> function returns true. 3047 </p> 3048 3049 3050 <p><a name="function-false"><b>Function: </b><i>boolean</i> <b>false</b>() 3051 </a> 3052 </p> 3053 3054 3055 <p>The <b><a href="#function-false">false</a></b> function returns false. 3056 </p> 3057 3058 3059 <p><a name="function-lang"><b>Function: </b><i>boolean</i> <b>lang</b>(<i>string</i>) 3060 </a> 3061 </p> 3062 3063 3064 <p>The <b><a href="#function-lang">lang</a></b> function returns true or false depending on 3065 whether the language of the context node as specified by 3066 <code>xml:lang</code> attributes is the same as or is a sublanguage of 3067 the language specified by the argument string. The language of the 3068 context node is determined by the value of the <code>xml:lang</code> 3069 attribute on the context node, or, if the context node has no 3070 <code>xml:lang</code> attribute, by the value of the 3071 <code>xml:lang</code> attribute on the nearest ancestor of the context 3072 node that has an <code>xml:lang</code> attribute. If there is no such 3073 attribute, then <b><a href="#function-lang">lang</a></b> returns false. If there is such an 3074 attribute, then <b><a href="#function-lang">lang</a></b> returns true if the attribute 3075 value is equal to the argument ignoring case, or if there is some 3076 suffix starting with <code>-</code> such that the attribute value is 3077 equal to the argument ignoring that suffix of the attribute value and 3078 ignoring case. For example, <code>lang("en")</code> would return true 3079 if the context node is any of these five elements: 3080 </p> 3081 3082 <pre><para xml:lang="en"/> 3083<div xml:lang="en"><para/></div> 3084<para xml:lang="EN"/> 3085<para xml:lang="en-us"/></pre> 3086 3087 3088 3089 <h3><a name="section-Number-Functions"></a>4.4 Number Functions 3090 </h3> 3091 3092 3093 <p><a name="function-number"><b>Function: </b><i>number</i> <b>number</b>(<i>object</i>?) 3094 </a> 3095 </p> 3096 3097 3098 <p>The <b><a href="#function-number">number</a></b> function converts its argument to a 3099 number as follows: 3100 </p> 3101 3102 3103 <ul> 3104 3105 3106 <li> 3107 <p>a string that consists of optional whitespace followed by an 3108 optional minus sign followed by a <a href="#NT-Number">Number</a> 3109 followed by whitespace is converted to the IEEE 754 number that is 3110 nearest (according to the IEEE 754 round-to-nearest rule) 3111 to the mathematical value represented by the string; any other 3112 string is converted to NaN 3113 </p> 3114 </li> 3115 3116 3117 <li> 3118 <p>boolean true is converted to 1; boolean false is converted to 3119 0 3120 </p> 3121 </li> 3122 3123 3124 <li> 3125 3126 3127 <p>a node-set is first converted to a string as if by a call to the 3128 <b><a href="#function-string">string</a></b> function and then converted in the same way as a 3129 string argument 3130 </p> 3131 3132 3133 </li> 3134 3135 3136 <li> 3137 <p>an object of a type other than the four basic types is 3138 converted to a number in a way that is dependent on that 3139 type 3140 </p> 3141 </li> 3142 3143 3144 </ul> 3145 3146 3147 <p>If the argument is omitted, it defaults to a node-set with the 3148 context node as its only member. 3149 </p> 3150 3151 3152 <blockquote><b>NOTE: </b>The <b><a href="#function-number">number</a></b> function should not be used 3153 for conversion of numeric data occurring in an element in an XML 3154 document unless the element is of a type that represents numeric data 3155 in a language-neutral format (which would typically be transformed 3156 into a language-specific format for presentation to a user). In 3157 addition, the <b><a href="#function-number">number</a></b> function cannot be used 3158 unless the language-neutral format used by the element is consistent 3159 with the XPath syntax for a <a href="#NT-Number">Number</a>. 3160 </blockquote> 3161 3162 3163 <p><a name="function-sum"><b>Function: </b><i>number</i> <b>sum</b>(<i>node-set</i>) 3164 </a> 3165 </p> 3166 3167 3168 <p>The <b><a href="#function-sum">sum</a></b> function returns the sum, for each 3169 node in the argument node-set, of the result of converting the 3170 <a href="#dt-string-value">string-value</a>s of the node to 3171 a number. 3172 </p> 3173 3174 3175 <p><a name="function-floor"><b>Function: </b><i>number</i> <b>floor</b>(<i>number</i>) 3176 </a> 3177 </p> 3178 3179 3180 <p>The <b><a href="#function-floor">floor</a></b> function returns the largest (closest to 3181 positive infinity) number that is not greater than the argument and 3182 that is an integer. 3183 </p> 3184 3185 3186 <p><a name="function-ceiling"><b>Function: </b><i>number</i> <b>ceiling</b>(<i>number</i>) 3187 </a> 3188 </p> 3189 3190 3191 <p>The <b><a href="#function-ceiling">ceiling</a></b> function returns the smallest (closest 3192 to negative infinity) number that is not less than the argument and 3193 that is an integer. 3194 </p> 3195 3196 3197 <p><a name="function-round"><b>Function: </b><i>number</i> <b>round</b>(<i>number</i>) 3198 </a> 3199 </p> 3200 3201 3202 <p>The <b><a href="#function-round">round</a></b> function returns the number that is 3203 closest to the argument and that is an integer. If there are two such 3204 numbers, then the one that is closest to positive infinity is 3205 returned. If the argument is NaN, then NaN is returned. If the 3206 argument is positive infinity, then positive infinity is returned. If 3207 the argument is negative infinity, then negative infinity is 3208 returned. If the argument is positive zero, then positive zero is 3209 returned. If the argument is negative zero, then negative zero is 3210 returned. If the argument is less than zero, but greater than or 3211 equal to -0.5, then negative zero is returned. 3212 </p> 3213 3214 3215 <blockquote><b>NOTE: </b>For these last two cases, the result of calling the 3216 <b><a href="#function-round">round</a></b> function is not the same as the result of 3217 adding 0.5 and then calling the <b><a href="#function-floor">floor</a></b> 3218 function. 3219 </blockquote> 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 <h2><a name="data-model"></a>5 Data Model 3230 </h2> 3231 3232 3233 <p>XPath operates on an XML document as a tree. This section describes 3234 how XPath models an XML document as a tree. This model is conceptual 3235 only and does not mandate any particular implementation. The 3236 relationship of this model to the XML Information Set <a href="#XINFO">[XML Infoset]</a> is described in <a href="#infoset">[<b>B XML Information Set Mapping</b>] 3237 </a>. 3238 </p> 3239 3240 3241 <p>XML documents operated on by XPath must conform to the XML 3242 Namespaces Recommendation <a href="#XMLNAMES">[XML Names]</a>. 3243 </p> 3244 3245 3246 <p>The tree contains nodes. There are seven types of node:</p> 3247 3248 3249 <ul> 3250 3251 3252 <li> 3253 <p>root nodes</p> 3254 </li> 3255 3256 3257 <li> 3258 <p>element nodes</p> 3259 </li> 3260 3261 3262 <li> 3263 <p>text nodes</p> 3264 </li> 3265 3266 3267 <li> 3268 <p>attribute nodes</p> 3269 </li> 3270 3271 3272 <li> 3273 <p>namespace nodes</p> 3274 </li> 3275 3276 3277 <li> 3278 <p>processing instruction nodes</p> 3279 </li> 3280 3281 3282 <li> 3283 <p>comment nodes</p> 3284 </li> 3285 3286 3287 </ul> 3288 3289 3290 <p><a name="dt-string-value"></a>For every type of 3291 node, there is a way of determining a <b>string-value</b> for a 3292 node of that type. For some types of node, the string-value is part 3293 of the node; for other types of node, the string-value is computed 3294 from the string-value of descendant nodes. 3295 </p> 3296 3297 3298 <blockquote><b>NOTE: </b>For element nodes and root nodes, the string-value of a node 3299 is not the same as the string returned by the DOM 3300 <code>nodeValue</code> method (see <a href="#DOM">[DOM]</a>). 3301 </blockquote> 3302 3303 3304 <p><a name="dt-expanded-name"></a>Some types of 3305 node also have an <b>expanded-name</b>, which is a pair 3306 consisting of a local part and a namespace URI. The local part is a 3307 string. The namespace URI is either null or a string. The namespace 3308 URI specified in the XML document can be a URI reference as defined in 3309 <a href="#RFC2396">[RFC2396]</a>; this means it can have a fragment identifier 3310 and can be relative. A relative URI should be resolved into an 3311 absolute URI during namespace processing: the namespace URIs of 3312 <a href="#dt-expanded-name">expanded-name</a>s of nodes in 3313 the data model should be absolute. Two <a href="#dt-expanded-name">expanded-name</a>s are equal if they have 3314 the same local part, and either both have a null namespace URI or both 3315 have non-null namespace URIs that are equal. 3316 </p> 3317 3318 3319 <p><a name="dt-document-order"></a>There is an 3320 ordering, <b>document order</b>, defined on all the nodes in the 3321 document corresponding to the order in which the first character of 3322 the XML representation of each node occurs in the XML representation 3323 of the document after expansion of general entities. Thus, the root 3324 node will be the first node. Element nodes occur before their 3325 children. Thus, document order orders element nodes in order of the 3326 occurrence of their start-tag in the XML (after expansion of 3327 entities). The attribute nodes and namespace nodes of an element occur 3328 before the children of the element. The namespace nodes are defined 3329 to occur before the attribute nodes. The relative order of namespace 3330 nodes is implementation-dependent. The relative order of attribute 3331 nodes is implementation-dependent. <a name="dt-reverse-document-order"></a><b>Reverse document order</b> is the reverse of <a href="#dt-document-order">document order</a>. 3332 </p> 3333 3334 3335 <p>Root nodes and element nodes have an ordered list of child nodes. 3336 Nodes never share children: if one node is not the same node as 3337 another node, then none of the children of the one node will be the 3338 same node as any of the children of another node. <a name="dt-parent"></a>Every node other than the root node has 3339 exactly one <b>parent</b>, which is either an element node or 3340 the root node. A root node or an element node is the parent 3341 of each of its child nodes. <a name="dt-descendants"></a>The <b>descendants</b> of a node are the 3342 children of the node and the descendants of the children of the 3343 node. 3344 </p> 3345 3346 3347 3348 <h3><a name="root-node"></a>5.1 Root Node 3349 </h3> 3350 3351 3352 <p>The root node is the root of the tree. A root node does not occur 3353 except as the root of the tree. The element node for the document 3354 element is a child of the root node. The root node also has as 3355 children processing instruction and comment nodes for processing 3356 instructions and comments that occur in the prolog and after the end 3357 of the document element. 3358 </p> 3359 3360 3361 <p>The <a href="#dt-string-value">string-value</a> of the 3362 root node is the concatenation of the <a href="#dt-string-value">string-value</a>s of all text node 3363 <a href="#dt-descendants">descendants</a> of the root 3364 node in document order. 3365 </p> 3366 3367 3368 <p>The root node does not have an <a href="#dt-expanded-name">expanded-name</a>. 3369 </p> 3370 3371 3372 3373 3374 3375 <h3><a name="element-nodes"></a>5.2 Element Nodes 3376 </h3> 3377 3378 3379 <p>There is an element node for every element in the document. An 3380 element node has an <a href="#dt-expanded-name">expanded-name</a> computed by expanding 3381 the <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> of the element 3382 specified in the tag in accordance with the XML Namespaces 3383 Recommendation <a href="#XMLNAMES">[XML Names]</a>. The namespace URI of the 3384 element's <a href="#dt-expanded-name">expanded-name</a> will 3385 be null if the <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> has no 3386 prefix and there is no applicable default namespace. 3387 </p> 3388 3389 3390 <blockquote><b>NOTE: </b>In the notation of Appendix A.3 of <a href="#XMLNAMES">[XML Names]</a>, 3391 the local part of the expanded-name corresponds to the 3392 <code>type</code> attribute of the <code>ExpEType</code> element; the 3393 namespace URI of the expanded-name corresponds to the <code>ns</code> 3394 attribute of the <code>ExpEType</code> element, and is null if the 3395 <code>ns</code> attribute of the <code>ExpEType</code> element is 3396 omitted. 3397 </blockquote> 3398 3399 3400 <p>The children of an element node are the element nodes, comment 3401 nodes, processing instruction nodes and text nodes for its content. 3402 Entity references to both internal and external entities are expanded. 3403 Character references are resolved. 3404 </p> 3405 3406 3407 <p>The <a href="#dt-string-value">string-value</a> of an 3408 element node is the concatenation of the <a href="#dt-string-value">string-value</a>s of all text node 3409 <a href="#dt-descendants">descendants</a> of the element 3410 node in document order. 3411 </p> 3412 3413 3414 3415 <h4><a name="unique-id"></a>5.2.1 Unique IDs 3416 </h4> 3417 3418 3419 <p>An element node may have a unique identifier (ID). This is the 3420 value of the attribute that is declared in the DTD as type 3421 <code>ID</code>. No two elements in a document may have the same 3422 unique ID. If an XML processor reports two elements in a document as 3423 having the same unique ID (which is possible only if the document is 3424 invalid) then the second element in document order must be treated as 3425 not having a unique ID. 3426 </p> 3427 3428 3429 <blockquote><b>NOTE: </b>If a document does not have a DTD, then no element in the 3430 document will have a unique ID. 3431 </blockquote> 3432 3433 3434 3435 3436 3437 3438 3439 <h3><a name="attribute-nodes"></a>5.3 Attribute Nodes 3440 </h3> 3441 3442 3443 <p>Each element node has an associated set of attribute nodes; the 3444 element is the <a href="#dt-parent">parent</a> of each of 3445 these attribute nodes; however, an attribute node is not a child of 3446 its parent element. 3447 </p> 3448 3449 3450 <blockquote><b>NOTE: </b>This is different from the DOM, which does not treat the 3451 element bearing an attribute as the parent of the attribute (see 3452 <a href="#DOM">[DOM]</a>). 3453 </blockquote> 3454 3455 3456 <p>Elements never share attribute nodes: if one element node is not 3457 the same node as another element node, then none of the attribute 3458 nodes of the one element node will be the same node as the attribute 3459 nodes of another element node. 3460 </p> 3461 3462 3463 <blockquote><b>NOTE: </b>The <code>=</code> operator tests whether two nodes have the 3464 same value, <i>not</i> whether they are the same node. Thus 3465 attributes of two different elements may compare as equal using 3466 <code>=</code>, even though they are not the same node. 3467 </blockquote> 3468 3469 3470 <p>A defaulted attribute is treated the same as a specified attribute. 3471 If an attribute was declared for the element type in the DTD, but the 3472 default was declared as <code>#IMPLIED</code>, and the attribute was 3473 not specified on the element, then the element's attribute set does 3474 not contain a node for the attribute. 3475 </p> 3476 3477 3478 <p>Some attributes, such as <code>xml:lang</code> and 3479 <code>xml:space</code>, have the semantics that they apply to all 3480 elements that are descendants of the element bearing the attribute, 3481 unless overridden with an instance of the same attribute on another 3482 descendant element. However, this does not affect where attribute 3483 nodes appear in the tree: an element has attribute nodes only for 3484 attributes that were explicitly specified in the start-tag or 3485 empty-element tag of that element or that were explicitly declared in 3486 the DTD with a default value. 3487 </p> 3488 3489 3490 <p>An attribute node has an <a href="#dt-expanded-name">expanded-name</a> and a <a href="#dt-string-value">string-value</a>. The <a href="#dt-expanded-name">expanded-name</a> is computed by 3491 expanding the <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> specified in 3492 the tag in the XML document in accordance with the XML Namespaces 3493 Recommendation <a href="#XMLNAMES">[XML Names]</a>. The namespace URI of the 3494 attribute's name will be null if the <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> of the attribute does not have 3495 a prefix. 3496 </p> 3497 3498 3499 <blockquote><b>NOTE: </b>In the notation of Appendix A.3 of <a href="#XMLNAMES">[XML Names]</a>, 3500 the local part of the expanded-name corresponds to the 3501 <code>name</code> attribute of the <code>ExpAName</code> element; the 3502 namespace URI of the expanded-name corresponds to the <code>ns</code> 3503 attribute of the <code>ExpAName</code> element, and is null if the 3504 <code>ns</code> attribute of the <code>ExpAName</code> element is 3505 omitted. 3506 </blockquote> 3507 3508 3509 <p>An attribute node has a <a href="#dt-string-value">string-value</a>. The <a href="#dt-string-value">string-value</a> is the normalized value 3510 as specified by the XML Recommendation <a href="#XML">[XML]</a>. An 3511 attribute whose normalized value is a zero-length string is not 3512 treated specially: it results in an attribute node whose <a href="#dt-string-value">string-value</a> is a zero-length 3513 string. 3514 </p> 3515 3516 3517 <blockquote><b>NOTE: </b>It is possible for default attributes to be declared in an 3518 external DTD or an external parameter entity. The XML Recommendation 3519 does not require an XML processor to read an external DTD or an 3520 external parameter unless it is validating. A stylesheet or other facility that assumes 3521 that the XPath tree contains default attribute values declared in an 3522 external DTD or parameter entity may not work with some non-validating 3523 XML processors. 3524 </blockquote> 3525 3526 3527 <p>There are no attribute nodes corresponding to attributes that 3528 declare namespaces (see <a href="#XMLNAMES">[XML Names]</a>). 3529 </p> 3530 3531 3532 3533 3534 3535 <h3><a name="namespace-nodes"></a>5.4 Namespace Nodes 3536 </h3> 3537 3538 3539 <p>Each element has an associated set of namespace nodes, one for each 3540 distinct namespace prefix that is in scope for the element (including 3541 the <code>xml</code> prefix, which is implicitly declared by the XML 3542 Namespaces Recommendation <a href="#XMLNAMES">[XML Names]</a>) and one for 3543 the default namespace if one is in scope for the element. The element 3544 is the <a href="#dt-parent">parent</a> of each of these 3545 namespace nodes; however, a namespace node is not a child of 3546 its parent element. Elements never share namespace nodes: if one element 3547 node is not the same node as another element node, then none of the 3548 namespace nodes of the one element node will be the same node as the 3549 namespace nodes of another element node. This means that an element 3550 will have a namespace node: 3551 </p> 3552 3553 3554 <ul> 3555 3556 3557 <li> 3558 <p>for every attribute on the element whose name starts with 3559 <code>xmlns:</code>; 3560 </p> 3561 </li> 3562 3563 3564 <li> 3565 <p>for every attribute on an ancestor element whose name starts 3566 <code>xmlns:</code> unless the element itself or a nearer ancestor 3567 redeclares the prefix; 3568 </p> 3569 </li> 3570 3571 3572 <li> 3573 3574 3575 <p>for an <code>xmlns</code> attribute, if the element or some 3576 ancestor has an <code>xmlns</code> attribute, and the value of the 3577 <code>xmlns</code> attribute for the nearest such element is 3578 non-empty 3579 </p> 3580 3581 3582 <blockquote><b>NOTE: </b>An attribute <code>xmlns=""</code> "undeclares" 3583 the default namespace (see <a href="#XMLNAMES">[XML Names]</a>). 3584 </blockquote> 3585 3586 3587 </li> 3588 3589 3590 </ul> 3591 3592 3593 <p>A namespace node has an <a href="#dt-expanded-name">expanded-name</a>: the local part is 3594 the namespace prefix (this is empty if the namespace node is for the 3595 default namespace); the namespace URI is always null. 3596 </p> 3597 3598 3599 <p>The <a href="#dt-string-value">string-value</a> of a 3600 namespace node is the namespace URI that is being bound to the 3601 namespace prefix; if it is relative, it must be resolved just like a 3602 namespace URI in an <a href="#dt-expanded-name">expanded-name</a>. 3603 </p> 3604 3605 3606 3607 3608 3609 3610 <h3><a name="section-Processing-Instruction-Nodes"></a>5.5 Processing Instruction Nodes 3611 </h3> 3612 3613 3614 <p>There is a processing instruction node for every processing 3615 instruction, except for any processing instruction that occurs within 3616 the document type declaration. 3617 </p> 3618 3619 3620 <p>A processing instruction has an <a href="#dt-expanded-name">expanded-name</a>: the local part is 3621 the processing instruction's target; the namespace URI is null. The 3622 <a href="#dt-string-value">string-value</a> of a processing 3623 instruction node is the part of the processing instruction following 3624 the target and any whitespace. It does not include the terminating 3625 <code>?></code>. 3626 </p> 3627 3628 3629 <blockquote><b>NOTE: </b>The XML declaration is not a processing instruction. 3630 Therefore, there is no processing instruction node corresponding to the 3631 XML declaration. 3632 </blockquote> 3633 3634 3635 3636 3637 3638 <h3><a name="section-Comment-Nodes"></a>5.6 Comment Nodes 3639 </h3> 3640 3641 3642 <p>There is a comment node for every comment, except for any comment that 3643 occurs within the document type declaration. 3644 </p> 3645 3646 3647 <p>The <a href="#dt-string-value">string-value</a> of 3648 comment is the content of the comment not including the opening 3649 <code><!--</code> or the closing <code>--></code>. 3650 </p> 3651 3652 3653 <p>A comment node does not have an <a href="#dt-expanded-name">expanded-name</a>. 3654 </p> 3655 3656 3657 3658 3659 3660 <h3><a name="section-Text-Nodes"></a>5.7 Text Nodes 3661 </h3> 3662 3663 3664 <p>Character data is grouped into text nodes. As much character data 3665 as possible is grouped into each text node: a text node never has an 3666 immediately following or preceding sibling that is a text node. The 3667 <a href="#dt-string-value">string-value</a> of a text node 3668 is the character data. A text node always has at least one character 3669 of data. 3670 </p> 3671 3672 3673 <p>Each character within a CDATA section is treated as character data. 3674 Thus, <code><![CDATA[<]]></code> in the source document will 3675 treated the same as <code>&lt;</code>. Both will result in a 3676 single <code><</code> character in a text node in the tree. Thus, a 3677 CDATA section is treated as if the <code><![CDATA[</code> and 3678 <code>]]></code> were removed and every occurrence of 3679 <code><</code> and <code>&</code> were replaced by 3680 <code>&lt;</code> and <code>&amp;</code> respectively. 3681 </p> 3682 3683 3684 <blockquote><b>NOTE: </b>When a text node that contains a <code><</code> character 3685 is written out as XML, the <code><</code> character must be escaped 3686 by, for example, using <code>&lt;</code>, or including it in a 3687 CDATA section. 3688 </blockquote> 3689 3690 3691 <p>Characters inside comments, processing instructions and attribute 3692 values do not produce text nodes. Line-endings in external entities 3693 are normalized to #xA as specified in the XML Recommendation <a href="#XML">[XML]</a>. 3694 </p> 3695 3696 3697 <p>A text node does not have an <a href="#dt-expanded-name">expanded-name</a>. 3698 </p> 3699 3700 3701 3702 3703 3704 3705 3706 <h2><a name="section-Conformance"></a>6 Conformance 3707 </h2> 3708 3709 3710 <p>XPath is intended primarily as a component that can be used by 3711 other specifications. Therefore, XPath relies on specifications that 3712 use XPath (such as <a href="#XPTR">[XPointer]</a> and <a href="#XSLT">[XSLT]</a>) to 3713 specify criteria for conformance of implementations of XPath and does 3714 not define any conformance criteria for independent implementations of 3715 XPath. 3716 </p> 3717 3718 3719 3720 3721 3722 3723 <hr title="Separator from footer"> 3724 3725 3726 <h2><a name="section-References"></a>A References 3727 </h2> 3728 3729 3730 <h3><a name="section-Normative-References"></a>A.1 Normative References 3731 </h3> 3732 3733 3734 <dl> 3735 3736 3737 <dt><a name="IEEE754">IEEE 754</a></dt> 3738 <dd>Institute of Electrical and 3739 Electronics Engineers. <i>IEEE Standard for Binary Floating-Point 3740 Arithmetic 3741 </i>. ANSI/IEEE Std 754-1985. 3742 </dd> 3743 3744 3745 <dt><a name="RFC2396">RFC2396</a></dt> 3746 <dd>T. Berners-Lee, R. Fielding, and 3747 L. Masinter. <i>Uniform Resource Identifiers (URI): Generic 3748 Syntax 3749 </i>. IETF RFC 2396. See <a href="http://www.ietf.org/rfc/rfc2396.txt">http://www.ietf.org/rfc/rfc2396.txt</a>. 3750 </dd> 3751 3752 3753 <dt><a name="XML">XML</a></dt> 3754 <dd>World Wide Web Consortium. <i>Extensible 3755 Markup Language (XML) 1.0. 3756 </i> W3C Recommendation. See <a href="http://www.w3.org/TR/1998/REC-xml-19980210">http://www.w3.org/TR/1998/REC-xml-19980210</a></dd> 3757 3758 3759 <dt><a name="XMLNAMES">XML Names</a></dt> 3760 <dd>World Wide Web 3761 Consortium. <i>Namespaces in XML.</i> W3C Recommendation. See 3762 <a href="http://www.w3.org/TR/REC-xml-names">http://www.w3.org/TR/REC-xml-names</a></dd> 3763 3764 3765 </dl> 3766 3767 3768 3769 <h3><a name="section-Other-References"></a>A.2 Other References 3770 </h3> 3771 3772 3773 <dl> 3774 3775 3776 <dt><a name="CHARMOD">Character Model</a></dt> 3777 <dd>World Wide Web Consortium. 3778 <i>Character Model for the World Wide Web.</i> W3C Working 3779 Draft. See <a href="http://www.w3.org/TR/WD-charmod">http://www.w3.org/TR/WD-charmod</a></dd> 3780 3781 3782 <dt><a name="DOM">DOM</a></dt> 3783 <dd>World Wide Web Consortium. <i>Document 3784 Object Model (DOM) Level 1 Specification. 3785 </i> W3C 3786 Recommendation. See <a href="http://www.w3.org/TR/REC-DOM-Level-1">http://www.w3.org/TR/REC-DOM-Level-1</a></dd> 3787 3788 3789 <dt><a name="JLS">JLS</a></dt> 3790 <dd>J. Gosling, B. Joy, and G. Steele. <i>The 3791 Java Language Specification 3792 </i>. See <a href="http://java.sun.com/docs/books/jls/index.html">http://java.sun.com/docs/books/jls/index.html</a>. 3793 </dd> 3794 3795 3796 <dt><a name="ISO10646">ISO/IEC 10646</a></dt> 3797 <dd>ISO (International 3798 Organization for Standardization). <i>ISO/IEC 10646-1:1993, 3799 Information technology -- Universal Multiple-Octet Coded Character Set 3800 (UCS) -- Part 1: Architecture and Basic Multilingual Plane 3801 </i>. 3802 International Standard. See <a href="http://www.iso.ch/cate/d18741.html">http://www.iso.ch/cate/d18741.html</a>. 3803 </dd> 3804 3805 3806 <dt><a name="TEI">TEI</a></dt> 3807 <dd>C.M. Sperberg-McQueen, L. Burnard 3808 <i>Guidelines for Electronic Text Encoding and 3809 Interchange 3810 </i>. See <a href="http://etext.virginia.edu/TEI.html">http://etext.virginia.edu/TEI.html</a>. 3811 </dd> 3812 3813 3814 <dt><a name="UNICODE">Unicode</a></dt> 3815 <dd>Unicode Consortium. <i>The Unicode 3816 Standard 3817 </i>. See <a href="http://www.unicode.org/unicode/standard/standard.html">http://www.unicode.org/unicode/standard/standard.html</a>. 3818 </dd> 3819 3820 3821 <dt><a name="XINFO">XML Infoset</a></dt> 3822 <dd>World Wide Web 3823 Consortium. <i>XML Information Set.</i> W3C Working Draft. See 3824 <a href="http://www.w3.org/TR/xml-infoset">http://www.w3.org/TR/xml-infoset</a> 3825 3826 </dd> 3827 3828 3829 <dt><a name="XPTR">XPointer</a></dt> 3830 <dd>World Wide Web Consortium. <i>XML 3831 Pointer Language (XPointer). 3832 </i> W3C Working Draft. See <a href="http://www.w3.org/TR/WD-xptr">http://www.w3.org/TR/WD-xptr</a></dd> 3833 3834 3835 <dt><a name="XQL">XQL</a></dt> 3836 <dd>J. Robie, J. Lapp, D. Schach. 3837 <i>XML Query Language (XQL)</i>. See 3838 <a href="http://www.w3.org/TandS/QL/QL98/pp/xql.html">http://www.w3.org/TandS/QL/QL98/pp/xql.html</a></dd> 3839 3840 3841 <dt><a name="XSLT">XSLT</a></dt> 3842 <dd>World Wide Web Consortium. <i>XSL 3843 Transformations (XSLT). 3844 </i> W3C Recommendation. See <a href="http://www.w3.org/TR/xslt">http://www.w3.org/TR/xslt</a></dd> 3845 3846 3847 </dl> 3848 3849 3850 3851 3852 3853 3854 <h2><a name="infoset"></a>B XML Information Set Mapping (Non-Normative) 3855 </h2> 3856 3857 3858 <p>The nodes in the XPath data model can be derived from the 3859 information items provided by the XML Information Set <a href="#XINFO">[XML Infoset]</a> as follows: 3860 </p> 3861 3862 3863 <blockquote><b>NOTE: </b>A new version of the XML Information Set Working Draft, which 3864 will replace the May 17 version, was close to completion at the time 3865 when the preparation of this version of XPath was completed and was 3866 expected to be released at the same time or shortly after the release 3867 of this version of XPath. The mapping is given for this new version 3868 of the XML Information Set Working Draft. If the new version of the 3869 XML Information Set Working has not yet been released, W3C members may 3870 consult the internal Working Group version <a href="http://www.w3.org/XML/Group/1999/09/WD-xml-infoset-19990915.html"> 3871 http://www.w3.org/XML/Group/1999/09/WD-xml-infoset-19990915.html 3872 </a> 3873 (<a href="http://cgi.w3.org/MemberAccess/">members 3874 only 3875 </a>). 3876 </blockquote> 3877 3878 3879 <ul> 3880 3881 3882 <li> 3883 <p>The root node comes from the document information item. The 3884 children of the root node come from the <i>children</i> and <i>children - comments</i> 3885 properties. 3886 </p> 3887 </li> 3888 3889 3890 <li> 3891 <p>An element node comes from an element information item. The 3892 children of an element node come from the <i>children</i> and <i>children - comments</i> properties. The 3893 attributes of an element node come from the <i>attributes</i> property. The namespaces 3894 of an element node come from the <i>in-scope namespaces</i> property. The 3895 local part of the <a href="#dt-expanded-name">expanded-name</a> of the element node 3896 comes from the <i>local name</i> 3897 property. The namespace URI of the <a href="#dt-expanded-name">expanded-name</a> of the element node 3898 comes from the <i>namespace URI</i> 3899 property. The unique ID of the element node comes from the <i>children</i> property of the attribute 3900 information item in the <i>attributes</i> property that has an <i>attribute type</i> property equal to 3901 <code>ID</code>. 3902 </p> 3903 </li> 3904 3905 3906 <li> 3907 <p>An attribute node comes from an attribute information item. 3908 The local part of the <a href="#dt-expanded-name">expanded-name</a> of the attribute node 3909 comes from the <i>local name</i> 3910 property. The namespace URI of the <a href="#dt-expanded-name">expanded-name</a> of the attribute node 3911 comes from the <i>namespace URI</i> 3912 property. The <a href="#dt-string-value">string-value</a> of 3913 the node comes from concatenating the <i>character code</i> property of each member 3914 of the <i>children</i> 3915 property. 3916 </p> 3917 </li> 3918 3919 3920 <li> 3921 <p>A text node comes from a sequence of one or more consecutive 3922 character information items. The <a href="#dt-string-value">string-value</a> of the node comes from 3923 concatenating the <i>character code</i> 3924 property of each of the character information items. 3925 </p> 3926 </li> 3927 3928 3929 <li> 3930 <p>A processing instruction node comes from a processing 3931 instruction information item. The local part of the <a href="#dt-expanded-name">expanded-name</a> of the node comes from 3932 the <i>target</i> property. (The 3933 namespace URI part of the <a href="#dt-expanded-name">expanded-name</a> of the node is null.) 3934 The <a href="#dt-string-value">string-value</a> of the node 3935 comes from the <i>content</i> 3936 property. There are no processing instruction nodes for processing 3937 instruction items that are children of document type declaration 3938 information item. 3939 </p> 3940 </li> 3941 3942 3943 <li> 3944 <p>A comment node comes from a comment information item. The 3945 <a href="#dt-string-value">string-value</a> of the node 3946 comes from the <i>content</i> property. 3947 There are no comment nodes for comment information items that are 3948 children of document type declaration information item. 3949 </p> 3950 </li> 3951 3952 3953 <li> 3954 <p>A namespace node comes from a namespace declaration 3955 information item. The local part of the <a href="#dt-expanded-name">expanded-name</a> of the node comes from 3956 the <i>prefix</i> property. (The 3957 namespace URI part of the <a href="#dt-expanded-name">expanded-name</a> of the node is null.) 3958 The <a href="#dt-string-value">string-value</a> of the node 3959 comes from the <i>namespace URI</i> 3960 property. 3961 </p> 3962 </li> 3963 3964 3965 </ul> 3966 3967 3968 3969 3970 3971 </body> 3972</html>