xml - 在带有 not() 的 bash 中使用 XPath

标签 xml bash shell xpath batch-processing

这是 previous question 的后续行动关于在 bash 中使用 XPath。

我有一组 XML 文件,其中大部分编码与其他文件的关系:

<file>
    <fileId>xyz123</fileId>
    <fileContents>Blah blah Blah</fileContents>
    <relatedFiles>
        <otherFile href='http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&amp;id=123‌​4'>
            <title>Some resource</title>
        </otherFile>
        <otherFile href='http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&amp;id=4321'>
            <title>Some other resource</title>
        </otherFile>
    </relatedFiles>
</file>

previous question 的答案帮助我成功处理了其中的大部分文件。但是,集合中有一些文件不包含任何 relatedFiles/otherFile 元素。我希望能够单独处理这些文件并将它们移动到“其他”文件夹中。我以为我可以使用 XPath not() 函数来执行此操作,但是当我运行脚本时,我收到该行的“找不到命令”错误。

#!/bin/bash

mkdir other
for f in *.xml; do
  fid=$(xpath -e '//fileId/text()' "$f" 2>/dev/null)   
  for uid in $(xpath -e '//otherFile/@href' "$f" 2>/dev/null | awk -F= '{gsub(/"/,"",$0); print $4}'); do
    echo  "Moving $f to ${fid:3}_${uid}.xml"
    cp "$f" "${fid:3}_${uid}.xml"    
  done      
  if $(xpath -e 'not(//otherFile)' "$f" 2>/dev/null); then            
    echo  "Moving $f to other/${fid:3}.xml"
    cp "$f" "other/${fid:3}.xml"              
  fi  
  rm "$f"    
done

如何在 bash 中使用 XPath 过滤掉不包含某些元素的文件?提前致谢。

最佳答案

$() 构造替换命令的输出。因此,无论 xpath 吐出什么都将被替换,shell 将尝试将其作为命令执行,这就是您收到错误消息的原因。

由于 xpath 似乎没有根据是否找到节点提供不同的退出代码,您可能只需要将输出与某些内容进行比较,或者测试是否为空:

if [ -z "$(xpath -q -e '//otherFile' "$f" 2>/dev/null)" ]; then

如果 xpath 没有输出,这应该执行以下代码。要颠倒意义,请使用 -n 而不是 -z(不确定您想要哪个)。

关于xml - 在带有 not() 的 bash 中使用 XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19082066/

相关文章:

c# - 在 XML 中存储文件名的正确方法是什么?

xml - 为什么命名空间限定的节点没有 XPath 语法?

linux - 无法用另一个字符串 shell 替换文件中的字符串

linux - shell map 是否限制 key 格式?

shell - 为每一行标准输出执行命令的简单 shell 解决方案

java - 如何使用eclipse解析我的junit selenium测试用例中的xml文件

c# - xdoc 查询的 Select 语句

regex - AWK 程序使用正则表达式来计算匹配行

bash - Hadoop作业配置文件规范

linux - 如果任何命令返回非零值,则中止 shell 脚本