Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const xml =
<from>Bob</from>
<to>Alice</to>
<subject>Hello</subject>
<body>Bla bla bla</body>
<body type="text">Bla bla bla</body>
</message>`;

const ast = XmlReader.parseSync(xml);
Expand Down Expand Up @@ -76,6 +76,12 @@ Find by name. Including top level nodes and all its children.
xmlQuery(ast).find('body'); // xmlQuery containing the body element
```

Attributes can be provided to find elements which match the name and the attributes.

```javascript
xmlQuery(ast).find('body', {'type': 'text'}); // xmlQuery containing the body element which has a 'type' attribute of value 'text'
```

### .has()

Returns `true` if it has the given element. Faster than `find()` because it stops on first occurence.
Expand Down
4 changes: 3 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ declare const xmlQuery: (ast: xmlQuery.XmlNode | xmlQuery.XmlNode[]) => {
children: () => any;
each: (fn: (v: xmlQuery.XmlNode, i: number, a: xmlQuery.XmlNode[]) => void) => void;
eq: (index: number) => any;
find: (sel: string) => any;
find: (sel: string, attr?: {
[name: string]: string;
}) => any;
has: (sel: string) => any;
first: () => any;
get: (index: number) => xmlQuery.XmlNode;
Expand Down
12 changes: 7 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ var xmlQuery = function (ast) {
var children = function () {
return xmlQuery(flatMap(nodes, function (node) { return node.children; }));
};
var findInNode = function (node, sel) {
var res = (node.name === sel) ? [node] : [];
return res.concat(flatMap(node.children, function (node) { return findInNode(node, sel); }));
var findInNode = function (node, sel, attr) {
var reducer = function (acc, key) { return acc && (attr[key] === node.attributes[key]); };
var res = (node.name === sel && Object.keys(attr).reduce(reducer, true)) ? [node] : [];
return res.concat(flatMap(node.children, function (node) { return findInNode(node, sel, attr); }));
};
var find = function (sel) {
return xmlQuery(flatMap(nodes, function (node) { return findInNode(node, sel); }));
var find = function (sel, attr) {
if (attr === void 0) { attr = {}; }
return xmlQuery(flatMap(nodes, function (node) { return findInNode(node, sel, attr); }));
};
var has = function (sel) {
if (nodes.length === 0) {
Expand Down
11 changes: 6 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ const xmlQuery = (ast: xmlQuery.XmlNode | xmlQuery.XmlNode[]) => {
/**
* Recursively find by name starting in the provided node
*/
const findInNode = (node: xmlQuery.XmlNode, sel: string) => {
const res = (node.name === sel) ? [node] : [];
return res.concat(flatMap(node.children, (node) => findInNode(node, sel)));
const findInNode = (node: xmlQuery.XmlNode, sel: string, attr: { [name: string]: string }) => {
const reducer = (acc, key) => acc && (attr[key] === node.attributes[key]);
const res = (node.name === sel && Object.keys(attr).reduce(reducer, true)) ? [node] : [];
return res.concat(flatMap(node.children, (node) => findInNode(node, sel, attr)));
};

/**
* Find by name. Including top level nodes and all its children.
*/
const find = (sel: string) =>
xmlQuery(flatMap(nodes, (node) => findInNode(node, sel)));
const find = (sel: string, attr: { [name: string]: string } = {}) =>
xmlQuery(flatMap(nodes, (node) => findInNode(node, sel, attr)));

/**
* Returns true if it has the given element. Faster than find because it stops on first occurence.
Expand Down
Loading