Giter Site home page Giter Site logo

xpath's Introduction

XPath library

DOM 3 and 4 XPath 1.0 implemention for browser and Node.js environment with support for custom Function, Variable and Namespace mapping.

Requirements

Release Notes

See CHANGELOG.md

Usage

Install with npm:

npm install xpath-ts

This library is xml engine agnostic but I recommend to use xmldom-ts, xmldom or jsdom

npm install xmldom-ts

or

npm install xmldom

or

npm install jsdom

API Documentation

Can be found here. See below for example usage.

Your first xpath:

import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';

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

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

title: Harry Potter
Node: <title>Harry Potter</title>

Alternatively

Using the same interface you have on modern browsers (MDN)

import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';

const xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>";
const doc = new dom().parseFromString(xml);

xpath.installDOM3XPathSupport(doc);

const result = doc.evaluate(
  '/book/title', // xpathExpression
  doc, // contextNode
  null, // namespaceResolver
  xpath.XPathResult.ANY_TYPE, // resultType
  null // result
);

let node = result.iterateNext();
while (node) {
  console.log(node.localName + ': ' + node.firstChild.data);
  console.log('Node: ' + node.toString());

  node = result.iterateNext();
}

title: Harry Potter
Node: <title>Harry Potter</title>

Evaluate string values directly:

import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';

const xml = '<book><title>Harry Potter</title></book>';
const doc = new dom().parseFromString(xml);
const title = xpath.select('string(//title)', doc);

console.log(title);

Harry Potter

Namespaces

import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';

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

console.log(node.namespaceURI);

myns

Namespaces with easy mappings

import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';

const xml = "<book xmlns:bookml='http://example.com/book'><bookml:title>Harry Potter</bookml:title></book>";
const select = xpath.useNamespaces({ bookml: 'http://example.com/book' });

console.log(select('//bookml:title/text()', doc)[0].nodeValue);

Harry Potter

Default namespace with mapping

import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';

const xml = "<book xmlns='http://example.com/book'><title>Harry Potter</title></book>";
const select = xpath.useNamespaces({ bookml: 'http://example.com/book' });

console.log(select('//bookml:title/text()', doc)[0].nodeValue);

Harry Potter

Attributes

import { DOMParserImpl as dom } from 'xmldom-ts';
import * as xpath from 'xpath-ts';

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

console.log(author);

J. K. Rowling

Developing and Testing

Download

git clone 'https://github.com/backslash47/xpath-ts'
cd xpath-ts

Install

npm install

Build

npm run build

You will get the transpiled code under '/dist/lib' and typings under '/dist/types'.

Test

Run standard tests with Mocha + Chai testing framework

npm test

Authors

  • Cameron McCormack - Initial work - blog
  • Yaron Naveh - blog
  • goto100
  • Jimmy Rishe
  • Thomas Weinert
  • Matus Zamborsky - TypeScript rewrite - Backslash47
  • Others - others

Licence

This project is licensed under the MIT License - see the LICENCE.md file for details.

xpath's People

Contributors

jlrishe avatar backslash47 avatar goto100 avatar yaronn avatar delibes avatar teknopaul avatar ideahunter avatar callicles avatar thomasweinert avatar vzvu3k6k avatar bbyars avatar boreplusplus avatar evxn avatar ivandimanov avatar jkphl avatar mramos-dev avatar blu3r4y avatar darobin avatar shadiakiki1986 avatar joepie91 avatar eugenelim avatar gngeorgiev avatar totopsy avatar

Stargazers

Leonel Sanches da Silva avatar Alex Kreidler avatar Aaron Gray avatar Eaton avatar Juan J. Jimenez-Anca avatar minhaolee avatar Orr Bibring avatar Mark Stosberg avatar Hyeonsoo Lee avatar Konstantin Vyatkin avatar  avatar Plastikfan avatar Enioluwa Segun avatar  avatar PeterSong avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.