Xpath Quick Reference
Total Page:16
File Type:pdf, Size:1020Kb
3243chAppA.qxd 6/20/05 10:34 AM Page 697 APPENDIX A ■ ■ ■ XPath Quick Reference This appendix gives you a quick guide to XPath, including its syntax and a guide to all the functions that are available to you. Sequences Every XPath expression returns a sequence, which can be empty or hold any number of items. Items can be nodes or atomic values. Nodes Nodes can be of the kinds shown in Table A-1. Table A-1. Node Kinds Node Kind Name String Value Document - Concatenation of all text nodes in the document Element Element name Depends on element type Attribute Attribute name Depends on attribute type Text - Text Comment - Text held within the comment Processing instruction Processing instruction target Text in the processing instruction after the space after the target name Namespace Namespace prefix Namespace URI Node Types Element and attribute nodes can be annotated with a type based on validation against a schema. The types, string values, and typed values of element and attribute nodes are summarized in Table A-2. 697 3243chAppA.qxd 6/20/05 10:34 AM Page 698 698 APPENDIX A ■ XPATH QUICK REFERENCE Table A-2. Node Types Node Kind Validity Type String Value Typed Value Element Unvalidated xdt:untyped Concatenation String value as of all text node xdt:untypedAtomic descendants value Element Invalid or xdt:anyType Concatenation String value as partially validated of all text node xdt:untypedAtomic descendants value Element Valid Simple Text with Sequence of items of whitespace appropriate type normalized based on type Element Valid Mixed Concatenation String value as of all text node xdt:untypedAtomic descendants value Element Valid Element-only Concatenation of all Undefined text node descendants Attribute Unvalidated xdt:untypedAtomic Value with String value as whitespace xdt:untypedAtomic replaced value Attribute Invalid or xdt:untypedAtomic Value with String value as partially validated whitespace xdt:untypedAtomic replaced value Attribute Valid Simple Value with Sequence of items of whitespace appropriate type normalized based on type Atomic Values Atomic values in Basic XSLT can be of any of the atomic types shown in Table A-3. The relevant namespace URIs are • http://www.w3.org/2001/XMLSchema (xs prefix by convention) • http://www.w3.org/2005/04/xpath-datatypes (xdt prefix by convention) ■Caution The namespace for XPath datatypes changes with each version of the spec. Basic XSLT processors may support other, implementation-defined types as well as those given in Table A-3. 3243chAppA.qxd 6/20/05 10:34 AM Page 699 APPENDIX A ■ XPATH QUICK REFERENCE 699 Table A-3. Atomic Types in Basic XSLT Atomic Type Representation Description xs:string The string itself A sequence of XML characters xs:boolean 'true', 'false', '1', true or false or '0' xs:decimal Any number of digits, with A decimal number, with size optional fractional part limits being implementation- defined xs:integer Any number of digits An integer, with size limits being implementation-defined xs:double As xs:decimal, but with optional A floating-point number: exponent, 'NaN', 'INF', or '-INF' a double-precision 64-bit format IEEE 754 value xs:float As xs:double A floating-point number: a single-precision 32-bit format IEEE 754 value xs:date YYYY-MM-DD A date: year, month, day, and optional timezone xs:time hh:mm:ss A time: hours, minutes, seconds, and optional timezone xs:dateTime YYYY-MM-DDThh:mm:ss A date and time: year, month, day, hours, minutes, seconds, and optional timezone xs:gYear YYYY A year with optional timezone xs:gYearMonth YYYY-MM A year and month with optional timezone xs:gMonth --MM A month with optional timezone xs:gMonthDay --MM-DD A month and day with optional timezone xs:gDay ---DD A day with optional timezone xs:duration PyYmMdDThHmMsS A duration of years, months, days, hours, minutes, and seconds xdt:dayTimeDuration PdDThHmMsS A duration of days, hours, minutes, and seconds xdt:yearMonthDuration PyYmM A duration of years and months xs:QName prefix:local-name or just A qualified name: local-name; namespace must a namespace URI and a local be declared name xs:anyURI URI syntax A URI xs:hexBinary Hex-encoded octets Binary data xs:base64Binary Base64-encoded octets Binary data xdt:untypedAtomic Any string An atomic value whose type is not known, and can be cast implicitly to another type 3243chAppA.qxd 6/20/05 10:34 AM Page 700 700 APPENDIX A ■ XPATH QUICK REFERENCE In Schema-Aware XSLT processors, all the atomic types from XML Schema and XPath are built in. These are shown in Figure A-1. You may also import types from a schema using <xsl:import-schema>. xdt:anyAtomicType xs:dateTime xs:date xs:time xs:gYear xs:gYearMonth xs:gMonth xs:gMonthDay xs:gDay xs:boolean xs:hexBinary xs:base64Binary xs:anyURI xs:QName xs:NOTATION xs:duration xs:decimal xs:float xs:double xdt:untypedAtomic xs:string xdt:yearMonthDuration xdt:dayTimeDuration xs:integer xs:normalizedString xs:long xs:nonPositiveInteger xs:nonNegativeInteger xs:token xs:int xs:negativeInteger xs:positiveInteger xs:unsignedLong xs:language xs:Name xs:NMTOKEN xs:short xs:unsignedInt xs:NCName xs:byte xs:unsignedShort xs:ID xs:IDREF xs:ENTITY xs:unsignedByte Key: primitive type XSD type XPath type Figure A-1. The hierarchy of built-in types Atomic values can be cast to other types using constructor functions—the name of the constructor function is the same as the name of the type—or the cast expression. Casting is allowed between the following primitive types: • xs:string and xdt:untypedAtomic values can be cast to any other type (though the cast won’t always succeed), with the exceptions of xs:QName and xs:NOTATION. • Values of numeric types (xs:decimal, xs:float, and xs:double) can be cast to other numeric types, though casts from xs:double and xs:float to xs:decimal won’t always succeed. • Values of numeric types can be cast to xs:boolean and vice versa. • xs:dateTime values can be cast to any of the date/time types. • xs:date values can be cast to xs:dateTime, xs:gYear, xs:gYearMonth, xs:gMonth, xs:gMonthDay, and xs:gDay types. • Values of the binary types (xs:hexBinary and xs:base64Binary) can be cast to each other. Whether a cast will succeed or not can be tested using the castable expression. 3243chAppA.qxd 6/20/05 10:34 AM Page 701 APPENDIX A ■ XPATH QUICK REFERENCE 701 ■Note Literal strings can be cast to xs:QName and to subtypes of xs:NOTATION. Effective Boolean Value Within tests, a value (equivalent to a single-item sequence) will be converted to its effective Boolean value, as shown in Table A-4. Table A-4. Effective Boolean Values Value Effective Boolean Value Node true String true if the string contains any characters, false otherwise Number false if number is 0 or NaN, true otherwise Boolean true if true, false if false Other atomic value Raises an error The effective Boolean value of a sequence is false if it is empty and true if the first item in the sequence is a node. Otherwise, if the sequence contains more than one item, you get an error. Sequence Types Sequence types are patterns that match sequences. Sequence types are summarized in Table A-5. Table A-5. Sequence Types Sequence Type Description void() Matches the empty sequence itemType Matches a sequence containing a single item itemType? Matches a sequence that contains 0 or 1 items itemType+ Matches a sequence that contains 1 or more items itemType* Matches a sequence that contains any number of items Sequence types may use an item type to specify the type of the items in the sequence. Item types are summarized in Table A-6. Table A-6. Item Types Item Type Description item() Matches any item atomicType Matches items of the named atomic type (see Table A-3) nodeKindTest Matches items that are nodes that match the node kind test (see Table A-7) 3243chAppA.qxd 6/20/05 10:34 AM Page 702 702 APPENDIX A ■ XPATH QUICK REFERENCE Node kind tests are used to test nodes. Node kind tests are summarized in Table A-7. Table A-7. Node Kind Tests Node Kind Node Test Description Nodes node() Matches or selects all kinds of nodes Document nodes document-node() Matches or selects all document nodes document-node(element(name)) Matches or selects document nodes with a single document element with a particular name document-node(element(*, type)) Matches or selects document nodes with a single document element with a particular type document-node(element(name, type)) Matches or selects document nodes with a single document element with a particular name and type document-node(schema-element(name)) Matches or selects document nodes with a single document element that matches a global element declaration within a schema Text text() Elements element() Matches or selects all elements element(name) Matches or selects elements with a particular name element(*, type) Matches or selects elements with a particular type element(name, type) Matches or selects elements with a particular name and type schema-element(name) Matches or selects elements based on a global element declaration within a schema Attributes attribute() Matches or selects all attributes attribute(name) Matches or selects attributes with a particular name attribute(*, type) Matches or selects attributes with a particular type attribute(name, type) Matches or selects attributes with a particular name and type schema-attribute(name) Matches or selects attributes based on a global attribute declaration within a schema Comments comment() Processing processing-instruction() Matches or selects all processing Instructions instructions processing-instruction(target) Matches or selects processing instructions with a particular target 3243chAppA.qxd 6/20/05 10:34 AM Page 703 APPENDIX A ■ XPATH QUICK REFERENCE 703 Paths Paths are made up of a number of steps, separated by /s.