我对xml非常陌生,我正在尝试使用R中的xml2包读取一个xml2文件。
演示sbml文件摘自sbml主页面。
对于如何使用xpath搜索节点,我感到困惑。
例如,我试过
test <- read_xml("./scratch.xml")
xml_children(test)[1]
xml_attr(xml_children(test)[1], "name")工作,并给我"EnzymaticReaction"作为答案。但是,我不想按索引访问节点,而是按名称访问--所以我尝试了
xml_find_one(test, ".//model")这给我带来了错误
Error: No matches有人能帮我知道我在xpath打电话时做错了什么吗?下面还粘贴了sbml文件。
谢谢!
<?xml version="1.0" encoding="UTF-8"?>
<sbml level="2" version="3" xmlns="http://www.sbml.org/sbml/level2/version3">
<model name="EnzymaticReaction">
<listOfUnitDefinitions>
<unitDefinition id="per_second">
<listOfUnits>
<unit kind="second" exponent="-1"/>
</listOfUnits>
</unitDefinition>
<unitDefinition id="litre_per_mole_per_second">
<listOfUnits>
<unit kind="mole" exponent="-1"/>
<unit kind="litre" exponent="1"/>
<unit kind="second" exponent="-1"/>
</listOfUnits>
</unitDefinition>
</listOfUnitDefinitions>
<listOfCompartments>
<compartment id="cytosol" size="1e-14"/>
</listOfCompartments>
<listOfSpecies>
<species compartment="cytosol" id="ES" initialAmount="0" name="ES"/>
<species compartment="cytosol" id="P" initialAmount="0" name="P"/>
<species compartment="cytosol" id="S" initialAmount="1e-20" name="S"/>
<species compartment="cytosol" id="E" initialAmount="5e-21" name="E"/>
</listOfSpecies>
<listOfReactions>
<reaction id="veq">
<listOfReactants>
<speciesReference species="E"/>
<speciesReference species="S"/>
</listOfReactants>
<listOfProducts>
<speciesReference species="ES"/>
</listOfProducts>
<kineticLaw>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<ci>cytosol</ci>
<apply>
<minus/>
<apply>
<times/>
<ci>kon</ci>
<ci>E</ci>
<ci>S</ci>
</apply>
<apply>
<times/>
<ci>koff</ci>
<ci>ES</ci>
</apply>
</apply>
</apply>
</math>
<listOfParameters>
<parameter id="kon" value="1000000" units="litre_per_mole_per_second"/>
<parameter id="koff" value="0.2" units="per_second"/>
</listOfParameters>
</kineticLaw>
</reaction>
<reaction id="vcat" reversible="false">
<listOfReactants>
<speciesReference species="ES"/>
</listOfReactants>
<listOfProducts>
<speciesReference species="E"/>
<speciesReference species="P"/>
</listOfProducts>
<kineticLaw>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<times/>
<ci>cytosol</ci>
<ci>kcat</ci>
<ci>ES</ci>
</apply>
</math>
<listOfParameters>
<parameter id="kcat" value="0.1" units="per_second"/>
</listOfParameters>
</kineticLaw>
</reaction>
</listOfReactions>
</model>
</sbml>发布于 2016-01-11 21:54:08
在输入文档中,有一个默认的命名空间。
<sbml xmlns="http://www.sbml.org/sbml/level2/version3">默认情况下,它适用于所有元素。类似于XPath的表达式
//model意思是在中寻找一个元素,而不是命名空间--但是在您的文档中,没有位于任何名称空间中的model节点。
我对R不熟悉,所以我只能提出一些更多的解决办法,而不是一个答案。解决方法是不直接提及元素的名称,而是使用类似于
//*[local-name() = 'model']但是忽略名称空间不如在代码中显式地提到它们。
同时,我读到了这个这里.
真正的解决方案是使用一种方法在R代码中从输入文档中声明名称空间URI,并在XPath表达式中使用前缀。我认为正确的方法是
ns <- xml_ns_rename(xml_ns(test), d1 = "sbml")
xml_find_one(test, "/sbml:sbml/sbml:model", ns)重命名并不是绝对必要的,但它是有用的。XML文档中的默认名称空间由此XML库命名为d1、d2等。
https://stackoverflow.com/questions/34726469
复制相似问题