Code Monkey home page Code Monkey logo

xpath.js's Introduction

xpath.js

An xpath module for node, written in pure javascript.

Originally written by Cameron McCormack (blog).

Prepared as a node module by Yaron Naveh (blog).

Install

Install with npm:

npm install xpath.js

xpath.js is xml engine agnostic but I recommend to use xmldom:

npm install xmldom

Your first xpath:

	var select = require('xpath.js')
	  , dom = require('xmldom').DOMParser

	var xml = "<book><title>Harry Potter</title></book>"
	var doc = new dom().parseFromString(xml)    
	var nodes = select(doc, "//title")
	console.log(nodes[0].localName + ": " + nodes[0].firstChild.data)
	console.log("node: " + nodes[0].toString())

-->

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

Get text values directly

    var xml = "<book><title>Harry Potter</title></book>"
    var doc = new dom().parseFromString(xml)    
    var title = select(doc, "//title/text()")[0].data   
    console.log(title)

-->

Harry Potter

Namespaces

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

-->

myns

Attributes

    var xml = "<book author='J. K. Rowling'><title>Harry Potter</title></book>"
    var doc = new dom().parseFromString(xml)    
    var author = select(doc, "/book/@author")[0].value    
    console.log(author)

-->

J. K. Rowling

xpath.js's People

Contributors

yaronn avatar

Stargazers

Hamster.Xu avatar

Watchers

wmingjian avatar James Cloos avatar

Forkers

toohamster

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.