Tips for Testing Schemas Having an XQuery queryBinding
3 min readJust now
–
The Choice of Schematron Processors for Running XSpec Tests topic mentions two Schematron processors that are bundled with XSpec: SchXslt (starting in XSpec v3.0) and XQS (starting in XSpec v3.3). If you want to try XQS with your schemas and test suites, can you simply use the same .sch and .xspec files that you would use with SchXslt?
Not exactly.
In the XQS repository, the main README document describes a few ways in which you’d write your schema a little differently for use with XQS. In addition, if you t…
Tips for Testing Schemas Having an XQuery queryBinding
3 min readJust now
–
The Choice of Schematron Processors for Running XSpec Tests topic mentions two Schematron processors that are bundled with XSpec: SchXslt (starting in XSpec v3.0) and XQS (starting in XSpec v3.3). If you want to try XQS with your schemas and test suites, can you simply use the same .sch and .xspec files that you would use with SchXslt?
Not exactly.
In the XQS repository, the main README document describes a few ways in which you’d write your schema a little differently for use with XQS. In addition, if you test that schema with XSpec, you’d write the test suite a little differently.
This topic describes three differences in an XSpec test suite, related to testing a schema with XQS instead of either SchXslt or SchXslt2.
#1: Test Helper Language
XSpec supports test helpers via the <x:helper> element. If test helpers are unfamiliar to you, please see Background: What Is Helper Functionality in XSpec? at the beginning of the Ignoring Code Comments During XSpec Testing topic.
In general, <x:helper> can point to a module written in XSLT or XQuery. But if the schema you are testing has a queryBinding attribute value that’s supported by XQS, then <x:helper> element should point to an XQuery module. Use the query and, optionally, query-at attributes of <x:helper>.
References from <x:helper> to an XSLT stylesheet or XSLT package will be silently ignored when the test suite runs.
#2: Location of Namespace Declarations
As described in Where to Put Namespace Declarations in XSpec Tests, tests for XQuery involve more complicated rules about where to place namespace declarations. Tests for Schematron using XQS follow the XQuery rules, not the XSLT rules. In particular, the <x:description> element is where you must declare prefixes that appear in XPath expressions stored in the following attributes:
locationattribute of a Schematron verification element (e.g.,<x:expect-assert>)selectattribute of<x:variable>,<x:param>, or<x:expect>testattribute of<x:expect>
Many tests for Schematron schemas don’t use the <x:expect> element, but it is in the list for completeness.
#3: Visibility of Schema Functions
A Schematron schema that you use with XQS can optionally declare custom functions written in XQuery. Such functions are visible to the code in the schema but not visible to XSpec. If you want to test the schema’s custom XQuery functions, use this arrangement:
- Place the XQuery functions in a custom XQuery library module separate from the schema.
- Use the instructions in How do I import XQuery modules? to point from your schema to your XQuery library module.
- Create one or more XSpec test scenarios for your XQuery library module, and place them in a test suite that has access to the module.
In step 3, you’d typically place the scenarios in a test suite for XQuery, separate from the test suite for the schema. For example, the test suite for XQuery would point to the XQuery library module like this:
<x:description query="urn:x-xspectacles:functions:xqs-based-testing:schema-fcn" query-at="schema-fcn.xqm" xmlns:sf="urn:x-xspectacles:functions:xqs-based-testing:schema-fcn" xmlns:x="http://www.jenitennison.com/xslt/xspec"> <!--Scenarios for testing XQuery library module...--></x:description>
Alternatively, you can place the scenarios in the same test suite for the schema, if that test suite uses the <x:helper> element to gain access to the XQuery library module. For example, the test suite would look like this:
<x:description schematron="xqs-based-schema.sch" xmlns:frc="urn:x-xspectacles:functions:helper:remove-comments" xmlns:sf="urn:x-xspectacles:functions:xqs-based-testing:schema-fcn" xmlns:x="http://www.jenitennison.com/xslt/xspec"> <x:helper query-at="schema-fcn.xqm" query="urn:x-xspectacles:functions:xqs-based-testing:schema-fcn"/> <!--Scenarios for testing schema...--> <!--Scenarios for testing XQuery library module...--></x:description>
Key Takeaways
- The
queryBindingattribute of a Schematron schema affects some aspects of how you write the schema and an XSpec test for the schema. - In tests for a schema compatible with XQS, the
<x:helper>element always points to an XQuery module. By contrast, in tests for a schema compatible with SchXslt or SchXslt2, the<x:helper>element points to an XSLT stylesheet or package. - When testing a schema compatible with XQS, you must place certain namespace declarations on the
<x:description>element instead of deeper in the XSpec element hierarchy, and you need to arrange files and file references to make any custom schema functions testable. You are free to adopt the same practices when testing schemas compatible with SchXslt or SchXslt2, too.