我有一个 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&id=1234'>
<title>Some resource</title>
</otherFile>
<otherFile href='http://sub.domain.abc.edu/directory/index.php?p=collections/pageview&id=4321'>
<title>Some other resource</title>
</otherFile>
</relatedFiles>
</file>
如果一个文件有多个relatedFiles/otherFile
元素,我需要为每个元素创建一个文件副本@href
并重命名它,将唯一 ID@href
的值与fileID
. 因此,例如,我需要创建文件的两个副本example01.xml
,一个名为abc01_xyz123.xml
,另一个名为abc0002_xyz123.xml
。otherFile
这应该扩大以创建与元素一样多的副本。
现在,如果只有一个otherFile
元素,我有一个 bash 脚本可以执行此操作,但我的脚本技能有限,我无法弄清楚如何处理多个otherFile
元素。
#!/bin/bash
for f in *.xml;
do
name=`xpath -e 'string(//otherFile/@href)' $f 2> /dev/null`
echo "Moving" $f "to" ${name:3}.xml
echo $name
mv $f ${name:3}.xml
done
提前致谢。