我正在尝试使用节点包 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);
第一个测试生成了预期的结果,但我收到了“测试 2”的错误。我错过了明显的吗?
谢谢。
您只是忘记了xmlns="..."
在元素上定义的命名空间会继承到子节点。
因此xmlns="http://www.cdisc.org/ns/odm/v1.3"
,您的 XML 中的这一行使所有子代都拥有这个 (an ODM
) 命名空间。
//ODM:Study/ODM:GlobalVariables/ODM:StudyName
把它放在整个表达式中
console.log("test 2 : " + select('//ODM:Study/ODM:GlobalVariables/ODM:StudyName', doc)[0].nodeValue);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句