Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 在bash中使用XPath输出多个文件_Xml_Bash_Shell_Xpath_Batch Processing - Fatal编程技术网

Xml 在bash中使用XPath输出多个文件

Xml 在bash中使用XPath输出多个文件,xml,bash,shell,xpath,batch-processing,Xml,Bash,Shell,Xpath,Batch Processing,我有一个XML文件目录。每个文件都有自己的唯一标识符。每个文件还包含对其他文件的一个或多个引用(在单独的目录中),这些文件也具有唯一的ID 例如,我有一个名为example01.xml的文件: <file> <fileId>xyz123</fileId> <fileContents>Blah blah Blah</fileContents> <relatedFiles> <oth

我有一个XML文件目录。每个文件都有自己的唯一标识符。每个文件还包含对其他文件的一个或多个引用(在单独的目录中),这些文件也具有唯一的ID

例如,我有一个名为
example01.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>

提前感谢。

类似的方法可能会奏效:

#!/bin/bash

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 $3}'); do
    echo  "Moving $f to ${fid}_${uid}.xml"
    cp "$f" "${fid}_${uid}.xml"
  done
  rm "$f"
done

类似的方法可能会奏效:

#!/bin/bash

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 $3}'); do
    echo  "Moving $f to ${fid}_${uid}.xml"
    cp "$f" "${fid}_${uid}.xml"
  done
  rm "$f"
done

这在示例文件中非常有效,但我使用的
@href
属性的实际值如下所示:
http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&id=1234
。如何在脚本中解析属性值?明白了!只需在
awk
脚本中将字段值更改为$4。感谢您的帮助。这对示例文件非常有效,但我正在使用的
@href
属性的实际值如下所示:
http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&id=1234
。如何在脚本中解析属性值?明白了!只需在
awk
脚本中将字段值更改为$4。谢谢你的帮助。