Path, Predicates, and XQuery - Dynamic Paths (Page 3 of 6 )
It is a common requirement that the paths in your query will not be static but will instead be calculated based on some input to the query. For example, if you want to provide users with a search capability where they choose the elements in the input document to search, you can't use a static path in your query. XQuery does not provide any built-in support for evaluating dynamic paths, but you do have a couple of alternatives.
For simple paths, it is easy enough to test for an elements name using the name function instead of including it directly as a step in the path. For example, if the name of the element to search and its value are bound to the variables $elementName and $searchValue, you can use a path like:
If the dynamic path is more complex than a simple element or attribute name, you can use an implementation-specific function. Most XQuery implementations provide a function for dynamic evaluation of paths or entire queries. For example, in Saxon, it is the saxon:evaluate function, while in Mark Logic it is called xdmp:eval. In Saxon, I could use the following expression to get the same results as the previous example:
A single query can access many input documents. The term input document is used in this book to mean any XML data that is being queried. Technically, it might not be an entire XML document; it might be a document fragment, such as an element or sequence of elements, possibly with children. Alternatively, it might not be a physical XML file at all; it might be data retrieved from an XML database, or an in-memory XML representation that was generated from non-XML data.
If the input document is physically stored in XML syntax, it must be well-formed XML. This means that it must comply with XML syntax rules, such as that every start tag has an end tag, there is no overlap among elements, and special characters are used appropriately. It must also use namespaces appropriately. This means that if colons are used in element or attribute names, the part before the colon must be a prefix that is mapped to a namespace using a namespace declaration.
Whether it is physically stored as an XML document or not, an input document must conform to other constraints on XML documents. For example, an element may not have two attributes with the same name, and element and attribute names may not contain special characters other than dashes, underscores, and periods.
There are four ways that input documents could be accessed from within a query. They are described in the next four sections.