I'm attempting to select certain parts of an XML document using the node packages XPATH & XMLDOM, but I am getting nothing through for the element values. At a guess it's probably my XPATH definition, but to be honest I've no idea.
The top of my XML looks like the following:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="define2-0-0.xsl"?>
<ODM
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.cdisc.org/ns/odm/v1.3"
xmlns:def="http://www.cdisc.org/ns/def/v2.0"
ODMVersion="1.3.2"
FileType="Snapshot"
FileOID="StudyNum.ADaM-IG.1.0"
CreationDateTime="2018-02-08T09:40:51">
<Study OID="StudyNum.ADaM-IG.1.0">
<GlobalVariables>
<StudyName>StudyNum</StudyName>
<StudyDescription>Study Description</StudyDescription>
<ProtocolName>StudyName_PRCL_StudyNum</ProtocolName>
</GlobalVariables>
<MetaDataVersion OID="MDV.StudyNum.ADaM-IG.1.0" Name="Study StudyNum Data Definitions"
Description="Awful Syndrome"
def:DefineVersion="2.0.0"
and my code so far looks like the following:
var xpath = require('xpath'),
dom = require('xmldom').DOMParser,
fs = require('fs');
var xml = fs.readFileSync('./Define/define.xml', 'utf8').toString();
var select = xpath.useNamespaces({"xlink":"http://www.w3.org/1999/xlink", "ODM":"http://www.cdisc.org/ns/odm/v1.3", "def":"http://www.cdisc.org/ns/def/v2.0"});
var doc = new dom().parseFromString(xml)
console.log("test 1 : " + select('//ODM:Study/@OID', doc)[0].nodeValue);
console.log("test 2 : " + select('//ODM:Study/GlobalVariables/StudyName/', doc)[0].nodeValue);
The first test generates the expected result, but I just get an error with 'test 2'. Am I missing the obvious?
Thanks.
You simply forgot that namespaces defined with xmlns="..."
on an element are inherited to the child nodes.
So the line xmlns="http://www.cdisc.org/ns/odm/v1.3"
in your XML makes all the children having this (an ODM
) namespace.
//ODM:Study/ODM:GlobalVariables/ODM:StudyName
Putting that in the whole expression it's
console.log("test 2 : " + select('//ODM:Study/ODM:GlobalVariables/ODM:StudyName', doc)[0].nodeValue);