Search code examples
xmlxquery

XQuery: how does the function "doc" work


I am using XQuery tester to learn the XML and XQuery.

Here is an simple example that I am testing.

If I copy the whole xml file above to the XQuery tester and try this xquery /bookstore/book/title, I can get what I want.

Now I try to use the function doc()
I don't know how it works. I know that this function needs a parameter as the xml file. So I think we need the whole path of the file. But in the example above, it just said that we could use this function like this: doc("books.xml"). I am confused: where is "books.xml"?

I also tried to create a xml file named "books.xml" in my PC and pass the parameter like this: doc("C:\Users\lenovo\Desktop\books.xml") but I got an error: Premature end of file.

Could someone tell me how to use the function doc()?


Solution

  • The function doc() gets a URI as parameter. The URI is the name of an XML document. That XML document is returned as the value of calling the function. Your processor can define a mapping from URI names to the file system, or to documents stored in a database, or anything else.

    In your case, it seems that you are using a processor through a webpage. The probability that it lets you resolve doc() from your local file system is very low, as it is (almost) impossible for a webpage to access a host's file system.

    It seems that the below panel let you paste the content of your document though (then is used to display the result of evaluating the query, which is a bit weird).

    If you need more details about how dcc() behaves on XPath Tester, I am afraid you'll need to ask them directly.