XSLT Can Be One of the Following Five Types
Total Page:16
File Type:pdf, Size:1020Kb
XPath Quick Reference Node Types Nodes can be of the following types: NodeType Name String Value root Concatenation of alI text nodes in the document element Element name Concatenation of alI text nodes descended from the element attribute Attribute name Attribute value text Text cornrnent Text held within the comment processing Processing Text in the processing instruction after the space after the instruction target targetname narnespace Namespace prefix Namespace URI Object Types Values in XSLT can be one of the following five types: Object Type Description boolean true or false number A floating-point number: a double-precision 64-bit format IEEE 754 value Table continued on following page Appendix A Object Type Description string A sequence of XML characters node set An unordered collection of nodes result tree fragment The root node of a generated node tree The various object types can be converted to each other by the following rules. Object Boolean Number String boolean 1 if the boolean is , true' if the boolean is true, O if the true, 'false' if the boolean is false boolean is false number false ifthe A string version of the number is O or number with no leading or NaN, otherwise trailing zeros; 'NaN' if the true number is NaN, , Infinity' if the number is infinity, and '-Infinity' ifthe number is negative infinity string false ifthe The number held string is an empty by the string, as string, otherwise long as it is a valid true XPath number (which doesn't include exponents or grouping separators) ; otherwise NaN node set true if the node The result of The string value of the first set holds nodes; converting the node in the node set false ifthe string value of the node set is empty first node in the node set to a number result tree true The result of The string value of the re suit fragment converting the tree fragment string value of the re suIt tree fragment to a number Booleans, numbers, and strings cannot be converted to node sets or result tree fragments. 694 XPath Quick Reference Paths Paths are made up of a number of steps, separated by / s. Each step is made up of an optional axis (defaults to child), a node test, and any number of predicates (held in square brackets). Axes Axes dictate the relationship between the context node and the selected nodes. The direction of an axis determines how the posi tion () of a node is calculated - forward axes look at document order, reverse axes look at reverse document order. Axis Direction Description self Forward Selects the context node itself ehild Forward Selects the children of the context node parent Reverse Selects the parent of the context node attribute Forward Selects the attributes of the context node deseendant Forward Selects the descendants of the context node at any level deseendant- Forward Selects the descendants of the context node, and the or-self context node itself aneestor Reverse Selects the ancestors of the context node aneestor-or-self Reverse Selects the ancestors of the context node, and the context node itself following-sibling Forward Selects the siblings of the context node that follow the context node in document order preeeding-sibling Reverse Selects the siblings of the context node that precede the context node in document order following Forward Selects the nodes (asi de from attribute and namespace nodes) that follow the context node in document order and that are not descendants of the context node preeeding Reverse Selects the nodes (asi de from attribute and namespace nodes) that precede the context node in document order and that are not ancestors of the context node namespaee Forward Selects the namespace nodes on the context node 695 Appendix A Node Tests Node tests match different kinds of nodes as follows: Node Test Description node() Matches nodes of all types * Matches elements with any name (or attributes if you're using the attribute axis) name Matches elements with the given name (or attribute if you're using the attribute axis) text( ) Matches text nodes comment () Matches comment nodes processing-instruction() Matches processing instruction nodes processing Matches processing instruction nodes with the given instruction('target') target Abbreviated Syntax You can use several abbreviations in XPath expressions, as described in the following table. Abbreviation Full Equivalent Description self: :node() The context node parent: :node () The parent of the context node II /descendant-or SuppIies quick access to descendants of the context self: :node () / node @ attribute: : Shortens expressions that access attributes Operators The operators in XPath can be spIit into four groups: o Logical operators O Comparisons O Numerical operators O N ode set operators 696 XPath Quick Reference The precedence of the operators (from lowest to highest) is as follows: 1. or 2. and 3. =, != 4. <=,<,>=,> 5. +, - 6. *,div,mod 7. unary - 8. Logical Operators Operator Description or true if either operand is true when converted to booleans; the right operand is not evaluated if the Ieft operand is true and true if both operands are true when converted to booleans; the right operand is not evaluated ifthe Ieft operand is false Comparisons Operator Description Equal to != Not equal to < Less than <= Less than or equal to > Greater than >= Greater than or equal to Comparisons between node sets and values are true if the comparison is true for the string value of any node in the node set. 697 Appendix A The numeric comparisons «, <=, >, and >=) can only be used to do numeric comparisons - you can't use them to test whether one string is alphabetically less than another string. Numerical Operators Operator Description + Addition Subtraction * Multiplication div Floating-point division mod Remainder from integer division unary - Negation Node Set Operator Operator Description Creates a union of two node sets Functions XPath and XSLT together define the following functions: boolean() Syntax: boolean boolean (object) See also: string (), number () Converts the argument to a boolean value (either true or false). Usually this is done automatically, for example in test attributes. ceiling() Syntax: number ceiling (number) See also: floor (), round () Rounds the argument up to the nearest integer. For negative numbers, this means rounding nearer to 0, so ceiling (-1. 5) is -1. 698 XPath Quick Reference concat() Syntax: string concat (string, string+) Concatenates the strings passed as arguments into a single string. contains() Syntax: boolean contains (string, string) See also: starts-with () Returns true if the string passed as the first argument contains the string passed as the second argument or if the second argument is an empty string, and false otherwise. count() Syntax: number count (node-set) See also: last ( ), posi tion (), <xsl: number> Counts the number of nodes in the node set passed as the argument. current() Syntax: node-set current ( ) See also: the path . Returns the current node - the node that's current1y being processed within the <xsl: for-each> or <xsl: template> as opposed to the context node, which is the node currently being looked at within an XPath. document() Syntax: node-set document (object, node-set?) If the first argument is a string, returns the node found at the URL specified by that string (usualIy the root node of a document). The URL is resolved relative to the location of the file containing the first node in the node set passed as the second argument (which is usualIy used to resolve the URL relative to the source document). If the second argument is- missing, the URL is resolved relative to the stylesheet itself. If the first argument is a node set, then the result is a node set containing alI the nodes that are retrieved by calling the document () function with the string values of the nodes in the node set. element-available( ) Syntax: boolean element-available (string) See also: function-available ( ), <xsl: fallback> 699 Appendix A Returns true if the processor supports the extension element or new XSLT instruction named by the string passed as the argument. The string should be a qualified name, with a prefix. false() Syntax: boolean false ( ) See also: true ( ), boolean ( ) Returns false. floorO Syntax: number floor (number) See also: ceiling (), round () Rounds the number down to the nearest integer. If the number is negative, then it gets rounded away from zero, so floor (-1.5) returns -2. format-number( ) Syntax: string format-number (number, string, string?) See also: number (), <xsl: decimal-format>, <xsl: number> Returns the number passed as the first argument formatted according to the format pattern passed as the second argument. The format pattern usually contains # to mean an optional digit, O to indicate a required digit, . to indicate the decimal point, and , to indicate a grouping separator. For example, format-number (1234.5, '#, ##0.00' ) returns 1,234.50. If the third argument is specified, it gives the name of a decimal format, declared with the <xsl: decimal-format> element, which can de fine other characters to be used instead of #, O, " and function-avai lable() Syntax: boolean function-available (string) See also: element-available ( ) Returns true if the processor supports the function named by the argument string. The string usually includes a prefix to test for the availability of extension functions. generate-id( ) Syntax: string generate-id (node-set?) Returns an ID for the first node in the node set passed as the argument which is both unique and a valid XML ID. If no argument is given, it returns a unique ID for the context node. The unique IDs are not stable - they are different from processor to processor, and even for the same processor operating over the same stylesheet with the same source XML document.