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='90120' | '''SignaturesSignature'''|{{Func|<pre>html:parse|( $input value as xs:anyAtomicType|, $options as map(*)? := map { }) as document-node()}}<br /pre>|-valign="top"
| '''Summary'''
|Converts the HTML document specified by {{Code|$inputvalue}} to XML, and returns a document node. :<br/>* The input may either be a of type {{Code|xs:string or a binary item (}}, {{Code|xs:hexBinarybase64Binary}}, or {{Code|xs:base64Binary)hexBinary}}. * If the input is passed on in its binary representation, and if no encoding option is supplied, the HTML parser will try to automatically choose the correct encodingautomatically. The {{Code|$options}} argument can be used to set [[Parsers#Options|TagSoup Options]].|-valign="top"
| '''Errors'''
|{{Error|BXHL0001parse|#Errors}} the input cannot be converted to XML.|} ==html:parser== {| width='100%'|-valign="top"| width='120'| 'Examples''Signature'''|* {{Code|'''html:parseparser'''("<html></html>")as xs:string}} returns {{Code|<htmlbr/>}}* <code><nowiki>html:parse(fetch:content|-binary(valign="http://en.wikipedia.orgtop"))</nowiki></code> returns an XML representation | '''Summary'''|Returns the name of the English Wikipedia main pageapplied HTML parser (currently: {{Code|TagSoup}}). The input If an ''empty string'' is passed on its binary representation such that returned, TagSoup was not found in the HTML parser can automatically detect classpath, and the correct encodinginput will be treated as well-formed XML.<br/>
|}
=Examples=
===Basic Example===
The following query converts the specified string to an XML document node.
;Query:
<pre lang='xquery'>
html:parse("<html>")
</pre>
;Result:
<pre lang="xml">
<html xmlns="http://www.w3.org/1999/xhtml"/>
</pre>
===Specifying Options===
The next query creates an XML document with namespaces:
;Query:
<pre lang='xquery'>
html:parse("<a href='ok.html'/>", map { 'nons': false() })
</pre>
;Result:
<pre lang="xml">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<a shape="rect" href="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:
;Query:
<pre lang='xquery'>
html:parse(fetch:binary("https://en.wikipedia.org"))
</pre>
;Result:
<pre lang="xml">
<html xmlns="http://www.w3.org/1999/xhtml" class="client-nojs" dir="ltr" lang="en">
<head>
<title>Wikipedia, the free encyclopedia</title>
<meta charset="UTF-8"/>
...
</pre>
=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.