Dynamic Paths

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 element's 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:

doc("catalog.xml")//*[name( ) = $elementName][. = $searchValue]

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:

saxon:evaluate(concat('doc("catalog.xml")//',$elementName,
                      '[. = "',$searchValue,'"]'))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.75.221