Forked from maxcnunes/libxmljs-finding-xml-elements-with-namespaces-using-xpath.js
Created
August 16, 2018 03:09
-
-
Save TuHuynhVan/9e3e923b884c81d3516b7800b19c959d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var libxmljs = require("libxmljs"); | |
var xml = [ | |
'<?xml version="1.0" encoding="utf-8"?>', | |
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">', | |
' <soap:Body>', | |
' <Response xmlns="http://tempuri.org/">', | |
' <Result>', | |
' <client xmlns="">', | |
' <msg>SEARCH OK.</msg>', | |
' <code>0</code>', | |
' </client>', | |
' </Result>', | |
' </Response>', | |
' </soap:Body>', | |
'</soap:Envelope>' | |
].join(''); | |
var doc = libxmljs.parseXml(xml); | |
console.log('-->soap:Envelope', doc.get('/soap:Envelope')); | |
// does not find element | |
console.log('-->soap:Envelope', doc.get('/soap:Envelope', {'soap':'http://schemas.xmlsoap.org/soap/envelope/'})); | |
// finds element | |
console.log('-->soap:Body', doc.get('//soap:Body', {'soap':'http://schemas.xmlsoap.org/soap/envelope/'})); | |
// finds element | |
console.log('-->Response', doc.get('//xmlns:Response', 'http://tempuri.org/')); | |
// finds element | |
console.log('-->Response', doc.get('//Response')); | |
// does not find element | |
console.log('-->Result', doc.get('//Result')); | |
// does not find element | |
console.log('-->client', doc.get('//client')); | |
// finds element | |
console.log('-->msg', doc.get('//msg')); | |
// finds element | |
console.log('-->xmlns:Result', doc.get('//xmlns:Result', 'http://tempuri.org/').get('//msg')); | |
// finds element |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment