Socket
Socket
Sign inDemoInstall

xpath.js

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    xpath.js

Xpath pure javascript implementation for node.js


Version published
Maintainers
1
Install size
127 kB
Created

Readme

Source

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, twitter).

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

License

MIT

Keywords

FAQs

Last updated on 04 Jan 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc