Difference between revisions of "Inspection Module"

From BaseX Documentation
Jump to navigation Jump to search
m (Text replacement - "syntaxhighlight" to "pre")
 
(88 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
This [[Module Library|XQuery Module]] contains functions for extracting internal information about modules and functions and generating documentation.
 
This [[Module Library|XQuery Module]] contains functions for extracting internal information about modules and functions and generating documentation.
 +
 +
With {{Announce|Version 11}}, {{Code|inspect:function-annotations}} has been removed as it is now available in the official specification as [https://qt4cg.org/specifications/xpath-functions-40/Overview.html#func-function-annotations <code>fn:function-annotations</code>].
  
 
=Conventions=
 
=Conventions=
  
All functions in this module are assigned to the {{Code|http://basex.org/modules/inspect}} namespace, which is statically bound to the {{Code|inspect}} prefix.<br/>
+
All functions and errors in this module are assigned to the <code><nowiki>http://basex.org/modules/inspect</nowiki></code> namespace, which is statically bound to the {{Code|inspect}} prefix.<br/>
All errors are assigned to the {{Code|http://basex.org/errors}} namespace, which is statically bound to the {{Code|bxerr}} prefix.
+
xqDoc document instances are assigned to the <code><nowiki>http://www.xqdoc.org/1.0</nowiki></code> namespace, which is statically bound to the {{Code|xqdoc}} prefix.
 +
 
 +
=Reflection=
 +
 
 +
==inspect:functions==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>inspect:functions(
 +
  $href  as xs:string  := ()
 +
) as function(*)*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns function items for all user-defined functions (both public and private) that are known in the current query context. If an {{Code|$href}} value is specified, the specified resource will be retrieved as a string and compiled, and its functions will be added to the query context and returned to the user. A relative URI will be resolved against the static base URI of the query.
 +
|- valign="top"
 +
|'''Examples'''
 +
|Invokes the declared functions and returns their values:<br/>
 +
<pre lang='xquery'>
 +
declare %private function local:one() { 12 };
 +
declare %private function local:two() { 34 };
 +
for $f in inspect:functions() return $f()
 +
</pre>
 +
Compiles all functions in {{Code|code.xqm}} and invokes the function named {{Code|run}}:
 +
<pre lang='xquery'>
 +
let $uri := 'code.xqm'
 +
let $name := 'run'
 +
for $f in inspect:functions($uri)
 +
where local-name-from-QName(function-name($f)) = $name
 +
return $f()
 +
</pre>
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|parse|#Errors}} Error while parsing a module.
 +
|}
 +
 
 +
==inspect:static-context==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>inspect:static-context(
 +
  $function  as function(*)?,
 +
  $name      as xs:string
 +
) as item()*</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns a component of the [https://www.w3.org/TR/xquery-31/#dt-static-context static context] of a {{Code|$function}} with the specified {{Code|$name}}. If no function is supplied, the current static context is considered.<br/>The following components can be requested:
 +
* {{Code|base-uri}}: Static base URI.
 +
* {{Code|namespaces}}: Prefix/URI map with all statically known namespaces.
 +
* {{Code|element-namespace}}: Default element/type namespace URI, or an empty sequence if it is absent.
 +
* {{Code|function-namespace}}: Default function namespace URI, or an empty sequence if it is absent.
 +
* {{Code|collation}}: URI of the default collation.
 +
* {{Code|ordering}}: Ordering mode ({{Code|ordered}}/{{Code|unordered}})
 +
* {{Code|construction}}: Construction mode ({{Code|preserve}}/{{Code|strip}})
 +
* {{Code|default-order-empty}}: Default order for empty sequences ({{Code|greatest}}/{{Code|least}})
 +
* {{Code|boundary-space}}: Boundary-space policy ({{Code|preserve}}/{{Code|strip}})
 +
* {{Code|copy-namespaces}}: Copy-namespaces mode ({{Code|inherit}}/{{Code|no-inherit}}, {{Code|preserve}}/{{Code|no-preserve}})
 +
* {{Code|decimal-formats}}: Nested map with all statically known decimal formats
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* Returns the static base URI (same as {{Code|static-base-uri()}}):
 +
<pre lang='xquery'>
 +
inspect:static-context((), 'base-uri')
 +
</pre>
 +
* Returns a map with all namespaces that are statically known in the module of the specified function:
 +
<pre lang='xquery'>
 +
import module namespace data = 'data.xqm';
 +
inspect:static-context(data:get#1, 'namespaces')
 +
</pre>
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|unknown|#Errors}} The specified component does not exist.
 +
|}
  
=Functions=
+
=Documentation=
 +
 
 +
==inspect:type==
 +
 
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>inspect:type(
 +
  $input    as item()*,
 +
  $options  as map(*)?  := map { }
 +
) as xs:string</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Returns a string representation of the type of a the given {{Code|$input}}:
 +
* The string includes the occurrence indicator.
 +
* The type of functions and nodes may be stricter than the returned type.
 +
* For type checking, the standard expressions {{Code|typeswitch}} and {{Code|instance of}} should be used instead.
 +
The following {{Code|$options}} are available:
 +
* {{Code|item}}: If enabled, only the item type is returned and the occurrence indicator is omitted. The default is {{Code|false()}}.
 +
* {{Code|mode}}: If {{Code|value}} is specified, the assigned type of the result value is returned. With {{Code|expression}} the type of the input expression is returned (please note that the original expression may already have been rewritten at compile-time). With {{Code|computed}}, the exact value is computed at runtime, based on the expression and the result value. The default is {{Code|computed}}.
 +
|- valign="top"
 +
| '''Examples'''
 +
|
 +
* <code>inspect:type((<a/>, <a/>))</code> yields <code>element(a)+</code>
 +
* <code>inspect:type(map { 'a': (1, 2)[. = 1] })</code> yields <code>map(xs:string, xs:integer)</code>
 +
* <code>inspect:type(1 to 100, map { 'item': true() })</code> yields <code>xs:integer</code>
 +
|}
  
 
==inspect:function==
 
==inspect:function==
 +
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
| width='90' | '''Signatures'''
+
| width='120' | '''Signature'''
|{{Func|inspect:function|$function as function()|element(function)}}
+
|<pre>inspect:function(
|-
+
  $function as function(*)
 +
) as element(function)</pre>
 +
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Inspects the specified {{Code|$function}} and returns an element that describs its structure. The output of this function is similar to eXist-db’s [http://exist-db.org/exist/apps/fundocs/view.html?uri=http://exist-db.org/xquery/inspection&location=java:org.exist.xquery.functions.inspect.InspectionModule inspect:inspect-function] function.
+
|Inspects the specified {{Code|$function}} and returns an element that describes its structure. The output of this function is similar to eXist-db’s [https://exist-db.org/exist/apps/fundocs/view.html?uri=http://exist-db.org/xquery/inspection&location=java:org.exist.xquery.functions.inspect.InspectionModule inspect:inspect-function] function.
|-
+
|- valign="top"
 
|'''Examples'''
 
|'''Examples'''
 
|The query {{Code|inspect:function(count#1)}} yields:
 
|The query {{Code|inspect:function(count#1)}} yields:
<pre class="brush:xml">
+
<pre lang="xml">
<function name="count" uri="http://www.w3.org/2005/xpath-functions">
+
<function name="count" uri="http://www.w3.org/2005/xpath-functions" external="false">
   <parameter type="item()" occurrence="*"/>
+
   <argument type="item()" occurrence="*"/>
 
   <return type="xs:integer"/>
 
   <return type="xs:integer"/>
 
</function>
 
</function>
 
</pre>
 
</pre>
 
The function…
 
The function…
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
(:~
 
(:~
 
  : This function simply returns the specified integer.
 
  : This function simply returns the specified integer.
Line 32: Line 137:
 
  : @return        specified number
 
  : @return        specified number
 
  :)
 
  :)
declare %private function local:same(
+
declare %private function local:same($number as xs:integer) as xs:integer {
  $number as xs:integer
 
) as xs:integer {
 
 
   $number
 
   $number
 
};
 
};
 
</pre>
 
</pre>
 
…is represented by {{Code|inspect:function(local:same#1)}} as…
 
…is represented by {{Code|inspect:function(local:same#1)}} as…
<pre class="brush:xml">
+
<pre lang="xml">
<function name="local:same" uri="http://www.w3.org/2005/xquery-local-functions">
+
<function name="local:same" uri="http://www.w3.org/2005/xquery-local-functions" external="false">
   <parameter type="xs:integer" name="number">number to return</parameter>
+
   <argument type="xs:integer" name="number">number to return</argument>
 
   <annotation name="private" uri="http://www.w3.org/2012/xquery"/>
 
   <annotation name="private" uri="http://www.w3.org/2012/xquery"/>
 
   <description>This function simply returns the specified integer.</description>
 
   <description>This function simply returns the specified integer.</description>
Line 47: Line 150:
 
</function>
 
</function>
 
</pre>
 
</pre>
 +
|}
 +
 +
==inspect:context==
 +
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>inspect:context() as element(context)</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Generates an element that describes all variables and functions in the current query context.
 +
|- valign="top"
 +
| '''Examples'''
 +
|Evaluate all user-defined functions with zero arguments in the query context:<br/>
 +
<pre lang='xquery'>
 +
inspect:context()/function ! function-lookup(QName(@uri, @name), 0) ! .()
 +
</pre>
 +
Return the names of all private functions in the current context:
 +
<pre lang='xquery'>
 +
for $f in inspect:context()/function
 +
where $f/annotation/@name = 'private'
 +
return $f/@name/string()
 +
</pre>
 +
|}
 +
 +
==inspect:module==
 +
 +
{| width='100%'
 +
|- valign="top"
 +
| width='120' | '''Signature'''
 +
|<pre>inspect:module(
 +
  $uri  as xs:string
 +
) as element(module)</pre>
 +
|- valign="top"
 +
| '''Summary'''
 +
|Retrieves the resource located at the specified {{Code|$uri}}, parses it as XQuery module, and generates an element that describes the module's structure. A relative URI will be resolved against the static base URI of the query.
 +
|- valign="top"
 +
|'''Examples'''
 +
|An example is [[#Examples|shown below]].
 +
|- valign="top"
 +
| '''Errors'''
 +
|{{Error|parse|#Errors}} Error while parsing a module.
 
|}
 
|}
  
 
==inspect:xqdoc==
 
==inspect:xqdoc==
 +
 
{| width='100%'
 
{| width='100%'
|-
+
|- valign="top"
| width='90' | '''Signatures'''
+
| width='120' | '''Signature'''
|{{Func|inspect:xqdoc|$uri as xs:string|element(xqdoc:xqdoc)}}
+
|<pre>inspect:xqdoc(
|-
+
  $uri as xs:string
 +
) as element(xqdoc:xqdoc)</pre>
 +
|- valign="top"
 
| '''Summary'''
 
| '''Summary'''
|Retrieves the string from the specified {{Code|$input}}, parses it as XQuery module, and generates an xqDoc element.<br/>xqDoc provides a simple vendor neutral solution for generating a documentation from XQuery modules. The documentation conventions have been inspired by the JavaDoc standard. Documentation comments begin with {{Code|(:~}} and end with {{Code|:)}}, and tags start with {{Code|@}}. xqDoc comments can be specified for main and library modules and variable and function declarations. We have slightly extended the conventions and updated the [http://files.basex.org/etc/xqdoc-1.1.30052013.xsd xqDoc schema]:<br/>
+
|Retrieves the resource located at the specified {{Code|$uri}}, parses it as XQuery module, and generates an xqDoc element. A relative URI will be resolved against the static base URI of the query.<br/>[http://xqdoc.org/ xqDoc] provides a simple vendor-neutral solution for generating documentation from XQuery modules. The documentation conventions have been inspired by the JavaDoc standard. Documentation comments begin with {{Code|(:~}} and end with {{Code|:)}}, and tags start with {{Code|@}}. xqDoc comments can be specified for main and library modules and variable and function declarations.<br/>
* an {{Code|<xqdoc:namespaces/>}} node lists all namespaces of a module
+
We have slightly extended the xqDoc conventions to do justice to more recent versions of XQuery (Schema: [https://files.basex.org/etc/xqdoc-1.1.30052013.xsd xqdoc-1.1.30052013.xsd]):<br/>
* if annotations are attached to variables or functions, an {{Code|<xqdoc:annotations/>}} node is added
+
* an {{Code|<xqdoc:annotations/>}} node is added to each variable or function that uses annotations. The xqdoc:annotation child nodes may have additional {{Code|xqdoc:literal}} elements with {{Code|type}} attributes (xs:string, xs:integer, xs:decimal, xs:double) and values.
* variables now have a name and a type.
+
* a single {{Code|<xqdoc:namespaces/>}} node is added to the root element, which summarizes all prefixes and namespace URIs used or declared in the module.
|-
+
* name and type elements are added to variables.
 +
|- valign="top"
 +
|'''Examples'''
 +
|An example is [[#Examples|shown below]].
 +
|- valign="top"
 
| '''Errors'''
 
| '''Errors'''
|{{Error|FODC0002|XQuery Errors#Functions Errors}} the addressed resource cannot be retrieved.
+
|{{Error|parse|#Errors}} Error while parsing a module.
|-
 
| '''Example'''
 
|An example is [[#Example|shown below]].
 
 
|}
 
|}
  
=Example=
+
=Examples=
  
This is the {{Code|xqdoc:parse('sample.xqm')}} library module:
+
This is the {{Code|sample.xqm}} library module:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
(:~  
 
(:~  
 
  : This module provides some sample functions to demonstrate
 
  : This module provides some sample functions to demonstrate
Line 84: Line 233:
  
 
(:~ This is a sample string. :)
 
(:~ This is a sample string. :)
declare variable $samples:test-string
+
declare variable $samples:test-string as xs:string := 'this is a string';
  as xs:string := 'this is a string';
 
  
 
(:~
 
(:~
Line 92: Line 240:
 
  : @return  specified number
 
  : @return  specified number
 
  :)
 
  :)
declare %private function samples:same(
+
declare %private function samples:same($number as xs:integer) as xs:integer {
  $number as xs:integer
 
) as xs:integer {
 
 
   $number
 
   $number
 
};
 
};
 
</pre>
 
</pre>
  
When [[#inspect:module|inspect:module]] is run, the following output will be generated:
+
If {{Code|inspect:module('sample.xqm')}} is run, the following output will be generated:
  
<pre class="brush:xml">
+
<pre lang="xml">
 
<module prefix="samples" uri="http://basex.org/modules/samples">
 
<module prefix="samples" uri="http://basex.org/modules/samples">
   <variable name="samples:test-string" uri="http://basex.org/modules/samples" type="xs:string">
+
  <description>This module provides some sample functions to demonstrate
     <comment>
+
the features of the Inspection Module.</description>
      <description>This is a sample string.</description>
+
  <author>BaseX Team</author>
    </comment>
+
  <see>http://docs.basex.org/wiki/XQDoc_Module</see>
 +
  <version>1.0</version>
 +
   <variable name="samples:test-string" uri="http://basex.org/modules/samples" type="xs:string" external="false">
 +
     <description>This is a sample string.</description>
 
   </variable>
 
   </variable>
   <function name="samples:same" uri="http://basex.org/modules/samples">
+
   <function name="samples:same" uri="http://basex.org/modules/samples" external="false">
     <parameter name="number" type="xs:integer">number to return</parameter>
+
     <argument name="number" type="xs:integer">number to return</argument>
 
     <annotation name="private" uri="http://www.w3.org/2012/xquery"/>
 
     <annotation name="private" uri="http://www.w3.org/2012/xquery"/>
 
     <description>This function simply returns the specified integer.</description>
 
     <description>This function simply returns the specified integer.</description>
    <return>specified number</return>
 
 
     <return type="xs:integer">specified number</return>
 
     <return type="xs:integer">specified number</return>
 
   </function>
 
   </function>
Line 118: Line 266:
 
</pre>
 
</pre>
  
When [[#inspect:xqdoc|inspect:xqdoc]] is run, the output looks as follows:
+
The output looks as follows if {{Code|inspect:xqdoc('sample.xqm')}} is called:
  
<pre class="brush:xml">
+
<pre lang="xml">
 
<xqdoc:xqdoc xmlns:xqdoc="http://www.xqdoc.org/1.0">
 
<xqdoc:xqdoc xmlns:xqdoc="http://www.xqdoc.org/1.0">
 
   <xqdoc:control>
 
   <xqdoc:control>
     <xqdoc:date>2013-05-31T08:43:05.123+02:00</xqdoc:date>
+
     <xqdoc:date>2013-06-01T16:59:33.654+02:00</xqdoc:date>
 
     <xqdoc:version>1.1</xqdoc:version>
 
     <xqdoc:version>1.1</xqdoc:version>
 
   </xqdoc:control>
 
   </xqdoc:control>
Line 129: Line 277:
 
     <xqdoc:uri>http://basex.org/modules/samples</xqdoc:uri>
 
     <xqdoc:uri>http://basex.org/modules/samples</xqdoc:uri>
 
     <xqdoc:name>sample.xqm</xqdoc:name>
 
     <xqdoc:name>sample.xqm</xqdoc:name>
 +
    <xqdoc:comment>
 +
      <xqdoc:description>This module provides some sample functions to demonstrate
 +
the features of the Inspection Module.</xqdoc:description>
 +
      <xqdoc:author>BaseX Team</xqdoc:author>
 +
      <xqdoc:see>http://docs.basex.org/wiki/XQDoc_Module</xqdoc:see>
 +
      <xqdoc:version>1.0</xqdoc:version>
 +
    </xqdoc:comment>
 
   </xqdoc:module>
 
   </xqdoc:module>
  <xqdoc:imports/>
 
 
   <xqdoc:namespaces>
 
   <xqdoc:namespaces>
 
     <xqdoc:namespace prefix="samples" uri="http://basex.org/modules/samples"/>
 
     <xqdoc:namespace prefix="samples" uri="http://basex.org/modules/samples"/>
 
   </xqdoc:namespaces>
 
   </xqdoc:namespaces>
 +
  <xqdoc:imports/>
 
   <xqdoc:variables>
 
   <xqdoc:variables>
 
     <xqdoc:variable>
 
     <xqdoc:variable>
Line 168: Line 323:
 
</xqdoc:xqdoc>
 
</xqdoc:xqdoc>
 
</pre>
 
</pre>
 +
 +
=Errors=
 +
 +
{| class="wikitable" width="100%"
 +
! width="110"|Code
 +
|Description
 +
|- valign="top"
 +
|{{Code|parse}}
 +
|Error while parsing a module.
 +
|- valign="top"
 +
|{{Code|unknown}}
 +
|The specified component does not exist.
 +
|}
  
 
=Changelog=
 
=Changelog=
 +
 +
;Version 11.0
 +
* Removed: {{Code|inspect:function-annotations}}
 +
 +
;Version 9.6
 +
* Updated: {{Function||inspect:type}}: options added
 +
 +
;Version 9.3
 +
* Added: {{Function||inspect:type}}
 +
 +
;Version 8.5
 +
* Added: {{Function||inspect:function-annotations}}, {{Function||inspect:static-context}}
 +
* Updated: {{Code|external}} attribute added to variables and functions
 +
* Updated: Relative URIs will always be resolved against the static base URI of the query
 +
 +
;Version 7.9
 +
* Updated: a query URI can now be specified with {{Function||inspect:functions}}.
  
 
This module was introduced with Version 7.7.
 
This module was introduced with Version 7.7.
 
[[Category:XQuery]]
 

Latest revision as of 17:39, 1 December 2023

This XQuery Module contains functions for extracting internal information about modules and functions and generating documentation.

With Version 11, inspect:function-annotations has been removed as it is now available in the official specification as fn:function-annotations.

Conventions[edit]

All functions and errors in this module are assigned to the http://basex.org/modules/inspect namespace, which is statically bound to the inspect prefix.
xqDoc document instances are assigned to the http://www.xqdoc.org/1.0 namespace, which is statically bound to the xqdoc prefix.

Reflection[edit]

inspect:functions[edit]

Signature
inspect:functions(
  $href  as xs:string  := ()
) as function(*)*
Summary Returns function items for all user-defined functions (both public and private) that are known in the current query context. If an $href value is specified, the specified resource will be retrieved as a string and compiled, and its functions will be added to the query context and returned to the user. A relative URI will be resolved against the static base URI of the query.
Examples Invokes the declared functions and returns their values:
declare %private function local:one() { 12 };
declare %private function local:two() { 34 };
for $f in inspect:functions() return $f()

Compiles all functions in code.xqm and invokes the function named run:

let $uri := 'code.xqm'
let $name := 'run'
for $f in inspect:functions($uri)
where local-name-from-QName(function-name($f)) = $name
return $f()
Errors parse: Error while parsing a module.

inspect:static-context[edit]

Signature
inspect:static-context(
  $function  as function(*)?,
  $name      as xs:string
) as item()*
Summary Returns a component of the static context of a $function with the specified $name. If no function is supplied, the current static context is considered.
The following components can be requested:
  • base-uri: Static base URI.
  • namespaces: Prefix/URI map with all statically known namespaces.
  • element-namespace: Default element/type namespace URI, or an empty sequence if it is absent.
  • function-namespace: Default function namespace URI, or an empty sequence if it is absent.
  • collation: URI of the default collation.
  • ordering: Ordering mode (ordered/unordered)
  • construction: Construction mode (preserve/strip)
  • default-order-empty: Default order for empty sequences (greatest/least)
  • boundary-space: Boundary-space policy (preserve/strip)
  • copy-namespaces: Copy-namespaces mode (inherit/no-inherit, preserve/no-preserve)
  • decimal-formats: Nested map with all statically known decimal formats
Examples
  • Returns the static base URI (same as static-base-uri()):
inspect:static-context((), 'base-uri')
  • Returns a map with all namespaces that are statically known in the module of the specified function:
import module namespace data = 'data.xqm';
inspect:static-context(data:get#1, 'namespaces')
Errors unknown: The specified component does not exist.

Documentation[edit]

inspect:type[edit]

Signature
inspect:type(
  $input    as item()*,
  $options  as map(*)?  := map { }
) as xs:string
Summary Returns a string representation of the type of a the given $input:
  • The string includes the occurrence indicator.
  • The type of functions and nodes may be stricter than the returned type.
  • For type checking, the standard expressions typeswitch and instance of should be used instead.

The following $options are available:

  • item: If enabled, only the item type is returned and the occurrence indicator is omitted. The default is false().
  • mode: If value is specified, the assigned type of the result value is returned. With expression the type of the input expression is returned (please note that the original expression may already have been rewritten at compile-time). With computed, the exact value is computed at runtime, based on the expression and the result value. The default is computed.
Examples
  • inspect:type((<a/>, <a/>)) yields element(a)+
  • inspect:type(map { 'a': (1, 2)[. = 1] }) yields map(xs:string, xs:integer)
  • inspect:type(1 to 100, map { 'item': true() }) yields xs:integer

inspect:function[edit]

Signature
inspect:function(
  $function  as function(*)
) as element(function)
Summary Inspects the specified $function and returns an element that describes its structure. The output of this function is similar to eXist-db’s inspect:inspect-function function.
Examples The query inspect:function(count#1) yields:
<function name="count" uri="http://www.w3.org/2005/xpath-functions" external="false">
  <argument type="item()" occurrence="*"/>
  <return type="xs:integer"/>
</function>

The function…

(:~
 : This function simply returns the specified integer.
 : @param $number  number to return
 : @return         specified number
 :)
declare %private function local:same($number as xs:integer) as xs:integer {
  $number
};

…is represented by inspect:function(local:same#1) as…

<function name="local:same" uri="http://www.w3.org/2005/xquery-local-functions" external="false">
  <argument type="xs:integer" name="number">number to return</argument>
  <annotation name="private" uri="http://www.w3.org/2012/xquery"/>
  <description>This function simply returns the specified integer.</description>
  <return type="xs:integer">specified number</return>
</function>

inspect:context[edit]

Signature
inspect:context() as element(context)
Summary Generates an element that describes all variables and functions in the current query context.
Examples Evaluate all user-defined functions with zero arguments in the query context:
inspect:context()/function ! function-lookup(QName(@uri, @name), 0) ! .()

Return the names of all private functions in the current context:

for $f in inspect:context()/function
where $f/annotation/@name = 'private'
return $f/@name/string()

inspect:module[edit]

Signature
inspect:module(
  $uri  as xs:string
) as element(module)
Summary Retrieves the resource located at the specified $uri, parses it as XQuery module, and generates an element that describes the module's structure. A relative URI will be resolved against the static base URI of the query.
Examples An example is shown below.
Errors parse: Error while parsing a module.

inspect:xqdoc[edit]

Signature
inspect:xqdoc(
  $uri  as xs:string
) as element(xqdoc:xqdoc)
Summary Retrieves the resource located at the specified $uri, parses it as XQuery module, and generates an xqDoc element. A relative URI will be resolved against the static base URI of the query.
xqDoc provides a simple vendor-neutral solution for generating documentation from XQuery modules. The documentation conventions have been inspired by the JavaDoc standard. Documentation comments begin with (:~ and end with :), and tags start with @. xqDoc comments can be specified for main and library modules and variable and function declarations.

We have slightly extended the xqDoc conventions to do justice to more recent versions of XQuery (Schema: xqdoc-1.1.30052013.xsd):

  • an <xqdoc:annotations/> node is added to each variable or function that uses annotations. The xqdoc:annotation child nodes may have additional xqdoc:literal elements with type attributes (xs:string, xs:integer, xs:decimal, xs:double) and values.
  • a single <xqdoc:namespaces/> node is added to the root element, which summarizes all prefixes and namespace URIs used or declared in the module.
  • name and type elements are added to variables.
Examples An example is shown below.
Errors parse: Error while parsing a module.

Examples[edit]

This is the sample.xqm library module:

(:~ 
 : This module provides some sample functions to demonstrate
 : the features of the Inspection Module.
 :
 : @author   BaseX Team
 : @see      http://docs.basex.org/wiki/XQDoc_Module
 : @version  1.0
 :)
module namespace samples = 'http://basex.org/modules/samples';

(:~ This is a sample string. :)
declare variable $samples:test-string as xs:string := 'this is a string';

(:~
 : This function simply returns the specified integer.
 : @param   $number number to return
 : @return  specified number
 :)
declare %private function samples:same($number as xs:integer) as xs:integer {
  $number
};

If inspect:module('sample.xqm') is run, the following output will be generated:

<module prefix="samples" uri="http://basex.org/modules/samples">
  <description>This module provides some sample functions to demonstrate
the features of the Inspection Module.</description>
  <author>BaseX Team</author>
  <see>http://docs.basex.org/wiki/XQDoc_Module</see>
  <version>1.0</version>
  <variable name="samples:test-string" uri="http://basex.org/modules/samples" type="xs:string" external="false">
    <description>This is a sample string.</description>
  </variable>
  <function name="samples:same" uri="http://basex.org/modules/samples" external="false">
    <argument name="number" type="xs:integer">number to return</argument>
    <annotation name="private" uri="http://www.w3.org/2012/xquery"/>
    <description>This function simply returns the specified integer.</description>
    <return type="xs:integer">specified number</return>
  </function>
</module>

The output looks as follows if inspect:xqdoc('sample.xqm') is called:

<xqdoc:xqdoc xmlns:xqdoc="http://www.xqdoc.org/1.0">
  <xqdoc:control>
    <xqdoc:date>2013-06-01T16:59:33.654+02:00</xqdoc:date>
    <xqdoc:version>1.1</xqdoc:version>
  </xqdoc:control>
  <xqdoc:module type="library">
    <xqdoc:uri>http://basex.org/modules/samples</xqdoc:uri>
    <xqdoc:name>sample.xqm</xqdoc:name>
    <xqdoc:comment>
      <xqdoc:description>This module provides some sample functions to demonstrate
the features of the Inspection Module.</xqdoc:description>
      <xqdoc:author>BaseX Team</xqdoc:author>
      <xqdoc:see>http://docs.basex.org/wiki/XQDoc_Module</xqdoc:see>
      <xqdoc:version>1.0</xqdoc:version>
    </xqdoc:comment>
  </xqdoc:module>
  <xqdoc:namespaces>
    <xqdoc:namespace prefix="samples" uri="http://basex.org/modules/samples"/>
  </xqdoc:namespaces>
  <xqdoc:imports/>
  <xqdoc:variables>
    <xqdoc:variable>
      <xqdoc:name>samples:test-string</xqdoc:name>
      <xqdoc:comment>
        <xqdoc:description>This is a sample string.</xqdoc:description>
      </xqdoc:comment>
      <xqdoc:type>xs:string</xqdoc:type>
    </xqdoc:variable>
  </xqdoc:variables>
  <xqdoc:functions>
    <xqdoc:function arity="1">
      <xqdoc:comment>
        <xqdoc:description>This function simply returns the specified integer.</xqdoc:description>
        <xqdoc:param>$number number to return</xqdoc:param>
        <xqdoc:return>specified number</xqdoc:return>
      </xqdoc:comment>
      <xqdoc:name>samples:same</xqdoc:name>
      <xqdoc:annotations>
        <xqdoc:annotation name="private"/>
      </xqdoc:annotations>
      <xqdoc:signature>declare %private function samples:same($number as xs:integer) as xs:integer</xqdoc:signature>
      <xqdoc:parameters>
        <xqdoc:parameter>
          <xqdoc:name>number</xqdoc:name>
          <xqdoc:type>xs:integer</xqdoc:type>
        </xqdoc:parameter>
      </xqdoc:parameters>
      <xqdoc:return>
        <xqdoc:type>xs:integer</xqdoc:type>
      </xqdoc:return>
    </xqdoc:function>
  </xqdoc:functions>
</xqdoc:xqdoc>

Errors[edit]

Code Description
parse Error while parsing a module.
unknown The specified component does not exist.

Changelog[edit]

Version 11.0
  • Removed: inspect:function-annotations
Version 9.6
Version 9.3
Version 8.5
Version 7.9

This module was introduced with Version 7.7.