xidel提取Pubmed节点时报invalid type for union错误如何解决
错误原因
XPath 中的 | 是节点集联合运算符,只能作用于节点类型的集合,你写的两个表达式返回的都是 join() 生成的字符串,不是节点,所以触发类型错误 err:XPTY0004。
正确实现方式
核心逻辑是先筛选出两类目标节点,再对每个节点统一做内容提取,不要对处理后的字符串结果做联合。
可直接运行的命令如下:
xidel -s -e '//*[self::PubmedArticle or self::BookDocument]/join((if (name()="PubmedArticle") then MedlineCitation/PMID else PMID, normalize-space(outer-xml())), x:cps(9))' 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&id=22259814,26978846'
上述表达式的逻辑:
- 先用
//*[self::PubmedArticle or self::BookDocument]筛选出所有PubmedArticle和BookDocument节点,这一步是对节点做筛选,完全符合 XPath 语法要求 - 对每个筛选出的节点做判断:如果是
PubmedArticle就取子节点MedlineCitation/PMID,如果是BookDocument就直接取子节点PMID - 把 PMID 和节点格式化后的 XML 内容用制表符拼接输出,和你之前两条命令的输出结果完全一致
如果你偏好使用 | 联合节点集的写法,效果完全相同:
xidel -s -e '(//PubmedArticle | //BookDocument)/join((if (name()="PubmedArticle") then MedlineCitation/PMID else PMID, normalize-space(outer-xml())), x:cps(9))' 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&id=22259814,26978846'
内容的提问来源于stack exchange,提问作者user1424739
相关产品推荐
相关产品推荐

