Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用node.js xpath xmldom读取XML文件_Node.js_Xml_Xpath_Xmldom - Fatal编程技术网

使用node.js xpath xmldom读取XML文件

使用node.js xpath xmldom读取XML文件,node.js,xml,xpath,xmldom,Node.js,Xml,Xpath,Xmldom,我试图使用节点包XPATH&XMLDOM来选择XML文档的某些部分,但是对于元素值我一无所获。我猜这可能是我的XPATH定义,但老实说我不知道 我的XML的顶部如下所示: <?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" xmln

我试图使用节点包XPATH&XMLDOM来选择XML文档的某些部分,但是对于元素值我一无所获。我猜这可能是我的XPATH定义,但老实说我不知道

我的XML的顶部如下所示:

<?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"
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);
第一个测试生成了预期的结果,但我只得到了一个“test2”错误。我是否错过了显而易见的事情


谢谢。

您只是忘记了在元素上用
xmlns=“…”
定义的名称空间被继承到子节点。
因此,行
xmlns=”http://www.cdisc.org/ns/odm/v1.3“
在您的XML中,使所有子项都具有此名称空间(一个
ODM

//ODM:Study/ODM:GlobalVariables/ODM:StudyName
把它放在整个表达式中

console.log("test 2 : " + select('//ODM:Study/ODM:GlobalVariables/ODM:StudyName', doc)[0].nodeValue);