我有一个XML,其中包含一组带有ID属性的标记,其值由数字和小写字符组成。
使用此示例就足以选择所需的标签:
doc <- XML::xmlTreeParse(file, useInternalNodes = TRUE)
myid <- "123_myID_567" # from <test> attributes
xpath <- paste("//node[@id='", myid,"']/subnode", sep = "")
df <- data.frame(t(sapply(XML::xpathApply(doc, xpath), XML::xmlAttrs)), stringsAsFactors = F)
但是,在相同的XML中,在其他标记中也使用这些相同的ID,但是,有时全部为小写,其他时间为大写。
尝试使用lower-case
解决方法似乎不适用于XML R包:
xpath <- paste("//node[lower-case(@id)='", myid,"']/subnode", sep = "")
我收到以下错误:
xmlXPathCompOpEval: function lower-case not found
XPath error : Unregistered function
XPath error : Invalid expression
XPath error : Stack usage error
Error in xpathApply.XMLInternalDocument(doc, xpath) :
error evaluating xpath expression //node[lower-case(@id)='123_myID_567']/subnode
Called from: xpathApply.XMLInternalDocument(doc, xpath)
尝试使用translate
似乎无效的其他解决方法:
xpath <- paste("//node[translate(@id,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='", myid,"']/subnode", sep = "")
虽然这次XPATH没有抱怨但却什么也没有回复。
尝试使用matches
解决方法似乎不适用于XML R包:
xpath <- paste("//node[matches(@id,'", myid,"','i')]/subnode", sep = "")
因为我得到了与第一次尝试相同的错误:
xmlXPathCompOpEval: function matches not found
XPath error : Unregistered function
XPath error : Invalid expression
<xml>
<tests>
<test id="123_myid_567" key="something" status="skipped">
<Paragraph>some text</Paragraph>
</test>
<test id="111_myid_888" key="something" status="skipped">
<Paragraph>some text</Paragraph>
</test>
<test id="222_myid_777" key="something" status="skipped">
<Paragraph>some text</Paragraph>
</test>
</tests>
<nodes>
<node id="123_myID_567" title="Sample title" value1="8" value2="4" value3="3.5">
<subnode> text1 </subnode>
<subnode2> text_1 </subnode2>
</node>
<node id="111_myid_888" title="Sample title2" value1="3" value2="4" value3="7.4">
<subnode> text2 </subnode>
<subnode2> text_2 </subnode2>
</node>
<node id="222_myid_777" title="Sample title3" value1="2" value2="5" value3="2.5">
<subnode> text3 </subnode>
<subnode2> text_3 </subnode2>
</node>
<node id="333_myID_567" title="Sample title4" value1="6" value2="7" value3="5.5">
<subnode> text4 </subnode>
<subnode2> text_4 </subnode2>
</node>
</nodes>
</xml>