Changes

Jump to navigation Jump to search
914 bytes added ,  17:39, 1 December 2023
m
Text replacement - "syntaxhighlight" to "pre"
This [[Module Library|XQuery Module]] provides functions for converting HTML to XML. The input Conversion will only be converted take place if [http://home.ccil.org/~cowan/XML/tagsoup/ TagSoup] is included in the classpath (see [[Parsers#HTML Parser|HTML Parsing]] for more details).
=Conventions=
All functions and errors in this module are assigned to the {{Code|<code><nowiki>http://basex.org/modules/html}} </nowiki></code> namespace, which is statically bound to the {{Code|html}} prefix.<br/>All errors are assigned to the {{Code|http://basex.org/errors}} namespace, which is statically bound to the {{Code|bxerr}} prefix.
=Functions=
==html:processordoc==
{| width='100%'
|-valign="top"| width='90120' | '''SignaturesSignature'''|{{Code|'''<pre>html:processor'''doc() $href as xs:string?, $options as map(*)? := map { }}) as document-node()?<br /pre>|-valign="top"
| '''Summary'''
|Returns Fetches the name of HTML document referred to by the applied HTML processor (currently: given {{Code|$href}}, converts it to XML and returns a document node. The {{Code|TagSoup$options}} or an argument can be used to set [[Parsers#Options|TagSoup Options]].|- valign="top"| ''empty string'Errors'). If the function returns an empty string, TagSoup was not found in the classpath, and ''|{{Error|parse|#Errors}} the input will cannot be parsed as well-formed converted to XML.<br />
|}
==html:parse==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>html:parse(
$value as xs:anyAtomicType,
$options as map(*)? := map { }
) as document-node()</pre>
|- valign="top"
| '''Summary'''
|Converts the HTML document specified by {{Code|$value}} to XML and returns a document node:<br/>
* The input may be of type {{Code|xs:string}}, {{Code|xs:base64Binary}}, or {{Code|xs:hexBinary}}.
* If the input is passed on in its binary representation, and if no encoding option is supplied, the HTML parser will try to choose the correct encoding automatically.
 
The {{Code|$options}} argument can be used to set [[Parsers#Options|TagSoup Options]].
|- valign="top"
| '''Errors'''
|{{Error|parse|#Errors}} the input cannot be converted to XML.
|}
 
==html:parser==
 
{| width='100%'
|-valign="top"| width='90120' | '''SignaturesSignature'''|{{FuncCode|'''html:parse|$input as xs:anyAtomicType|document-nodeparser'''()}}<br />{{Func|html:parse|$input as xs:anyAtomicType, $options as item()|document-node()string}}<br />|-valign="top"
| '''Summary'''
|Converts Returns the name of the applied HTML document specified by parser (currently: {{Code|$inputTagSoup}} to XML, and returns a document node. The input may either be a string or a binary item (xs:hexBinary, xs:base64Binary). If the input an ''empty string'' is passed on returned, TagSoup was not found in its binary representationthe classpath, and the HTML parser input will try to automatically choose the correct encodingbe treated as well-formed XML.<br/>The {{Code|$options}} argument can be used  =Examples= ===Basic Example=== The following query converts the specified string to customize the [[Parsers#TagSoup Options|HTML conversion]]an XML document node. ;Query:<pre lang='xquery'>html:parse("<html>")</pre> ;Result:<pre lang="xml"><html xmlns="http://www.w3. It can be specifiedorg/1999/xhtml"/><br /pre>* as children of ===Specifying Options=== The next query creates an {{Code|XML document with namespaces: ;Query:<pre lang='xquery'>html:optionsparse("<a href='ok.html'/>", map { 'nons': false() }} element)</pre> ; e.g.Result:<pre classlang="brush:xml"><htmlxmlns="http:options//www.w3.org/1999/xhtml"> <xslt:key1 valuebody> <a shape="rect" href='value1'"ok.html"/> ...</body></html></pre> ===Parsing Binary Input=== If the input encoding is unknown, the data to be processed can be passed on in its binary representation.The HTML parser will automatically try to detect the correct encoding:options ;Query:<pre lang='xquery'>html:parse(fetch:binary("https://en.wikipedia.org"))
</pre>
* as map, which contains all key/value pairs;Result:<pre classlang="brush:xml">map { <html xmlns="http://www.w3.org/1999/xhtml" class="client-nojs" dir="key1ltr" :lang= "value1en"> <head> <title>Wikipedia, the free encyclopedia</title> <meta charset="UTF-8"/> ... }
</pre>
|-
| '''Errors'''
|{{Error|BXHL0001|#Errors}} the input cannot be converted to XML.
|-
| '''Examples'''
|
* {{Code|html:parse("<html></html>")}} returns {{Code|<html/>}}
* <code><nowiki>html:parse(fetch:content-binary("http://en.wikipedia.org"))</nowiki></code> returns an XML representation of the English Wikipedia main page. The input is passed on its binary representation such that the HTML parser can automatically detect the correct encoding.
|}
=Errors=
{| width='100%' class="wikitable" width="100%"! width="5%110"|Code! width="95%"|Description|-valign="top"|{{Code|BXHL0001parse}}
|The input cannot be converted to XML.
|}
=Changelog=
The module was introduced with ;Version 79.5.14 * Added: {{Function||html:doc}} ;Version 9.0 * Updated: error codes updated; errors now use the module namespace
[[Category:XQuery]]The module was introduced with Version 7.6.
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu