This commit is contained in:
Josh Johnson 2016-01-18 08:53:42 -05:00
parent 65fca7bdf4
commit c6efe2a2dd
3 changed files with 18 additions and 2 deletions

View File

@ -78,6 +78,11 @@
"type": "array",
"default": ["-xquery", "$(script)", "-in", "$(input)", "-out", "$(input).output.xml"],
"description": "Arguments to be passed to the execution engine. '$(script)' and '$(input)' refer to the XQuery script and input XML file, respectively."
},
"xmlTools.ignoreDefaultNamespace": {
"type": "boolean",
"default": true,
"description": "Ignores default xmlns attribute when evaluating XPath."
}
}
},

View File

@ -6,6 +6,7 @@ import { XPathEvaluator } from '../services/XPathEvaluator';
const CFG_SECTION: string = 'xmlTools';
const CFG_PERSIST_QUERY: string = 'persistXPathQuery';
const CFG_IGNORE_DEFAULT_XMLNS: string = 'ignoreDefaultNamespace';
const MEM_QUERY_HISTORY: string = 'xpathQueryHistory';
const MEM_QUERY_LAST: string = 'xPathQueryLast';
const OUTPUT_CHANNEL: string = 'XPath Results';
@ -54,9 +55,11 @@ export class XPathFeatureProvider {
// showInputBox() will return undefined if the user dimissed the prompt
if (query) {
let ignoreDefaultNamespace: boolean = vsc.workspace.getConfiguration(CFG_SECTION).get<boolean>(CFG_IGNORE_DEFAULT_XMLNS, true);
// run the query
let xml: string = editor.document.getText();
let nodes: Node[] = XPathEvaluator.evaluate(query, xml);
let nodes: Node[] = XPathEvaluator.evaluate(query, xml, ignoreDefaultNamespace);
// show the results to the user
let outputChannel: vsc.OutputChannel = vsc.window.createOutputChannel(OUTPUT_CHANNEL);

View File

@ -5,10 +5,18 @@ import * as xpath from 'xpath';
let DOMParser = require('xmldom').DOMParser;
export class XPathEvaluator {
static evaluate(query: string, xml: string): Node[] {
static evaluate(query: string, xml: string, ignoreDefaultNamespace: boolean): Node[] {
if (ignoreDefaultNamespace) {
xml = xml.replace(/xmlns=".+"/g, (match: string) => {
return match.replace(/xmlns/g, 'xmlns:default');
});
}
let nodes: Node[] = new Array<Node>();
let xdoc: Document = new DOMParser().parseFromString(xml, 'text/xml');
console.log(xdoc);
let resolver: xpath.XPathNSResolver = xpath.createNSResolver(xdoc);
let expression: xpath.XPathExpression = xpath.createExpression(query, resolver);
let result: xpath.XPathResult = expression.evaluate(xdoc, xpath.XPathResult.ORDERED_NODE_ITERATOR_TYPE);