Difference between revisions of "Fetch Module"

From BaseX Documentation
Jump to navigation Jump to search
(Added an example to show fetch:text() with optional attributes)
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
This [[Module Library|XQuery Module]] provides simple functions to fetch the content of resources identified by URIs. Resources can be stored locally or remotely and e.g. use the {{Code|file://}} or {{Code|http://}} scheme. If more control over HTTP requests is required, the [[HTTP Module]] can be used. With the [[HTML Module]], retrieved HTML documents can be converted to XML.
+
This [[Module Library|XQuery Module]] provides simple functions to fetch the content of resources identified by URIs. Resources can be stored locally or remotely and e.g. use the {{Code|file://}} or {{Code|http://}} scheme. If more control over HTTP requests is required, the [[HTTP Client Module]] can be used. With the [[HTML Module]], retrieved HTML documents can be converted to XML.
  
 
=Conventions=
 
=Conventions=
  
All functions in this module are assigned to the <code><nowiki>http://basex.org/modules/fetch</nowiki></code> namespace, which is statically bound to the {{Code|fetch}} prefix.<br/>
+
All functions and errors in this module are assigned to the <code><nowiki>http://basex.org/modules/fetch</nowiki></code> namespace, which is statically bound to the {{Code|fetch}} prefix.<br/>
All errors are assigned to the <code><nowiki>http://basex.org/errors</nowiki></code> namespace, which is statically bound to the {{Code|bxerr}} prefix.
 
  
 
URI arguments can point be URLs or point to local files. Relative file paths will be resolved against the ''current working directory'' (for more details, have a look at the [[File Module#File Paths|File Module]]).
 
URI arguments can point be URLs or point to local files. Relative file paths will be resolved against the ''current working directory'' (for more details, have a look at the [[File Module#File Paths|File Module]]).
Line 18: Line 17:
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Fetches the resource referred to by the given URI and returns it as [[Streaming Module|streamable]] {{Code|xs:base64Binary}}.
+
|Fetches the resource referred to by the given URI and returns it as [[Lazy Module|lazy]] {{Code|xs:base64Binary}} item.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|{{Error|BXFE0001|XQuery Errors#Functions Errors}} the URI could not be resolved, or the resource could not be retrieved.
+
|{{Error|open|#Errors}} the URI could not be resolved, or the resource could not be retrieved.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
 
* <code><nowiki>fetch:binary("http://images.trulia.com/blogimg/c/5/f/4/679932_1298401950553_o.jpg")</nowiki></code> returns the addressed image.
 
* <code><nowiki>fetch:binary("http://images.trulia.com/blogimg/c/5/f/4/679932_1298401950553_o.jpg")</nowiki></code> returns the addressed image.
* <code><nowiki>stream:materialize(fetch:binary("http://en.wikipedia.org"))</nowiki></code> returns a materialized representation of the streamable result.
+
* <code><nowiki>lazy:cache(fetch:binary("http://en.wikipedia.org"))</nowiki></code> enforces the fetch operation (otherwise, it will be delayed until requested first).
 
|}
 
|}
  
Line 37: Line 36:
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Fetches the resource referred to by the given {{Code|$uri}} and returns it as [[Streaming Module|streamable]] {{Code|xs:string}}:
+
|Fetches the resource referred to by the given {{Code|$uri}} and returns it as [[Lazy Module|lazy]] {{Code|xs:string}} item:
 
* The UTF-8 default encoding can be overwritten with the optional {{Code|$encoding}} argument.
 
* The UTF-8 default encoding can be overwritten with the optional {{Code|$encoding}} argument.
 
* By default, invalid characters will be rejected. If {{Code|$fallback}} is set to true, these characters will be replaced with the Unicode replacement character <code>FFFD</code> (&#xFFFD;).
 
* By default, invalid characters will be rejected. If {{Code|$fallback}} is set to true, these characters will be replaced with the Unicode replacement character <code>FFFD</code> (&#xFFFD;).
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|{{Error|BXFE0001|XQuery Errors#Functions Errors}} the URI could not be resolved, or the resource could not be retrieved.<br/>{{Error|BXFE0002|XQuery Errors#Functions Errors}} the specified encoding is not supported, or unknown.
+
|{{Error|open|#Errors}} the URI could not be resolved, or the resource could not be retrieved.<br/>{{Error|encoding|#Errors}} the specified encoding is not supported, or unknown.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
Line 48: Line 47:
 
* <code><nowiki>fetch:text("http://en.wikipedia.org")</nowiki></code> returns a string representation of the English Wikipedia main HTML page.
 
* <code><nowiki>fetch:text("http://en.wikipedia.org")</nowiki></code> returns a string representation of the English Wikipedia main HTML page.
 
* <code><nowiki>fetch:text("http://www.bbc.com","US-ASCII",true())</nowiki></code> returns the BBC homepage in US-ASCII with all non-US-ASCII characters replaced with &#xFFFD;.
 
* <code><nowiki>fetch:text("http://www.bbc.com","US-ASCII",true())</nowiki></code> returns the BBC homepage in US-ASCII with all non-US-ASCII characters replaced with &#xFFFD;.
* <code><nowiki>stream:materialize(fetch:text("http://en.wikipedia.org"))</nowiki></code> returns a materialized representation of the streamable result.
+
* <code><nowiki>lazy:cache(fetch:text("http://en.wikipedia.org"))</nowiki></code> enforces the fetch operation (otherwise, it will be delayed until requested first).
 
|}
 
|}
  
Line 56: Line 55:
 
|-
 
|-
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|fetch:xml|$uri as xs:string|document-node()}}<br/>{{Func|fetch:xml|$uri as xs:string, $options as map(*)|document-node()}}
+
|{{Func|fetch:xml|$uri as xs:string|document-node()}}<br/>{{Func|fetch:xml|$uri as xs:string, $options as map(*)?|document-node()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Fetches the resource referred to by the given {{Code|$uri}} and returns it as XML document node.<br/>In contrast to <code>fn:doc</code>, each function call returns a different document node. As a consequence, document instances created by this function will not be kept in memory until the end of query evaluation.<br/>The {{Code|$options}} argument can be used to change the parsing behavior. Allowed options are all [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options in lower case.
+
|Fetches the resource referred to by the given {{Code|$uri}} and returns it as document node.<br/>The {{Code|$options}} argument can be used to change the parsing behavior. Allowed options are all [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options in lower case.<br/>The function is different to <code>fn:doc</code> in various aspects:
 +
* As it is non-deterministic, a new document node will be created by each call of this function.
 +
* A document created by this function will be garbage-collected as soon as it is not referenced anymore.
 +
* URIs will not be resolved against existing databases. As a result, it will not trigger any locks (see [[Transaction Management#Limitations|limitations of database locking]] for more details).
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|{{Error|BXFE0001|XQuery Errors#Functions Errors}} the URI could not be resolved, or the resource could not be retrieved.
+
|{{Error|open|#Errors}} the URI could not be resolved, or the resource could not be retrieved.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
 
* Retrieve an XML representation of the English Wikipedia main HTML page, chop all whitespace nodes:
 
* Retrieve an XML representation of the English Wikipedia main HTML page, chop all whitespace nodes:
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
fetch:xml("http://en.wikipedia.org", map { 'chop': true() })
 
fetch:xml("http://en.wikipedia.org", map { 'chop': true() })
</pre>
+
</syntaxhighlight>
 
* Return a document located in the current base directory:
 
* Return a document located in the current base directory:
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
fetch:xml(file:base-dir() || "example.xml")
 
fetch:xml(file:base-dir() || "example.xml")
</pre>
+
</syntaxhighlight>
 +
* Return a web page as XML, preserve namespaces:
 +
<syntaxhighlight lang="xquery">
 +
fetch:xml(
 +
  'http://basex.org/',
 +
  map {
 +
    'parser': 'html',
 +
    'htmlparser': map { 'nons': false() }
 +
  }
 +
)
 +
</syntaxhighlight>
 
|}
 
|}
  
 
==fetch:xml-binary==
 
==fetch:xml-binary==
 
{{Mark|Introduced with Version 8.7:}}
 
  
 
{| width='100%'
 
{| width='100%'
 
|-
 
|-
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|fetch:xml-binary|$data as xs:base64Binary|document-node()}}<br/>{{Func|fetch:xml-binary|$data as xs:base64Binary, $options as map(*)|document-node()}}
+
|{{Func|fetch:xml-binary|$data as xs:base64Binary|document-node()}}<br/>{{Func|fetch:xml-binary|$data as xs:base64Binary, $options as map(*)?|document-node()}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Parses binary {{Code|$data}} and returns it as XML document node.<br/>In contrast to fn:parse-xml, which expects an XQuery string, the input of this function can be arbitrarily encoded. The encoding will be derived from the XML declaration or (in case of UTF16 or UTF32) from the first bytes of the input.<br/>The {{Code|$options}} argument can be used to change the parsing behavior. Allowed options are all [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options in lower case.
+
|Parses binary {{Code|$data}} and returns it as document node.<br/>In contrast to fn:parse-xml, which expects an XQuery string, the input of this function can be arbitrarily encoded. The encoding will be derived from the XML declaration or (in case of UTF16 or UTF32) from the first bytes of the input.<br/>The {{Code|$options}} argument can be used to change the parsing behavior. Allowed options are all [[Options#Parsing|parsing]] and [[Options#XML Parsing|XML parsing]] options in lower case.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
 
* Retrieves file input as binary data and parses it as XML:
 
* Retrieves file input as binary data and parses it as XML:
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
fetch:xml-binary(file:read-binary('doc.xml'))
 
fetch:xml-binary(file:read-binary('doc.xml'))
</pre>
+
</syntaxhighlight>
 
* Encodes a string as CP1252 and parses it as XML. The input and the string {{Code|touché}} will be correctly decoded because of the XML declaration:
 
* Encodes a string as CP1252 and parses it as XML. The input and the string {{Code|touché}} will be correctly decoded because of the XML declaration:
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
fetch:xml-binary(convert:string-to-base64(
 
fetch:xml-binary(convert:string-to-base64(
 
   "<?xml version='1.0' encoding='CP1252'?><xml>touché</xml>",
 
   "<?xml version='1.0' encoding='CP1252'?><xml>touché</xml>",
 
   "CP1252"
 
   "CP1252"
 
))
 
))
</pre>
+
</syntaxhighlight>
 
* Encodes a string as UTF16 and parses it as XML. The document will be correctly decoded, as the first bytes of the data indicate that the input must be UTF16:
 
* Encodes a string as UTF16 and parses it as XML. The document will be correctly decoded, as the first bytes of the data indicate that the input must be UTF16:
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
fetch:xml-binary(convert:string-to-base64("<xml/>", "UTF16"))
 
fetch:xml-binary(convert:string-to-base64("<xml/>", "UTF16"))
</pre>
+
</syntaxhighlight>
 
|}
 
|}
  
Line 120: Line 130:
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|{{Error|BXFE0001|XQuery Errors#Functions Errors}} the URI could not be resolved, or the resource could not be retrieved.
+
|{{Error|open|#Errors}} the URI could not be resolved, or the resource could not be retrieved.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
Line 133: Line 143:
 
|Description
 
|Description
 
|-
 
|-
|{{Code|BXFE0001}}
+
|{{Code|encoding}}
 +
|The specified encoding is not supported, or unknown.
 +
|-
 +
|{{Code|open}}
 
|The URI could not be resolved, or the resource could not be retrieved.
 
|The URI could not be resolved, or the resource could not be retrieved.
|-
 
|{{Code|BXFE0002}}
 
|The specified encoding is not supported, or unknown.
 
 
|}
 
|}
  
 
=Changelog=
 
=Changelog=
  
;Version 8.7
+
;Version 9.0
  
 
* Added: [[#fetch:xml-binary|fetch:xml-binary]]
 
* Added: [[#fetch:xml-binary|fetch:xml-binary]]
 +
* Updated: error codes updated; errors now use the module namespace
  
 
;Version 8.5
 
;Version 8.5

Latest revision as of 16:21, 27 February 2020

This XQuery Module provides simple functions to fetch the content of resources identified by URIs. Resources can be stored locally or remotely and e.g. use the file:// or http:// scheme. If more control over HTTP requests is required, the HTTP Client Module can be used. With the HTML Module, retrieved HTML documents can be converted to XML.

Conventions[edit]

All functions and errors in this module are assigned to the http://basex.org/modules/fetch namespace, which is statically bound to the fetch prefix.

URI arguments can point be URLs or point to local files. Relative file paths will be resolved against the current working directory (for more details, have a look at the File Module).

Functions[edit]

fetch:binary[edit]

Signatures fetch:binary($uri as xs:string) as xs:base64Binary
Summary Fetches the resource referred to by the given URI and returns it as lazy xs:base64Binary item.
Errors open: the URI could not be resolved, or the resource could not be retrieved.
Examples
  • fetch:binary("http://images.trulia.com/blogimg/c/5/f/4/679932_1298401950553_o.jpg") returns the addressed image.
  • lazy:cache(fetch:binary("http://en.wikipedia.org")) enforces the fetch operation (otherwise, it will be delayed until requested first).

fetch:text[edit]

Signatures fetch:text($uri as xs:string) as xs:string
fetch:text($uri as xs:string, $encoding as xs:string) as xs:string
fetch:text($uri as xs:string, $encoding as xs:string, $fallback as xs:boolean) as xs:string
Summary Fetches the resource referred to by the given $uri and returns it as lazy xs:string item:
  • The UTF-8 default encoding can be overwritten with the optional $encoding argument.
  • By default, invalid characters will be rejected. If $fallback is set to true, these characters will be replaced with the Unicode replacement character FFFD (�).
Errors open: the URI could not be resolved, or the resource could not be retrieved.
encoding: the specified encoding is not supported, or unknown.
Examples
  • fetch:text("http://en.wikipedia.org") returns a string representation of the English Wikipedia main HTML page.
  • fetch:text("http://www.bbc.com","US-ASCII",true()) returns the BBC homepage in US-ASCII with all non-US-ASCII characters replaced with �.
  • lazy:cache(fetch:text("http://en.wikipedia.org")) enforces the fetch operation (otherwise, it will be delayed until requested first).

fetch:xml[edit]

Signatures fetch:xml($uri as xs:string) as document-node()
fetch:xml($uri as xs:string, $options as map(*)?) as document-node()
Summary Fetches the resource referred to by the given $uri and returns it as document node.
The $options argument can be used to change the parsing behavior. Allowed options are all parsing and XML parsing options in lower case.
The function is different to fn:doc in various aspects:
  • As it is non-deterministic, a new document node will be created by each call of this function.
  • A document created by this function will be garbage-collected as soon as it is not referenced anymore.
  • URIs will not be resolved against existing databases. As a result, it will not trigger any locks (see limitations of database locking for more details).
Errors open: the URI could not be resolved, or the resource could not be retrieved.
Examples
  • Retrieve an XML representation of the English Wikipedia main HTML page, chop all whitespace nodes:
fetch:xml("http://en.wikipedia.org", map { 'chop': true() })
  • Return a document located in the current base directory:
fetch:xml(file:base-dir() || "example.xml")
  • Return a web page as XML, preserve namespaces:
fetch:xml(
  'http://basex.org/',
  map {
    'parser': 'html',
    'htmlparser': map { 'nons': false() }
  }
)

fetch:xml-binary[edit]

Signatures fetch:xml-binary($data as xs:base64Binary) as document-node()
fetch:xml-binary($data as xs:base64Binary, $options as map(*)?) as document-node()
Summary Parses binary $data and returns it as document node.
In contrast to fn:parse-xml, which expects an XQuery string, the input of this function can be arbitrarily encoded. The encoding will be derived from the XML declaration or (in case of UTF16 or UTF32) from the first bytes of the input.
The $options argument can be used to change the parsing behavior. Allowed options are all parsing and XML parsing options in lower case.
Examples
  • Retrieves file input as binary data and parses it as XML:
fetch:xml-binary(file:read-binary('doc.xml'))
  • Encodes a string as CP1252 and parses it as XML. The input and the string touché will be correctly decoded because of the XML declaration:
fetch:xml-binary(convert:string-to-base64(
  "<?xml version='1.0' encoding='CP1252'?><xml>touché</xml>",
  "CP1252"
))
  • Encodes a string as UTF16 and parses it as XML. The document will be correctly decoded, as the first bytes of the data indicate that the input must be UTF16:
fetch:xml-binary(convert:string-to-base64("<xml/>", "UTF16"))

fetch:content-type[edit]

Signatures fetch:content-type($uri as xs:string) as xs:string
Summary Returns the content-type (also called mime-type) of the resource specified by $uri:
  • If a remote resource is addressed, the request header will be evaluated.
  • If the addressed resource is locally stored, the content-type will be guessed based on the file extension.
Errors open: the URI could not be resolved, or the resource could not be retrieved.
Examples
  • fetch:content-type("http://docs.basex.org/skins/vector/images/wiki.png") returns image/png.

Errors[edit]

Code Description
encoding The specified encoding is not supported, or unknown.
open The URI could not be resolved, or the resource could not be retrieved.

Changelog[edit]

Version 9.0
  • Added: fetch:xml-binary
  • Updated: error codes updated; errors now use the module namespace
Version 8.5
Version 8.0

The module was introduced with Version 7.6.