### XPath Abbreviated Path Examples Source: https://www.w3.org/TR/xpath/20 Various examples demonstrating the use of abbreviated syntax for selecting nodes and attributes. ```XPath para ``` ```XPath * ``` ```XPath text() ``` ```XPath @name ``` ```XPath @* ``` ```XPath para[1] ``` ```XPath para[fn:last()] ``` ```XPath */para ``` ```XPath /book/chapter[5]/section[2] ``` ```XPath chapter//para ``` ```XPath //para ``` ```XPath //@version ``` ```XPath //list/member ``` ```XPath .//para ``` ```XPath .. ``` ```XPath ../@lang ``` ```XPath para[@type="warning"] ``` ```XPath para[@type="warning"][5] ``` ```XPath para[5][@type="warning"] ``` ```XPath chapter[title="Introduction"] ``` ```XPath chapter[title] ``` ```XPath employee[@secretary and @assistant] ``` ```XPath book/(chapter|appendix)/section ``` ```XPath E/. ``` -------------------------------- ### XPath MapTest Examples Source: https://www.w3.org/TR/xpath/-31 Provides examples of using MapTest expressions to check map instances against different types. Demonstrates type checking for keys and values. ```xpath $M instance of map(*) $M instance of map(xs:integer, xs:string) $M instance of map(xs:decimal, xs:anyAtomicType) not($M instance of map(xs:int, xs:string)) not($M instance of map(xs:integer, xs:token)) ``` -------------------------------- ### XPath 3.0 EQName Examples Source: https://www.w3.org/TR/xpath/-30 Provides examples of EQNames in XPath 3.0, illustrating lexical QNames with and without prefixes, and URIQualifiedNames using BracedURILiterals. ```xpath pi math:pi Q{http://www.w3.org/2005/xpath-functions/math}pi ``` -------------------------------- ### XPath Node Tests Examples Source: https://www.w3.org/TR/xpath/-10 Demonstrates various XPath node tests for selecting elements, attributes, text, comments, and processing instructions. Includes examples with QNames, wildcards, and specific node types. ```xpath child::para attribute::href child::* attribute::* NCName:* child::text() comment() processing-instruction() processing-instruction('data') node() ``` -------------------------------- ### XPath 3.0 FunctionTest Examples Source: https://www.w3.org/TR/xpath/-30 Provides examples of FunctionTests in XPath 3.0, covering any function and typed functions with specific argument and return types. ```xpath FunctionTest ::= AnyFunctionTest | TypedFunctionTest ``` ```xpath AnyFunctionTest ::= "function" "(" "*" ")" ``` ```xpath TypedFunctionTest ::= "function" "(" (SequenceType ("," SequenceType)*)? ")" "as" SequenceType ``` ```xpath FunctionTest ::= "function(*)" ``` ```xpath FunctionTest ::= "function(node()) as xs:string*" ``` ```xpath FunctionTest ::= "(function(node()) as xs:string)*" ``` -------------------------------- ### XPath Kind Test Examples Source: https://www.w3.org/TR/xpath/-31 Provides examples of Kind Tests in XPath, used to select nodes based on their type or name and type. ```XPath node() text() comment() namespace-node() element() schema-element(person) element(person) element(person, surgeon) element(*, surgeon) attribute() attribute(price) attribute(*, xs:decimal) document-node() ``` -------------------------------- ### XPath 3.0 Node Tests Examples Source: https://www.w3.org/TR/xpath/-30 Provides examples of various specific node tests in XPath 3.0, including PITest, CommentTest, TextTest, NamespaceNodeTest, and AnyKindTest. ```xpath PITest ::= "processing-instruction" ("(" (NCName | StringLiteral)? ")")? ``` ```xpath CommentTest ::= "comment" "(" ")" ``` ```xpath TextTest ::= "text" "(" ")" ``` ```xpath NamespaceNodeTest ::= "namespace-node" "(" ")" ``` ```xpath AnyKindTest ::= "node" "(" ")" ``` -------------------------------- ### XPath Type Promotion vs. Subtype Substitution Example Source: https://www.w3.org/TR/xpath/20 Illustrates the difference between type promotion and subtype substitution in XPath using function parameter examples. Type promotion involves conversion, while subtype substitution retains the original type. ```xpath-type-concepts A function that expects a parameter `$p` of type `xs:float` can be invoked with a value of type `xs:decimal`. This is an example of type promotion. The value is actually converted to the expected type. Within the body of the function, `$p instance of xs:decimal` returns `false`. A function that expects a parameter `$p` of type `xs:decimal` can be invoked with a value of type `xs:integer`. This is an example of subtype substitution. The value retains its original type. Within the body of the function, `$p instance of xs:integer` returns `true`. ``` -------------------------------- ### XPath Conditional Expression Example (Comparison) Source: https://www.w3.org/TR/xpath/-30 An example of an XPath conditional expression that compares unit costs and returns one of two widgets based on the result. ```XPath if ($widget1/unit-cost < $widget2/unit-cost) then $widget1 else $widget2 ``` -------------------------------- ### XPath Basic Path Expressions Source: https://www.w3.org/TR/xpath/-31 Examples of basic path expressions selecting elements and attributes. ```xpath para ``` ```xpath * ``` ```xpath text() ``` ```xpath @name ``` ```xpath @* ``` -------------------------------- ### XPath Node Selection Examples Source: https://www.w3.org/TR/xpath/-3 Provides various examples of selecting nodes using abbreviated syntax, including selecting all elements ('*'), text nodes ('text()'), and specific children by index or function. ```xpath * ``` ```xpath text() ``` ```xpath para[1] ``` ```xpath para[fn:last()] ``` ```xpath */para ``` ```xpath /book/chapter[5]/section[2] ``` ```xpath para[@type="warning"][5] ``` ```xpath para[5][@type="warning"] ``` ```xpath chapter[title="Introduction"] ``` ```xpath chapter[title] ``` ```xpath employee[@secretary and @assistant] ``` ```xpath book/(chapter|appendix)/section ``` -------------------------------- ### XPath Static Type Inference Examples Source: https://www.w3.org/TR/xpath/-30 Examples illustrating the inferred static types for various XPath expressions. These examples show how different operations and variable types affect the resulting static type. ```xpath concat(a,b) ``` ```xpath $a = $v ``` ```xpath $s[exp] ``` ```xpath data($x) ``` -------------------------------- ### XPath Sequence Combination Examples Source: https://www.w3.org/TR/xpath/20 Illustrates the usage of XPath's union, intersect, and except operators with sample node sequences. ```xpath $seq1 union $seq2 ``` ```xpath $seq2 union $seq3 ``` ```xpath $seq1 intersect $seq2 ``` ```xpath $seq2 intersect $seq3 ``` ```xpath $seq1 except $seq2 ``` ```xpath $seq2 except $seq3 ``` -------------------------------- ### XPath Location Step Example with Predicate Source: https://www.w3.org/TR/xpath/TR/1999/REC-xpath-19991116 Provides an example of an XPath location step, highlighting the axis ('child'), node test ('para'), and a predicate ('[position()=1]') used to filter the selected nodes. ```xpath child::para[position()=1] ``` -------------------------------- ### XPath General Item Matching Source: https://www.w3.org/TR/xpath/20 Shows examples of general item matching using `item()`, `node()`, `text()`, and `comment()` in XPath. ```xpath item() matches any single item. ``` ```xpath node() matches any node. ``` ```xpath text() matches any text node. ``` ```xpath comment() matches any comment node. ``` -------------------------------- ### XPath Abbreviated Syntax Examples Source: https://www.w3.org/TR/xpath/-10 Demonstrates various location paths using the abbreviated syntax in XPath, showcasing selections of elements, attributes, and their relationships. ```xpath para ``` ```xpath * ``` ```xpath text() ``` ```xpath @name ``` ```xpath @* ``` ```xpath para[1] ``` ```xpath para[last()] ``` ```xpath */para ``` ```xpath /doc/chapter[5]/section[2] ``` ```xpath chapter//para ``` ```xpath //para ``` ```xpath //olist/item ``` ```xpath . ``` ```xpath .//para ``` ```xpath .. ``` ```xpath ../@lang ``` ```xpath para[@type="warning"] ``` ```xpath para[@type="warning"][5] ``` ```xpath para[5][@type="warning"] ``` ```xpath chapter[title="Introduction"] ``` ```xpath chapter[title] ``` ```xpath employee[@secretary and @assistant] ``` -------------------------------- ### XPath Conditional Expression Example (Attribute Existence) Source: https://www.w3.org/TR/xpath/-30 An example of an XPath conditional expression that checks for the existence of a 'discounted' attribute and returns wholesale or retail price accordingly. ```XPath if ($part/@discounted) then $part/wholesale else $part/retail ``` -------------------------------- ### XPath ArrayTest Examples Source: https://www.w3.org/TR/xpath/-3 Provides examples of ArrayTest in XPath, showing how `array(*)` matches any array and `array(X)` matches arrays where all members conform to SequenceType X. ```XPath [ 1, 2 ] instance array(*) [] instance of array(xs:string) [ "foo" ] instance of array(xs:string) [ "foo" ] instance of array(xs:integer) [(1,2),(3,4)] instance of array(xs:integer) [(1,2),(3,4)] instance of array(xs:integer+) ``` -------------------------------- ### XPath 3.0 AtomicOrUnionType Example Source: https://www.w3.org/TR/xpath/-30 Shows an example of an AtomicOrUnionType in XPath 3.0, referring to a named atomic or union type. ```xpath AtomicOrUnionType ::= EQName ``` ```xpath AtomicOrUnionType ::= "xs:date" ``` -------------------------------- ### XPath Date/Time Lexical to Component Conversion Examples Source: https://www.w3.org/TR/xpath/-datamodel-30 Demonstrates how different lexical representations of XPath date and time types are converted into their internal component-based storage. This includes examples with and without timezone information, and normalization of time values. ```xpath Lexical: "2003-01-02T11:30:00-05:00" Stored: {2003, 1, 2, 11, 30, 0.0, -PT05H00M} Lexical: "2003-01-16T16:30:00" Stored: {2003, 1, 16, 16, 30, 0, ()} Lexical: "---30+10:30" Stored: {(), (), 30, (), (), (), PT10H30M} Lexical: "24:00:00" Stored (as xs:time): {(), (), (), 0, 0, 0.0, ()} Stored (as xs:dateTime): {2000, 1, 1, 0, 0, 0.0, ()} ``` -------------------------------- ### XPath Path Expression Examples Source: https://www.w3.org/TR/xpath/20 Illustrates common XPath path expressions, including the use of wildcards and the selection of elements based on relationships. ```xpath child::div1/child::para ``` ```xpath /* ``` ```xpath / * ``` ```xpath (/) * 5 ``` ```xpath 4 + (/) * 5 ``` ```xpath 4 + / ``` -------------------------------- ### XPath Name Test Examples Source: https://www.w3.org/TR/xpath/20 Demonstrates the usage of name tests in XPath, including wildcards and qualified names for selecting elements and attributes. ```XPath child::para ``` ```XPath attribute::abc:href ``` ```XPath child::* ``` ```XPath attribute::* ``` ```XPath NCName:* ``` ```XPath *:NCName ``` -------------------------------- ### XPath Dynamic Function Call Examples Source: https://www.w3.org/TR/xpath/-30 Illustrates dynamic function calls in XPath, where a variable holds a function reference. Examples show passing arguments, accessing sequence items as functions, and combining with predicates. ```xpath $f(2, 3) ``` ```xpath $f[2]("Hi there") ``` ```xpath $f()[2] ``` -------------------------------- ### XPath Arithmetic Examples: Division and Subtraction Source: https://www.w3.org/TR/xpath/-30 Provides examples of arithmetic operations in XPath, including integer and floating-point division, subtraction of date values resulting in a duration, and subtraction for price calculations. ```xpath -3 div 2 ``` ```xpath -3 idiv 2 ``` ```xpath $emp/hiredate - $emp/birthdate ``` ```xpath $unit-price - $unit-discount ``` -------------------------------- ### XPath Function Coercion Examples for Maps and Arrays Source: https://www.w3.org/TR/xpath/-31 The specification now includes examples clarifying how function coercion applies to maps and arrays in XPath. This aids developers in understanding and implementing correct type conversions. ```xpath map(?) ``` ```xpath array(?) ``` -------------------------------- ### XPath 1.0 Limitations: Quantification Examples Source: https://www.w3.org/TR/xpath/20req Illustrates limitations in XPath 1.0 regarding 'for any' and 'for all' comparisons, which XPath 2.0 aims to address. These examples show scenarios where XPath 1.0's implicit quantification differs from explicit requirements. ```XPath 5 > $ns ``` ```XPath $ns1 > $ns2 ``` ```XPath 5 = $ns ``` ```XPath $ns1 = $ns2 ``` ```XPath 0 + LineItem/@UnitPrice > 40 ``` ```XPath LineItem/@UnitPrice > 40 ``` -------------------------------- ### XPath Relative Location Path Example Source: https://www.w3.org/TR/xpath/TR/1999/REC-xpath-19991116 Demonstrates a relative location path in XPath, showing how steps are composed using the '/' separator. This example selects 'para' elements that are grandchildren of the context node and have 'div' parents. ```xpath child::div/child::para ``` -------------------------------- ### XPath SequenceType Examples Source: https://www.w3.org/TR/xpath/20 Illustrates common usage patterns for XPath SequenceTypes, including atomic types, node occurrences, and specific element/attribute tests. ```xpath * `xs:date` refers to the built-in atomic schema type named `xs:date` ``` ```xpath * `attribute()?` refers to an optional attribute node ``` ```xpath * `element()` refers to any element node ``` ```xpath * `element(po:shipto, po:address)` refers to an element node that has the name `po:shipto` and has the type annotation `po:address` (or a schema type derived from `po:address`) ``` ```xpath * `element(*, po:address)` refers to an element node of any name that has the type annotation `po:address` (or a type derived from `po:address`) ``` ```xpath * `element(customer)` refers to an element node named `customer` with any type annotation ``` ```xpath * `schema-element(customer)` refers to an element node whose name is `customer` (or is in the substitution group headed by `customer`) and whose type annotation matches the schema type declared for a `customer` element in the in-scope element declarations ``` ```xpath * `node()*` refers to a sequence of zero or more nodes of any kind ``` ```xpath * `item()+` refers to a sequence of one or more nodes or atomic values ``` -------------------------------- ### XPath 3.1 Binary Operator Mapping Examples Source: https://www.w3.org/TR/xpath/-3 Provides examples of binary operator mappings in XPath 3.1, including numeric addition and subtraction, as well as date and duration arithmetic. It specifies the function used for each operation and the resulting data type. ```XPath Binary Operators Operator | Type(A) | Type(B) | Function | Result type ---|---|---|---|--- A + B | numeric | numeric | op:numeric-add(A, B) | numeric A + B | xs:date | xs:yearMonthDuration | op:add-yearMonthDuration-to-date(A, B) | xs:date A + B | xs:yearMonthDuration | xs:date | op:add-yearMonthDuration-to-date(B, A) | xs:date A + B | xs:date | xs:dayTimeDuration | op:add-dayTimeDuration-to-date(A, B) | xs:date A + B | xs:dayTimeDuration | xs:date | op:add-dayTimeDuration-to-date(B, A) | xs:date A + B | xs:time | xs:dayTimeDuration | op:add-dayTimeDuration-to-time(A, B) | xs:time A + B | xs:dayTimeDuration | xs:time | op:add-dayTimeDuration-to-time(B, A) | xs:time A + B | xs:dateTime | xs:yearMonthDuration | op:add-yearMonthDuration-to-dateTime(A, B) | xs:dateTime A + B | xs:yearMonthDuration | xs:dateTime | op:add-yearMonthDuration-to-dateTime(B, A) | xs:dateTime A + B | xs:dateTime | xs:dayTimeDuration | op:add-dayTimeDuration-to-dateTime(A, B) | xs:dateTime A + B | xs:dayTimeDuration | xs:dateTime | op:add-dayTimeDuration-to-dateTime(B, A) | xs:dateTime A + B | xs:yearMonthDuration | xs:yearMonthDuration | op:add-yearMonthDurations(A, B) | xs:yearMonthDuration A + B | xs:dayTimeDuration | xs:dayTimeDuration | op:add-dayTimeDurations(A, B) | xs:dayTimeDuration A - B | numeric | numeric | op:numeric-subtract(A, B) | numeric A - B | xs:date | xs:date | op:subtract-dates(A, B) | xs:dayTimeDuration ``` -------------------------------- ### XPath Name Test Examples Source: https://www.w3.org/TR/xpath/-30 Demonstrates the usage of NameTests in XPath, including selecting nodes by QName, wildcards with prefixes, and URILiterals. ```xpath child::para ``` ```xpath attribute::abc:href ``` ```xpath child::* ``` ```xpath attribute::* ``` ```xpath element::prefix:* ``` ```xpath element::{http://example.com/msg}* ``` ```xpath element::*:localname ``` -------------------------------- ### XPath Expression Type Inferencing Examples Source: https://www.w3.org/TR/xpath/-31 Examples illustrating how static types are inferred for various XPath expressions during the static analysis phase. This includes string concatenation, boolean comparisons, and sequence filtering. ```xpath concat(a,b) -> xs:string ``` ```xpath $a = $v -> xs:boolean ``` ```xpath $s[exp] -> same item type as $s, allows empty sequence ``` ```xpath data($x) -> depends on static type of $x; e.g., element(*, xs:integer) -> xs:integer ``` -------------------------------- ### XPath Variable Reference Example Source: https://www.w3.org/TR/xpath/20 Demonstrates how to reference a variable in XPath. A variable reference is a QName preceded by a $-sign. ```XPath $variableName ``` -------------------------------- ### XPath Kind Test Examples Source: https://www.w3.org/TR/xpath/20 Illustrates various kind tests in XPath for selecting nodes based on their type, name, and schema information. ```XPath node() ``` ```XPath text() ``` ```XPath comment() ``` ```XPath element() ``` ```XPath schema-element(person) ``` ```XPath element(person) ``` ```XPath element(person, surgeon) ``` ```XPath element(*, surgeon) ``` ```XPath attribute() ``` ```XPath attribute(price) ``` ```XPath attribute(*, xs:decimal) ``` ```XPath document-node() ``` ```XPath document-node(element(book)) ``` -------------------------------- ### XPath Complex Path Expressions Source: https://www.w3.org/TR/xpath/-31 Examples of more complex path expressions involving multiple steps, axes, and unions. ```xpath */para ``` ```xpath /book/chapter[5]/section[2] ``` ```xpath chapter//para ``` ```xpath //para ``` ```xpath //@version ``` ```xpath //list/member ``` ```xpath book/(chapter|appendix)/section ``` -------------------------------- ### XPath 3.0 Named Function Reference Examples Source: https://www.w3.org/TR/xpath/-30 Illustrates how to reference named functions in XPath 3.0, showing examples with built-in functions like 'fn:abs' and custom functions like 'local:myfunc', specifying their names and arity. ```XPath fn:abs#1 ``` ```XPath fn:concat#5 ``` ```XPath local:myfunc#2 ``` -------------------------------- ### XPath Atomic Type Construction (Examples) Source: https://www.w3.org/TR/xpath/20 Demonstrates constructing atomic values of various XML Schema types using their respective constructor functions. ```xpath xs:integer("12") ``` ```xpath xs:date("2001-08-25") ``` ```xpath xs:dayTimeDuration("PT5H") ``` -------------------------------- ### Whitespace Handling Examples in XPath Source: https://www.w3.org/TR/xpath/20 Illustrates the impact of whitespace on XPath expression parsing, showing how it separates terminals or is included within QNames. ```xpath Example 1: `foo- foo` (syntax error) Example 2: `foo -foo` (equivalent to `foo - foo`) Example 3: `foo(: This is a comment :)- foo` (equivalent to `foo - foo`) Example 4: `foo-foo` (single QName) Example 5: `10div 3` (syntax error) Example 6: `10 div3` (syntax error) Example 7: `10div3` (syntax error) ``` -------------------------------- ### XPath Absolute Path Abbreviation: Root Node Source: https://www.w3.org/TR/xpath/-31 Describes the abbreviation for absolute paths starting with '/'. This selects nodes starting from the root of the tree containing the context item. ```XPath "(fn:root(self::node()) treat as document-node())/" ``` -------------------------------- ### XPath 3.0 Sequence Examples Source: https://www.w3.org/TR/xpath/-30 Illustrates how to construct sequence types in XPath 3.0 using item types and occurrence indicators. ```xpath SequenceType ::= "empty-sequence" "(" ")" | (ItemType OccurrenceIndicator?) ``` ```xpath SequenceType ::= "node()*" ``` ```xpath SequenceType ::= "item()+" ``` -------------------------------- ### XPath 3.0 DocumentTest Example Source: https://www.w3.org/TR/xpath/-30 Shows the syntax for a DocumentTest in XPath 3.0, which matches document nodes. ```xpath DocumentTest ::= "document-node" "(" (ElementTest | SchemaElementTest)? ")" ``` -------------------------------- ### Example XML Document for Data Modeling Source: https://www.w3.org/TR/xpath/-datamodel-3 This XML document serves as a non-normative example to demonstrate data modeling concepts. It includes elements for catalog items like t-shirts and albums, featuring attributes such as code, label, price, and related information. It utilizes namespaces for various XML extensions. ```xml Staind: Been Awhile Tee Black (1-sided) Lyrics from the hit song 'It's Been Awhile' are shown in white, beneath the large 'Flock & Weld' Staind logo. 25.00 It's Been A While 10.99 Staind ``` -------------------------------- ### XPath Comment Example Source: https://www.w3.org/TR/xpath/20 An example of a valid comment in an XPath expression, delimited by '(: ... :)'. Comments are ignored during expression processing. ```xpath (: Houston, we have a problem :) ``` -------------------------------- ### XPath 3.1 SequenceType Examples Source: https://www.w3.org/TR/xpath/-31 Illustrates various common SequenceTypes used in XPath 3.1, demonstrating different item types and occurrence indicators. ```xpath xs:date ``` ```xpath attribute()? ``` ```xpath element() ``` ```xpath element(po:shipto, po:address) ``` ```xpath element(*, po:address) ``` ```xpath element(customer) ``` ```xpath schema-element(customer) ``` ```xpath node()* ``` ```xpath item()+ ``` ```xpath function(*) ``` -------------------------------- ### Get Element Description Type Name Source: https://www.w3.org/TR/xpath/-datamodel-30 Shows how to get the type name of an element named 'description' using dm:type-name. ```xpath dm:type-name(E4) | = | cat:description ``` -------------------------------- ### XPath Function Call Examples Source: https://www.w3.org/TR/xpath/20 Illustrates various ways to structure function calls in XPath, including calls with multiple arguments, arguments that are sequences, empty sequences, and zero arguments. ```xpath my:three-argument-function(1, 2, 3) ``` ```xpath my:two-argument-function((1, 2), 3) ``` ```xpath my:two-argument-function(1, ()) ``` ```xpath my:one-argument-function((1, 2, 3)) ``` ```xpath my:one-argument-function(( )) ``` ```xpath my:zero-argument-function( ) ``` -------------------------------- ### Whitespace Handling Examples in XPath Source: https://www.w3.org/TR/xpath/-30 Illustrates the impact of whitespace and comments on parsing XPath expressions, including scenarios with QNames and operators. ```xpath foo- foo // Syntax error: '-' is part of QName 'foo-' foo -foo // Syntactically equivalent to 'foo - foo', two QNames separated by subtraction operator. foo(: This is a comment :)- foo // Syntactically equivalent to 'foo - foo', comment separates adjacent terminals. foo-foo // Syntactically equivalent to a single QName, '-' is a valid character in a QName. // To use '-' as an operator, it must be separated by whitespace or parentheses, e.g., 'foo - foo'. 10div 3 // Syntax error: 'div' is not separated from '10'. 10 div3 // Syntax error: 'div' is not separated from '3'. 10div3 // Syntax error: 'div' is not separated from '10' or '3'. ``` -------------------------------- ### XPath Atomic Type Matching Examples Source: https://www.w3.org/TR/xpath/-30 Provides examples of matching atomic values in XPath using specific ItemTypes like xs:decimal and union types. The matching logic depends on whether the actual type derives from the specified ItemType. ```XPath xs:decimal ``` ```XPath dress-size ``` -------------------------------- ### XPath Node Tests Examples Source: https://www.w3.org/TR/xpath/TR/1999/REC-xpath-19991116 Demonstrates various XPath node tests for selecting elements, attributes, text, comments, and processing instructions. Covers QNames, wildcards (* and NCName:*), and specific node types. ```xpath child::para ``` ```xpath attribute::href ``` ```xpath child::* ``` ```xpath attribute::* ``` ```xpath child::text() ``` ```xpath comment() ``` ```xpath processing-instruction() ``` ```xpath processing-instruction('xml-stylesheet') ``` ```xpath node() ``` -------------------------------- ### XPath Kind Test Examples Source: https://www.w3.org/TR/xpath/-30 Illustrates various KindTests in XPath for selecting nodes based on their type, name, and schema information. ```xpath node() ``` ```xpath text() ``` ```xpath comment() ``` ```xpath namespace-node() ``` ```xpath element() ``` ```xpath schema-element(person) ``` ```xpath element(person) ``` ```xpath element(person, surgeon) ``` ```xpath element(*, surgeon) ``` ```xpath attribute() ``` ```xpath attribute(price) ``` ```xpath attribute(*, xs:decimal) ``` ```xpath document-node() ``` ```xpath document-node(element(book)) ``` -------------------------------- ### XPath Name Test Examples Source: https://www.w3.org/TR/xpath/-31 Demonstrates various forms of Name Tests in XPath, including wildcards, specific element names, and namespace-qualified names. ```XPath child::para attribute::abc:href child::* attribute::* NCName:* *:NCName Q{http://example.com/msg}* ``` -------------------------------- ### Get Element Type Name Source: https://www.w3.org/TR/xpath/-datamodel-30 Illustrates how to get the type name of an element using dm:type-name. This indicates the schema type associated with the element. ```xpath dm:type-name(E3) | = | xs:token ``` -------------------------------- ### Get Attribute Type Name Source: https://www.w3.org/TR/xpath/-datamodel-30 Illustrates how to get the type name of an attribute using dm:type-name. This indicates the schema type associated with the attribute. ```xpath dm:type-name(A7) | = | cat:sizeList ``` -------------------------------- ### XPath String Literal Examples Source: https://www.w3.org/TR/xpath/20 Demonstrates the syntax for string literals in XPath, including escaping of internal quotes. ```xpath "12.5" ``` ```xpath "He said, ""I don't like it.""" ``` -------------------------------- ### XPath: Get Base URI of Element E3 Source: https://www.w3.org/TR/xpath/-datamodel-3 Retrieves the base URI of an element identified as E3. This uses the dm:base-uri() function to get the document's URI. ```XPath dm:base-uri(E3) ``` -------------------------------- ### XPath: Get Node Name of Attribute A7 Source: https://www.w3.org/TR/xpath/-datamodel-3 Retrieves the qualified name of an attribute identified as A7. This uses the dm:node-name() function to get the node's name. ```XPath dm:node-name(A7) ``` -------------------------------- ### XPath Absolute Path Expression - Root Node Source: https://www.w3.org/TR/xpath/-3 Shows the expansion of a path expression starting with '/'. This selects nodes starting from the root of the tree containing the context item. ```xpath "/" : (fn:root(self::node()) treat as document-node())/ ``` -------------------------------- ### XPath Numeric Literal Examples Source: https://www.w3.org/TR/xpath/20 Illustrates different numeric literal formats in XPath, representing integers, decimals, and doubles. ```xpath 12 ``` ```xpath 12.5 ``` ```xpath 125E2 ``` -------------------------------- ### XPath 2.0 Type Error Example (Substring) Source: https://www.w3.org/TR/xpath/20 This example highlights that XPath 2.0 prevents implicit type conversions, such as applying the substring function to a date, which would result in a type error. ```xpath substring(date_attribute, 1, 4) ``` -------------------------------- ### XPath Static Function Call Examples Source: https://www.w3.org/TR/xpath/-3 Illustrates various static function call syntaxes in XPath, including calls with multiple arguments, sequences as arguments, empty sequences, and zero arguments. ```xpath my:three-argument-function(1, 2, 3) ``` ```xpath my:two-argument-function((1, 2), 3) ``` ```xpath my:two-argument-function(1, ()) ``` ```xpath my:one-argument-function((1, 2, 3)) ``` ```xpath my:one-argument-function(( )) ``` ```xpath my:zero-argument-function() ``` -------------------------------- ### XPath: Select 'chapter' with 'title' child equal to 'Introduction' Source: https://www.w3.org/TR/xpath/20 Selects 'chapter' children of the context node that have one or more 'title' children whose typed value is equal to the string 'Introduction'. Uses a nested path within a predicate. ```xpath child::chapter[child::title = 'Introduction'] ``` -------------------------------- ### XPath Let Expression Example Source: https://www.w3.org/TR/xpath/-30 Demonstrates a let expression with multiple variable bindings, where the second binding references a variable from the first binding. ```XPath let $x := doc('a.xml')/*, $y := $x//* return $y[@value gt $x/@min] ``` -------------------------------- ### Get Text Node Type Name Source: https://www.w3.org/TR/xpath/-datamodel-30 Illustrates how to get the type name of a text node using dm:type-name. Text nodes are typically of type xs:untypedAtomic. ```xpath dm:type-name(T1) | = | xs:untypedAtomic ``` -------------------------------- ### Type Promotion Example: Function Call with xs:decimal to xs:float Source: https://www.w3.org/TR/xpath/-31 Demonstrates type promotion where an xs:decimal value is passed to a function expecting an xs:float. The value is converted, and the original type is not retained within the function. ```XQuery declare function local:process-float( $p as xs:float ) as xs:boolean { $p instance of xs:decimal }; let $value := xs:decimal("10.5") return local:process-float($value) ``` -------------------------------- ### QNames and NOTATIONS Source: https://www.w3.org/TR/xpath/-datamodel-30 Explains the handling of QNames and NOTATIONS in the XPath Data Model, including their lexical and value spaces, namespace bindings, and consistency constraints. ```APIDOC ## QNames and NOTATIONS ### Description The `QName` and `NOTATION` data types require special attention. The following sections apply to `xs:QName`, `xs:NOTATION`, and types derived from them. These types are referred to collectively as “qualified names”. As defined in XML Schema, the lexical space for qualified names includes a local name and an optional namespace prefix. The value space for qualified names contains a local name and an optional namespace URI. Therefore, it is not possible to derive a lexical value from the typed value, or vice versa, without access to some context that defines the namespace bindings. When qualified names exist as values of nodes in a well-formed document, it is always possible to determine such a namespace context. However, the data model also allows qualified names to exist as freestanding atomic values, or as the name or value of a parentless attribute node, and in these cases no namespace context is available. In this Data Model, therefore, the value space for qualified names contains a local-name, an optional namespace URI, and an optional prefix. The prefix is used only when producing a lexical representation of the value, that is, when casting the value to a string. The prefix plays no part in other operations involving qualified names: in particular, two qualified names are equal if their local names and namespace URIs match, regardless whether they have the same prefix. ### Consistency Constraints * If the namespace URI of a qualified name is absent, then the prefix must also be absent. * For every element node whose name has a prefix, the prefix must be one that has a binding to the namespace URI of the element name in the namespaces property of the element. * For every element node whose name has no prefix, the element must have a a binding for the empty prefix to the namespace URI of the element name, or must have no binding for the empty prefix in the case where the name of the element has no namespace URI. * For every attribute node whose name has a prefix, the attribute node must either be parentless, or the prefix must be one that has a binding to the namespace URI of the attribute name in the namespaces property of the parent element. * For every qualified name that contains a prefix and that is included in the typed value of an element node, or of an attribute node that has an element node as its parent, the prefix must be one that is bound to the namespace URI of the qualified name in the namespaces property of that element. * For every qualified name that contains a namespace URI and no prefix, and that is included in the typed value of an element node, or of an attribute node that has an element node as its parent, that element node must have a binding for the empty prefix to that namespace URI in its namespace property. * For every qualified name that contains neither a namespace URI nor a prefix, and that is included in the typed value of an element node, or of an attribute node that has an element node as its parent, that node must not have a binding for the empty prefix. * No qualified name that contains a prefix may be included in the typed value of an attribute node that has no parent. ``` -------------------------------- ### Get Element Namespace Bindings Source: https://www.w3.org/TR/xpath/-datamodel-30 Shows how to retrieve the namespace bindings (prefix and URI) for an element using dm:namespace-bindings. ```xpath dm:namespace-bindings(E3) | = | ("xml", "http://www.w3.org/XML/1998/namespace", "html", "http://www.w3.org/1999/xhtml", "", "http://www.example.com/catalog", "xlink", "http://www.w3.org/1999/xlink", "xsi", "http://www.w3.org/2001/XMLSchema-instance") ``` -------------------------------- ### XPath 3.0 Grammar Productions Example Source: https://www.w3.org/TR/xpath/-30 Demonstrates the EBNF notation used for defining XPath 3.0 grammar productions, specifically for a static function call and its argument list. ```text [59] | `FunctionCall` | ::= | `EQName ArgumentList` [49] | `ArgumentList` | ::= | `"(" (Argument ("," Argument)*)? ")"` ``` -------------------------------- ### XPath 3.0 SchemaElementTest Example Source: https://www.w3.org/TR/xpath/-30 Demonstrates the syntax for a SchemaElementTest in XPath 3.0, which matches element nodes based on schema declarations. ```xpath SchemaElementTest ::= "schema-element" "(" ElementDeclaration ")" ``` ```xpath SchemaElementTest ::= "schema-element(customer)" ``` -------------------------------- ### XPath: Sequence of Inline Functions Example Source: https://www.w3.org/TR/xpath/-30 Generates a sequence where each item is a function that captures and returns a node from the default collection. ```xpath collection()/(let $a := . return function() { $a }) ``` -------------------------------- ### Example of Nested Array Atomization in XPath Source: https://www.w3.org/TR/xpath/-3 Provides an example of atomizing a nested array in XPath, showing how the fn:data function recursively processes array elements to produce a single sequence of atomic values. ```xpath Atomizing the array [ [1, 2, 3], [4, 5, 6] ] results in the sequence (1, 2, 3, 4, 5, 6). ``` -------------------------------- ### XPath Any Kind Test Syntax Source: https://www.w3.org/TR/xpath/-31 Provides the syntax for the AnyKindTest, which matches any node type. ```xpath "node" (" ") ``` -------------------------------- ### Function Test Examples Source: https://www.w3.org/TR/xpath/-3 Demonstrates various forms of FunctionTest for matching functions. Includes matching any function, functions with specific argument and return types, and functions with wildcard types. ```XPath function(*) ``` ```XPath function(int, int) as int ``` ```XPath function(xs:anyAtomicType) as item()* ``` ```XPath function(xs:integer) as item()* ``` -------------------------------- ### XPath Predicate Expressions Source: https://www.w3.org/TR/xpath/-31 Examples of using predicates to filter nodes based on position, function calls, or attribute values. ```xpath para[1] ``` ```xpath para[fn:last()] ``` ```xpath para[@type="warning"][5] ``` ```xpath para[5][@type="warning"] ``` ```xpath chapter[title="Introduction"] ``` ```xpath chapter[title] ``` ```xpath employee[@secretary and @assistant] ``` -------------------------------- ### XPath 3.0 AttributeTest Examples Source: https://www.w3.org/TR/xpath/-30 Shows the syntax for AttributeTests in XPath 3.0, including tests for attribute names, wildcards, and type annotations. ```xpath AttributeTest ::= "attribute" "(" (AttribNameOrWildcard ("," TypeName)? )? ")" ``` ```xpath AttributeTest ::= "attribute()?" ``` -------------------------------- ### XPath 2.0 Explicit Type Conversion Example Source: https://www.w3.org/TR/xpath/20 XPath 2.0 requires explicit type conversion for function arguments, unlike XPath 1.0's implicit conversions. This example shows how to convert a number to a string for the `substring-before` function. ```xpath substring-before(string(10 div 3), ".") ``` -------------------------------- ### XPath Type Error Example (Statically Reportable) Source: https://www.w3.org/TR/xpath/-3 This XPath example illustrates a type error that can be reported during the static analysis phase. The expression attempts to multiply a string by an integer, which is a type mismatch, even if the 'else' branch might be executed. ```xpath if (empty($arg)) then "cat" * 2 else 0 ```