Skip to content

Commit

Permalink
Merge pull request #117 from goto100/xmldom
Browse files Browse the repository at this point in the history
Update usages of xmldom to @xmldom/xmldom
  • Loading branch information
JLRishe authored Jul 6, 2023
2 parents 6142157 + 2ee9e8b commit baacc3b
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 80 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ Can be found [here](https://github.com/goto100/xpath/blob/master/docs/xpath%20me

## Your first xpath:
`````javascript
var xpath = require('xpath')
, dom = require('xmldom').DOMParser
var xpath = require('xpath');
var dom = require('@xmldom/xmldom').DOMParser;

var xml = "<book><title>Harry Potter</title></book>"
var doc = new dom().parseFromString(xml)
var nodes = xpath.select("//title", doc)
var xml = "<book><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml, 'text/xml');
var nodes = xpath.select("//title", doc);

console.log(nodes[0].localName + ": " + nodes[0].firstChild.data)
console.log("Node: " + nodes[0].toString())
console.log(nodes[0].localName + ": " + nodes[0].firstChild.data);
console.log("Node: " + nodes[0].toString());
`````

Expand All @@ -46,15 +46,15 @@ Using the same interface you have on modern browsers ([MDN])

`````javascript
var node = null;
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>"
var doc = new dom().parseFromString(xml)
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml, 'text/xml');
var result = xpath.evaluate(
"/book/title", // xpathExpression
doc, // contextNode
null, // namespaceResolver
xpath.XPathResult.ANY_TYPE, // resultType
null // result
)
);

node = result.iterateNext();
while (node) {
Expand All @@ -72,7 +72,7 @@ while (node) {
## Evaluate string values directly:
`````javascript
var xml = "<book><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml);
var doc = new dom().parseFromString(xml, 'text/xml');
var title = xpath.select("string(//title)", doc);

console.log(title);
Expand All @@ -83,11 +83,11 @@ console.log(title);

## Namespaces
`````javascript
var xml = "<book><title xmlns='myns'>Harry Potter</title></book>"
var doc = new dom().parseFromString(xml)
var node = xpath.select("//*[local-name(.)='title' and namespace-uri(.)='myns']", doc)[0]
var xml = "<book><title xmlns='myns'>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml, 'text/xml');
var node = xpath.select("//*[local-name(.)='title' and namespace-uri(.)='myns']", doc)[0];

console.log(node.namespaceURI)
console.log(node.namespaceURI);
`````

Expand Down Expand Up @@ -117,11 +117,11 @@ console.log(select('//bookml:title/text()', doc)[0].nodeValue);

## Attributes
`````javascript
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>"
var doc = new dom().parseFromString(xml)
var author = xpath.select1("/book/@author", doc).value
var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>";
var doc = new dom().parseFromString(xml, 'text/xml');
var author = xpath.select1("/book/@author", doc).value;

console.log(author)
console.log(author);
`````

Expand Down
35 changes: 18 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
}
],
"devDependencies": {
"mocha": "^9.0.2",
"xmldom": "^0.6.0"
"@xmldom/xmldom": "^0.8.8",
"mocha": "^9.0.2"
},
"typings": "./xpath.d.ts",
"scripts": {
Expand All @@ -34,4 +34,4 @@
"xml"
],
"license": "MIT"
}
}
Loading

0 comments on commit baacc3b

Please sign in to comment.