Newer
Older
libxmljs-extra / docs / Document.md
# Document
A copy of the Document class from `libxmljs` with extra methods and existing ones modified.

## Constructor
### new Document(source = null, type = Document.TYPE_XML, options = null);
>Creates a new `Document`.  

>**args**  
*source*: `string` (optional) - The source as a string.  
*type*: `number` (optional) - The type of the `Document`.  
*options*: `libxmls.ParserOptions` (optional) - The parser options.  


## Properties

### static TYPE_XML: `number`
>Describe the `Document` as an XML document.  

>**value**  
0


### static TYPE_HTML: `number`
>Describe the `Document` as an HTML document.  

>**value**  
1


### static TYPE_HTML_FRAGMENT: `number`
>Describe the `Document` as an HTML fragment.  

>**value**  
2


### _original: `libxmljs.Document|null`
>The internal `libxmljs.Document` instance.


### _hasContent: `boolean`
>Tells whether the `Document` has content.


### namespace: `object|null`
>The object that holds the namespace information.


### errors: `SyntaxError[]`
>An array containing the errors in the current `Document`.


### validationErrors: `ValidationError[]|undefined`
>An array containing the validation errors in the current `Document`.


## Methods

### child(idx): `libxmljs.Element|null`
>Gets the idxth child of the root node.  

>**args**  
*idx*: `number` - The child index.  

>**returns** A `libxmljs.Element` or `null`.

### childNodes(): `libxmljs.Element[]`
>Gets all the children of the root node.  

>**returns** An array of `libxmljs.Element`s.


### count(xpath): `number`
>Counts the amount of results of the provided XPath.  

>**args**  
*xpath*: `string` - The XPath to count.  

>**returns** The amount of results.


### encoding(): `string`
>Gets the `Document`'s encoding.  

>**returns** The `Document`'s encoding.


### encoding(enc): `Document`
>Sets the `Document`'s encoding.  

>***args**  
*enc*: `string` - The encoding as a string.

>**returns** The `Document`.


### find(xpath): `libxmljs.Element[]`
>Finds the result of the provided XPath.  

>**args**  
*xpath*: `string` - The XPath to find.  

>**returns** An array of `libxmljs.Element`s.


### fromHtml(html, options = {}): `Document`
>Parses an HTML document.  

>**args**  
*html*: `string` - The HTML content.  
*options*: `libxmljs.ParserOptions` (optional) - The parser options.  

>**returns** The `Document`.


### fromHtmlFragment(htmlFragment, options = {}): `Document`
>Parses an HTML fragment.  

>**args**  
*htmlFragment*: `string` - The HTML content.  
*options*: `libxmljs.ParserOptions` (optional) - The parser options.  

>**returns** The `Document`.


### fromXml(xml, options = {}): `Document`
>Parses an XML document.  

>**args**  
*xml*: `string` - The XML content.  
*options*: `libxmljs.ParserOptions` (optional) - The parser options.  

>**returns** The `Document`.


### get(xpath): `libxmljs.Element`
>Gets the first result of the provided XPath.  

>**args**  
*xpath*: `string` - The XPath to get.  

>**returns** A `libxmljs.Element`.


### getDtd(): `object`
>Gets the `Document`'s DTD.  

>**returns** The DTD object.


### namespaces(): `libxmljs.Namespace[]`
>Gets the `Document`'s namespaces.  

>**returns** An array of `libxmljs.Namespace`s.


### node(name, content): `libxmljs.Node`
>Creates the root.  

>**args**  
*name*: `string` - The root's tag name.  
*content*: `string` - The root's text content.  

>**returns** The created `libxmljs.Node`.


### rngValidate(rng): `boolean`
>Checks whether the `Document` is valid using Relax NG.  

>**args**  
*rng*: `object` - The Relax NG.  

>**returns** Whether the `Document` is valid using Relax NG.


### root(): `libxmljs.Element|null`
>Gets the root.  

>**returns** The root or `null`.


### root(node): `libxmljs.Element`
>Sets the root.  

>**args**  
*node*: `libxmljs.Element` - The root `libxmljs.Element`.  

>**returns** The created `libxmljs.Element`.


### setDtd(name, ext, sys): `Document`
>Sets the `Document`'s DTD.  

>**args**  
*name*: `string` - The name of the DTD.  
*ext*: `string` - The external ID for the DTD.  
*sys*: `string` - The system ID for the DTD.  

>**returns** The `Document`.


### setNamespace(alias, url): `Document`
>Sets the `Document`'s namespace.  

>**args**  
*alias*: `string` - The alias of the namespace.  
*url*: `string` - The URL of the namespace.  

>**returns** The `Document`.


### toString(formatting = true): `string`
>Returns the `Document` as a string.  

>**args**  
*formatting*: `boolean` (optional) - Tells whether the ouput is formatted.  

>**returns** The `Document` as a string.


### type(): `string`
>Returns the string "document".  

>**returns** "document".


### validate(xsdDoc): `boolean`
>Validates the `Document` against a XSD document.  

>**args**  
*xsdDoc*: `Document` - The XSD `Document`.  

>**returns** Whether the `Document` is valid. `validationErrors` contains the errors if any.


### version(): `string`
>Gets the `Document`'s version.  

>**returns** The `Document`'s version.


### assertNoContentError(): `void`
>Throws an error if the `Document` has no content.