What is the difference between xpath and xquery
It is based on a tree representation of the XML document, and selects nodes by a variety of criteria. In popular use, an XPath expression is often referred to simply as an XPath.
Silvester Welch. Yet No Comments. Gerald Daniel. Notify me of followup comments via e-mail. Written by : jeyakanth. User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.
It has the following features that are used for the transformation of XML data: Side effect free. Strongly typed. High level. It has its own library defining standard functions. It is a major component of XSLT.
Other differences between XPath and XQuery: 1. It defines a Query collection of XML documents. It takes XML has a data model to express complex queries. XQuery is also case-sensitive to retrieve information from the XML file. It retrieves both tree structure and tabular data. Very easy to parse and type with XML Language. They are a read-only language and rely on path expression to traverse the hierarchical document. And the Path expressions are based on XPath 1. Parsed like regular expressions.
The Query is done in an orderly fashion, and therefore the nodes are returned in order. See, for example, the two signatures for fn:string. In the first signature, the parameter is omitted and the argument defaults to the context item, referred to as.
In the second signature, the argument must be present but may be the empty sequence, written as. Some functions accept a sequence of zero or more values as an argument. The sequence may contain zero or more items of the named type. For example, the function below accepts a sequence of xs:double and returns a xs:double or the empty sequence.
As a matter of convention, a number of functions defined in this document take a parameter whose value is a map, defining options controlling the detail of how the function is evaluated.
Maps are a new datatype introduced in XPath 3. For example, the function fn:xml-to-json has an options parameter allowing specification of whether the output is to be indented. A call might be written:. These are referred to as the option parameter conventions.
These rules apply only to functions that explicitly refer to them. The value of the relevant argument must be a map. The entries in the map are referred to as options: the key of the entry is called the option name, and the associated value is the option value. Option names defined in this specification are always strings single xs:string values.
Option values may be of any type. Although option names are described above as strings, the actual key may be any value that compares equal to the required string using the eq operator with Unicode codepoint collation; or equivalently, the op:same-key relation. For example, instances of xs:untypedAtomic or xs:anyURI are equally acceptable. It is not an error if the options map contains options with names other than those described in this specification.
Implementations that define additional options in this way should use values of type xs:QName as the option names, using an appropriate namespace. All entries in the options map are optional, and supplying an empty map has the same effect as omitting the relevant argument in the function call, assuming this is permitted. For each named option, the function specification defines a required type for the option value.
The value that is actually supplied in the map is converted to this required type using the function conversion rules XP A type error also occurs if this conversion delivers a coerced function whose invocation fails with a type error. A dynamic error occurs if the supplied value after conversion is not one of the permitted values for the option in question: the error codes for this error are defined in the specification of each function.
It is the responsibility of each function implementation to invoke this conversion; it does not happen automatically as a consequence of the function calling rules. In cases where an option is list-valued, by convention the value may be supplied either as a sequence or as an array.
Accepting a sequence is convenient if the value is generated programmatically using an XPath expression; while accepting an array allows the options to be held in an external file in JSON format, to be read using a call on the fn:json-doc function.
In cases where the value of an option is itself a map, the specification of the particular function must indicate whether or not these rules apply recursively to the contents of that map. The diagrams in this section show how nodes, functions, primitive simple types, and user defined types fit together into a type system. This type system comprises two distinct subsystems that both include the primitive atomic types. In the diagrams, connecting lines represent relationships between derived types and the types from which they are derived; the arrowheads point toward the type from which they are derived.
The dashed line represents relationships not present in this diagram, but that appear in one of the other diagrams. Dotted lines represent additional relationships that follow an evident pattern.
The information that appears in each diagram is recapitulated in tabular form. The first diagram and its corresponding table illustrate the relationship of various item types. Item types are used to characterize the various types of item that can appear in a sequence nodes, atomic values, and functions , and they are therefore used in declaring the types of variables or the argument types and result types of functions. Item types in the data model form a directed graph, rather than a hierarchy or lattice: in the relationship defined by the derived-from A, B function, some types are derived from more than one other type.
Examples include functions function xs:string as xs:int is substitutable for function xs:NCName as xs:int and also for function xs:string as xs:decimal , and union types A is substitutable for union A, B and also for union A, C. In XDM, item types include node types, function types, and built-in atomic types. The diagram, which shows only hierarchic relationships, is therefore a simplification of the full model.
In the table, each type whose name is indented is derived from the type whose name appears nearest above it with one less level of indentation. The next diagram and table illustrate the schema type subsystem, in which all types are derived from the distinguished type xs:anyType. Schema types include built-in types defined in the XML Schema specification, and user-defined types defined using mechanisms described in the XML Schema specification.
Schema types define the permitted contents of nodes. The main categories are complex types, which define the permitted content of elements, and simple types, which can be used to constrain the values of both elements and attributes. The final diagram and table show all of the atomic types, including the primitive simple types and the built-in types derived from the primitive simple types.
Atomic types are both item types and schema types, so the root type xs:anyAtomicType may be found in both the previous diagrams. The terminology used to describe the functions and operators on types defined in [XML Schema Part 2: Datatypes Second Edition] is defined in the body of this specification.
The terms defined in this section are used in building those definitions. The valid characters are defined by their codepoints, and include some whose codepoints have not been assigned by the Unicode consortium to any character. This specification spells "codepoint" as one word; the Unicode specification spells it as "code point". Equivalent terms found in other specifications are "character number" or "code position".
Because these terms appear so frequently, they are hyperlinked to the definition only when there is a particular desire to draw the reader's attention to the definition; the absence of a hyperlink does not mean that the term is being used in some other sense.
Unless explicitly stated, the xs:string values returned by the functions in this document are not normalized in the sense of [Character Model for the World Wide Web 1. Some implementations may represent a codepoint above xFFFF using two bit values known as a surrogate pair. A surrogate pair counts as one character, not two. This document uses the phrase "namespace URI" to identify the concept identified in [Namespaces in XML] as "namespace name", and the phrase "local name" to identify the concept identified in [Namespaces in XML] as "local part".
Two expanded QNames are equal if the namespace URIs are the same or both absent and the local names are the same. The prefix plays no part in the comparison, but is used only if the expanded QName needs to be converted back to a string. The auxiliary verb must , when rendered in small capitals, indicates a precondition for conformance.
When the sentence relates to an implementation of a function for example "All implementations must recognize URIs of the form The auxiliary verb may , when rendered in small capitals, indicates optional or discretionary behavior.
The statement "An implementation may do X" implies that it is implementation-dependent whether or not it does X.
The auxiliary verb should , when rendered in small capitals, indicates desirable or recommended behavior. The statement "An implementation should do X" implies that it is desirable to do X, but implementations may choose to do otherwise if this is judged appropriate.
Where this specification states that something is implementation-defined or implementation-dependent, it is open to host languages to place further constraints on the behavior. This section is concerned with the question of whether two calls on a function, with the same arguments, may produce different results. For example, two calls to fn:current-dateTime within the same execution scope will return the same result. The execution scope is defined by the host language that invokes the function library.
In XSLT, for example, any two function calls executed during the same transformation are in the same execution scope except that static expressions, such as those used in use-when attributes, are in a separate execution scope.
The following definition explains more precisely what it means for two function calls to return the same result:. Two items are identical if and only if one of the following conditions applies:.
Both items are atomic values, of precisely the same type, and the values are equal as defined using the eq operator, using the Unicode codepoint collation when comparing strings. Both items are function items, neither item is a map or array , and all the following conditions apply:. Either both functions have the same name, or both names are absent DM Both functions have the same function signature.
Two function signatures are defined to be the same if the declared result types are identical and the declared argument types are pairwise identical. Two types S and T are defined to be identical if and only if subtype S, T and subtype T, S both hold, where the subtype relation is defined in Section 2.
However, two functions whose signatures differ in this way will probably be deemed non-identical under rule e below, because they are likely to have different effect when invoked with an argument of type xs:untypedAtomic. Both functions have the same nonlocal variable bindings sometimes called the function's closure. The processor is able to determine that the implementations of the two functions are equivalent, in the sense that for all possible combinations of arguments, the two functions have the same effect.
There is no function or operator defined in the specification that tests whether two function items are identical. Where the specification requires two function items to be identical, for example in the results of repeated calls of a function whose result is a function, then the processor must ensure that it returns functions that are indistinguishable in their observable effect. Where the specification defines behavior conditional on two function items being identical, the determination of identity is to some degree implementation-dependent.
There are cases where function items are definitely not identical for example if they have different name or arity , but positive determination of identity is possible only using implementation-dependent techniques, for example when both items contain references to the same piece of code representing the function's implementation. Some functions produce results that depend not only on their explicit arguments, but also on the static and dynamic context.
A function that is context-dependent can be used as a named function reference, can be partially applied, and can be found using fn:function-lookup. The principle in such cases is that the static context used for the function evaluation is taken from the static context of the named function reference, partial function application, or the call on fn:function-lookup ; and the dynamic context for the function evaluation is taken from the dynamic context of the evaluation of the named function reference, partial function application, or the call of fn:function-lookup.
In effect, the static and dynamic part of the context thus act as part of the closure of the function item. The same applies to a number of functions in the op: namespace that manipulate dates and times and that make use of the implicit timezone.
A number of functions including fn:base-uri 0 , fn:data 0 , fn:document-uri 0 , fn:element-with-id 1 , fn:id 1 , fn:idref 1 , fn:lang 1 , fn:last 0 , fn:local-name 0 , fn:name 0 , fn:namespace-uri 0 , fn:normalize-space 0 , fn:number 0 , fn:path 0 , fn:position 0 , fn:root 0 , fn:string 0 , and fn:string-length 0 depend on the focus XP These functions will in general return different results on different calls if the focus is different.
The function fn:default-collation and many string-handling operators and functions depend on the default collation and the in-scope collations, which are both properties of the static context. If a particular call of one of these functions is evaluated twice with the same arguments then it will return the same result each time because the static context, by definition, does not change at run time.
However, two distinct calls that is, two calls on the function appearing in different places in the source code may produce different results even if the explicit arguments are the same.
Functions such as fn:static-base-uri , fn:doc , and fn:collection depend on other aspects of the static context. As with functions that depend on collations, a single call will produce the same results on each call if the explicit arguments are the same, but two calls appearing in different places in the source code may produce different results. The fn:function-lookup function is a special case because it is potentially dependent on everything in the static and dynamic context.
This is because the static and dynamic context of the call to fn:function-lookup are used as the static and dynamic context of the function that fn:function-lookup returns. Exceptions include the following:. In such cases two calls with the same arguments are not guaranteed to produce the results in the same order. These functions are said to be nondeterministic with respect to ordering.
Some functions such as fn:analyze-string , fn:parse-xml , fn:parse-xml-fragment , and fn:json-to-xml construct a tree of nodes to represent their results. There is no guarantee that repeated calls with the same arguments will return the same identical node in the sense of the is operator. However, if non-identical nodes are returned, their content will be the same in the sense of the fn:deep-equal function.
Such a function is said to be non-deterministic with respect to node identity. Some functions such as fn:doc and fn:collection create new nodes by reading external documents. Some of these accessors are exposed to the user through the functions described below. Each of these functions has an arity-zero signature which is equivalent to the arity-one form, with the context item supplied as the implicit first argument. In addition, each of the arity-one functions accepts an empty sequence as the argument, in which case it generally delivers an empty sequence as the result: the exception is fn:string , which delivers a zero-length string.
If the argument is omitted, it defaults to the context item. The behavior of the function if the argument is omitted is exactly the same as if the context item had been passed as the argument. For element and attribute nodes, the name of the node is returned as an xs:QName , retaining the prefix, namespace URI, and local part. For a namespace node, the function returns an empty sequence if the node represents the default namespace; otherwise it returns an xs:QName in which prefix and namespace URI are absent DM31 and the local part is the namespace prefix being bound.
That is, calling fn:string is equivalent to calling fn:string. Every node has a string value, even an element with element-only content which has no typed value. Moreover, casting an atomic value to a string always succeeds. Functions, maps, and arrays have no string value, so these are the only arguments that satisfy the type signature but cause failure.
Returns the result of atomizing a sequence. This process flattens arrays, and replaces nodes by their typed values. If the item is a node, the typed value of the node is appended to the result sequence. If the item is an array, the result of applying fn:data to each member of the array, in order, is appended to the result sequence.
The process of applying the fn:data function to a sequence is referred to as atomization. In many cases an explicit call on fn:data is not required, because atomization is invoked implicitly when a node or sequence of nodes is supplied in a context where an atomic value or sequence of atomic values is required.
The zero-argument version of the function returns the base URI of the context node: it is equivalent to calling fn:base-uri. In this document, as well as in [XQuery 3. Raising an error is equivalent to calling the fn:error function defined in this section with the provided error code. Except where otherwise specified, errors defined in this specification are dynamic errors.
Some errors, however, are classified as type errors. Type errors are typically used where the presence of the error can be inferred from knowledge of the type of the actual arguments to a function, for example with a call such as fn:string fn:abs 1. Host languages may allow type errors to be reported statically if they are discovered during static analysis. When function specifications indicate that an error is to be raised, the notation "[ error code ]".
It is this xs:QName that is actually passed as an argument to the fn:error function. Calling this function raises an error. For a more detailed treatment of error handing, see Section 2. The fn:error function is a general function that may be called as above but may also be called from [XQuery 3. This function never returns a value. Instead it always raises an error. The effect of the error is identical to the effect of dynamic errors raised implicitly, for example when an incorrect argument is supplied to a function.
The parameters to the fn:error function supply information that is associated with the error condition and that is made available to a caller that asks for information about the error. It is an xs:QName ; the namespace URI conventionally identifies the component, subsystem, or authority responsible for defining the meaning of the error code, while the local part identifies the specific error condition. The namespace URI part of the error code should therefore not include a fragment identifier.
This function always raises a dynamic error. By default, it raises [ err:FOER ]. The type "none" is a special type defined in [XQuery 1. It indicates that the function never returns and ensures that it has the correct static type. Any QName may be used as an error code; there are no reserved names or namespaces. The error is always classified as a dynamic error, even if the error code used is one that is normally used for static errors or type errors.
The expression fn:error raises error FOER Sometimes there is a need to output trace information unrelated to a specific value. Consider a situation in which a user wants to investigate the actual value passed to a function. The operators described in this section are defined on the following atomic types.
Each type whose name is indented is derived from the type whose name appears nearest above with one less level of indentation. The type xs:numeric is defined as a union type whose member types are in order xs:double , xs:float , and xs:decimal. This type is implicitly imported into the static context, so it can also be used in defining the signature of user-written functions.
Apart from the fact that it is implicitly imported, it behaves exactly like a user-defined type with the same definition. This means, for example:. If the expected type of a function parameter is given as xs:numeric , the actual value supplied can be an instance of any of these three types, or any type derived from these three by restriction this includes the built-in type xs:integer , which is derived from xs:decimal. If the expected type of a function parameter is given as xs:numeric , and the actual value supplied is xs:untypedAtomic or a node whose atomized value is xs:untypedAtomic , then it will be cast to the union type xs:numeric using the rules in Because the lexical space of xs:double subsumes the lexical space of the other member types, and xs:double is listed first, the effect is that if the untyped atomic value is in the lexical space of xs:double , it will be converted to an xs:double , and if not, a dynamic error occurs.
When the return type of a function is given as xs:numeric , the actual value returned will be an instance of one of the three member types and perhaps also of types derived from these by restriction. The rules for the particular function will specify how the type of the result depends on the values supplied as arguments. In many cases, for the functions in this specification, the result is defined to be the same type as the first argument.
This specification uses [IEEE ] arithmetic for xs:float and xs:double values. One consequence of this is that some operations result in the value NaN not-a number , which has the unusual property that it is not equal to itself. Another consequence is that some operations return the value negative zero.
The text accompanying several functions defines behavior for both positive and negative zero inputs and outputs in the interest of alignment with [IEEE ].
A conformant implementation must respect these semantics. In consequence, the expression As a concession to implementations that rely on implementations of XSD 1. XML Schema 1. The following functions define the semantics of arithmetic operators defined in [XQuery 3. The parameters and return types for the above operators are in most cases declared to be of type xs:numeric , which permits the basic numeric types: xs:integer , xs:decimal , xs:float and xs:double , and types derived from them.
In general the two-argument functions require that both arguments are of the same primitive type, and they return a value of this same type. The exceptions are op:numeric-divide , which returns an xs:decimal if called with two xs:integer operands, and op:numeric-integer-divide which always returns an xs:integer. If the two operands of an arithmetic expression are not of the same type, subtype substitution and numeric type promotion are used to obtain two operands of the same type.
Section B. The result type of operations depends on their argument datatypes and is defined in the following table:. These rules define any operation on any pair of arithmetic types. Consider the following example:. For this operation, xs:int must be converted to xs:double.
This can be done, since by the rules above: xs:int can be substituted for xs:integer , xs:integer can be substituted for xs:decimal , xs:decimal can be promoted to xs:double. As far as possible, the promotions should be done in a single step. Specifically, when an xs:decimal is promoted to an xs:double , it should not be converted to an xs:float and then to xs:double , as this risks loss of precision.
As another example, a user may define height as a derived type of xs:integer with a minimum value of 20 and a maximum value of They may then derive fenceHeight using an enumeration to restrict the permitted set of values to, say, 36, 48 and The basic rules for addition, subtraction, and multiplication of ordinary numbers are not set out in this specification; they are taken as given.
In the case of xs:double and xs:float the rules are as defined in [IEEE ]. The rules for handling division and modulus operations, as well as the rules for handling special values such as infinity and NaN , and exception conditions such as overflow and underflow, are described more explicitly since they are not necessarily obvious.
On overflow and underflow situations during arithmetic operations conforming implementations must behave as follows:. For xs:float and xs:double operations, overflow behavior must be conformant with [IEEE ]. This specification allows the following options:.
Raising a dynamic error [ err:FOAR ] via an overflow trap. For xs:float and xs:double operations, underflow behavior must be conformant with [IEEE ]. Raising a dynamic error [ err:FOAR ] via an underflow trap. Returning 0. For xs:decimal operations, overflow behavior must raise a dynamic error [ err:FOAR ]. On underflow, 0. For xs:integer operations, implementations that support limited-precision integer operations must select from the following options:.
They may choose to always raise a dynamic error [ err:FOAR ]. See [ISO ]. The functions op:numeric-add , op:numeric-subtract , op:numeric-multiply , op:numeric-divide , op:numeric-integer-divide and op:numeric-mod are each defined for pairs of numeric operands, each of which has the same type: xs:integer , xs:decimal , xs:float , or xs:double.
The functions op:numeric-unary-plus and op:numeric-unary-minus are defined for a single operand whose type is one of those same numeric types.
For xs:float and xs:double arguments, if either argument is NaN , the result is NaN. Then for addition, subtraction, and multiplication operations, the returned result should be accurate to N digits of precision, and for division and modulus operations, the returned result should be accurate to at least M digits of precision. This Recommendation does not specify whether xs:decimal operations are fixed point or floating point.
In an implementation using floating point it is possible for very simple operations to require more digits of precision than are available; for example adding 1e to 1e requires digits of precision for an accurate representation of the result. The IEEE divideByZero exception is raised not only by a direct attempt to divide by zero, but also by operations such as log 0.
The IEEE invalidOperation exception is raised by attempts to call a function with an argument that is outside the function's domain for example, sqrt -1 or log Although IEEE defines these as exceptions, it also defines "default non-stop exception handling" in which the operation returns a defined result, typically positive or negative infinity, or NaN.
With this function library, these IEEE exceptions do not cause a dynamic error at the application level; rather they result in the relevant function or operator returning the defined non-error result.
These two values are not distinguishable in the XDM model: the value spaces of xs:float and xs:double each include only a single NaN value. Defines the semantics of the "-" operator when applied to two numeric values. For xs:float or xs:double values, if one of the operands is a zero or a finite number and the other is INF or -INF , an infinity of the appropriate sign is returned.
For xs:float or xs:double values, if one of the operands is a zero and the other is an infinity, NaN is returned. If one of the operands is a non-zero number and the other is an infinity, an infinity with the appropriate sign is returned. Defines the semantics of the "div" operator when applied to two numeric values.
0コメント