[xquery-talk] Top N Most Common Mistakes [SEC=UNCLASSIFIED]

Michael Rys mrys at microsoft.com
Tue Aug 14 19:05:08 PDT 2007

SQL Server 2005 similarly will check your XPath expression against an XML datatype's associated XML Schema collection.


Declare @x xml(MySchemaCollection);

SELECT @x.query('/a/b');

will raise a compile time error if none of the XML Schemas in the schema collection allow for a b element contained inside an a element.

Best regards

-----Original Message-----
From: talk-bounces at x-query.com [mailto:talk-bounces at x-query.com] On Behalf Of Michael Kay
Sent: Tuesday, August 14, 2007 5:25 PM
To: 'Smith, Donald T.'; Nicholas.Ardlie at ga.gov.au; rpbourret at rpbourret.com; talk at x-query.com
Subject: RE: [xquery-talk] Top N Most Common Mistakes [SEC=UNCLASSIFIED]

> By this, Michael, do you mean anything more than "use named
> types rather than anonymous types" in your Schemas? If so,
> can you elaborate on what "properly" means here?

To get compile time checking of your query against a schema, you need to
declare the types of variables and of arguments to functions. You also need
to ensure that the expected type of the context item and of the result of
doc() is known statically. This is best done by defining global variables,
for example

declare variable $in as document-node(schema-element(purchase-order)) :=
declare variable $books as document-node(schema-element(booklist)) := :=

declare function f:orders-for-customer($c as schema-element(customer)) as
schema-element(order)* {

When you write a path expression and the type of the first step in the path
is known, Saxon-SA will give you warnings if any subsequent steps are
incapable of selecting any nodes.

Using named types in your schemas is useful because it allows you to refer
to them in your queries - but there's no benefit unless you actually do
refer to them!

Michael Kay

talk at x-query.com

More information about the talk mailing list