Using Data Sources in Queries
In XQuery, data sources and query results are both represented using an XML data model. Physical inputs such as XML text files, DOM trees, and relational databases are mapped into the XML data model when they are queried. In the case of relational databases, the mapping is logical, and relational data is not materialized as XML. These mappings are documented in this section.
The result of a query (the XML output) is also defined in the XML data model and must be mapped to a physical format such as DOM, SAX, StAX, or text in order for an application to use the results. See Accessing XML Results for more information about the mapping of results.
In addition, when you use DataDirect XQuery with DataDirect XML Converters, you can convert non-XML formats into XML, including EDI messages, tab-delimited and comma-separated text files, dBASE files, RTF files, and many more. Once these non-XML data sources are converted to XML, they are accessed the same as XML documents.
XML Data Sources
The XML data sources that DataDirect XQuery can access have the following physical formats:
- XML text files/streams. These files/streams can be accessed using fn:doc(), which supports the http:, ftp:, and file: URI schemes and DataDirect XML Converters URI schemes.
Here is an example of a file URI scheme:
let $request := doc('file:///c:/request.xml')/request
...
Here is an example of a DataDirect XQuery XML Converters URI scheme in which the name of the Converter is Base64, the properties set for the conversion are newline and encoding, and the file to convert is base_to_xml.bin:
let $request := doc('converter:Base64:newline=crlf:
encoding=utf-8?file///w:/myfiles/base_to_xml.bin')/
request
...
NOTE: Using fn:collection(), DataDirect XQuery also allows you to make the following types of queries on XML files:
- Query multiple XML files in a directory. See Querying Multiple Files in a Directory.
- Query XML files archived in ZIP or JAR files. See Querying ZIP, JAR, and MS Office Files.
NOTES FOR SPECIFYING URIs:
- You must use forward slashes (/) in the path regardless of the platform.
- Relative URIs are allowed in the path. For example:
let $request := doc('request.xml')/request
...
- Spaces are allowed in the path. For example:
let $request := doc('file:///c:/DD XQuery/
request.xml')/request...
If you use a custom URI resolver, the rules enforced for URI paths are governed by the syntax specified by the custom URI resolver (see Document URI Resolvers).
- XML contained in a Java application. This type of XML can be bound to the initial context item or to external variables in XQJ and used in XQuery queries. See Querying Data from XML Files or DOM Trees for an example.
- XML stored in columns of any supported relational database using an XML data type. See Support for Relational Databases for details.
- XML stored in character columns of any supported relational database. See Querying XML Type Data for details.
Data Model Representation of XML Documents
For XQuery queries that access XML using fn:doc() or external variables in the query, DataDirect XQuery implements the Infoset mapping described in the XQuery 1.0 and XPath 2.0 Data Model specification located at: http://www.w3.org/TR/2005/CR-xpath-datamodel-20051103/
Relational Data Sources
DataDirect XQuery provides support for the following relational databases:
Note: For support for specific versions, see Supported Databases. This information is also maintained on the DataDirect web site:
http://www.datadirect.com/support/product_info/
databasesupport/index.sspSpecifying Relational Database Tables
Using DataDirect XQuery, you specify relational database tables in a query using fn:collection(). The following example specifies a database table named holdings:
Notes for Specifying Database Table Names
- Qualified names – You might need to qualify the database table name in order to specify the exact table you want to access. The argument to fn:collection() can include any combination of JDBC connection name, database catalog, or database schema in addition to the database table name.
For example, if the database table being accessed is not owned by the current user or is not located in the current database or catalog, you can qualify the database table name with the catalog name and schema name of the database table. The following example specifies a catalog name, schema name, and database table name:
collection('financial.joseph.holdings')
If you need to further qualify the database table name, you can use a JDBC connection name, which identifies a specific connection to a relational database. The following example specifies a JDBC connection name, catalog name, schema name, and database table name:
collection('stocks:financial.joseph.holdings')
- Escape characters – If the catalog name, schema name, or table name in the fn:collection() argument contains a period (.), a colon (:), or a backslash(\), escape the character with a backslash (\) so that DataDirect XQuery can correctly parse the argument. For example, if the target table is named a.holdings and you specify the following query, DataDirect XQuery parses 'a' as the schema name, not as part of the table name:
collection('a.holdings')
Escaping the period (.) in the fn:collection() argument using the backslash character allows DataDirect XQuery to parse the argument correctly:
collection('a\.holdings')
In addition, XQuery string literal syntax applies to the fn:collection() argument. If a table name contains double quotes, for example, a"holdings, and the fn:collection() argument uses double quote delimiters, you must repeat the double quotes:
collection("a""holdings")
Or, you can use:
collection('a"holdings')
See Path Expressions for Relational Sources for examples of using fn:collection(). See Resolving fn:collection Errors for information about troubleshooting fn:collection() errors.
Data Model Representation of Relational Tables
In DataDirect XQuery, XML views of relational data are based on the SQL/XML mappings specified in the SQL 2003 standard. These are logical XML views, not a physical format.
SQL/XML provides different ways to parameterize views. DataDirect XQuery uses the XMLFOREST variable to parameterize views. Using XMLFOREST is sufficient for most applications, but you can choose not to use it by setting the JdbcSqlXmlForest property of DDXQDataSource to false (see DDXQDataSource and DDXQJDBCConnection Properties).
When the value of the JdbcSqlXmlForest property is set to true (the default), DataDirect XQuery represents each database table by a sequence of row elements in a document node. The row elements use the table name as the element name and contain an element for each non-null column in the row; DataDirect XQuery uses the column name as the element name for these elements. For example, when XML FOREST is used, the result for
collection('users')
is a document node containing the following elements:<users> <userid>Minollo</userid> <firstname>Carlo</firstname> <lastname>Innocenti</lastname> <membersince>2004-06-16T00:00:00</membersince> </users> <users> <userid>Jonathan</userid> <firstname>Jonathan</firstname> <lastname>Robie</lastname> <othername>William</othername> <membersince>2004-03-03T00:00:00</membersince> </users>Suppose we wanted to write a FLWOR expression to bind each row of the preceding table to a variable. In this case, we add an argument to fn:collection() that defines a path matching the users elements:
Here is a FLWOR expression that iterates over the rows returned by the preceding path expression:
When the XMLFOREST variable is false, the result is a document node. Inside the document node is a single element that represents the table. Inside the single element node is a sequence of elements named row, each representing one row of the table. For example, when XMLFOREST is not used, a result for
collection('users')
might look like this:<users> <row> <userid>Minollo</userid> <firstname>Carlo</firstname> <lastname>Innocenti</lastname> <membersince>2004-06-16T00:00:00</membersince> </row> <row> <userid>Jonathan</userid> <firstname>Jonathan</firstname> <lastname>Robie</lastname> <othername>William</othername> <membersince>2004-03-03T00:00:00</membersince> </row> </users>Suppose we wanted to write a FLWOR expression to bind each row of the preceding table to a variable. In this case, we would need to add a path expression to fn:collection(). The following path expression defines a path that matches the row elements:
Here is a FLWOR expression that iterates over the rows returned by the preceding path expression:
Case Sensitivity
XML element and attribute names are case-sensitive. When SQL column and table names are mapped into XML elements, the case depends on a number of factors that can vary by database vendor and the parameters used to create a database.
The case sensitivity of the argument to fn:collection() depends on the database.
Data Type Mappings
See Data Type Mappings for information about how database data types are mapped to XML schema data types.
Security Features
DataDirect XQuery supports authentication and data encryption security features for data source connections. For more information, see Chapter 7, Securing Data Source Connections.