更正xml文件格式,并将其应用于文件夹和子文件夹中的所有xml文件



我有大约800个xml文件,格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns = "http://xspf.org/ns/0/">
<trackList><track>
  <location>../ppa/1/50/01 - Taknavazi.111</location>
  <title>Taknavazi</title>
  <creator>1</creator>
    <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</track><track>
  <location>../ppa/1/50/02 - Saz-o-Avaz - Daramad AbuAta.111</location>
  <title>Sazo avaz-Darama abouatta</title>
  <creator>2</creator>
    <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</track><track>
  <location>../ppa/1/50/03 - Saz-o-Avaz - Hejaz-JameDaran-   Hejaz.111</location>
  <title>Sazo avaz-Hejaz-jamehdaran-hejaz</title>
  <creator>3</creator>
    <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</track><track>
  <location>../ppa/1/50/04 - Saz-o-Avaz - KordBayat-Ouj-Esfahanak-Forod.111</location>
  <title>Sazo avaz-bayate kord-ooj-esfahanak-foroud</title>
  <creator>4</creator>
    <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</track><track>
  <location>../ppa/1/50/05 - Saz-o-Avaz - Dashtestani-KhosroShirin-Dashtestaniat.111</location>
  <title>Sazo avaz-dashtestani-khosroo va shirin-dashtestaniyat</title>
  <creator>5</creator>
    <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</track><track>
  <location>../ppa/1/50/06 - Tasnif - Bahare Delkash.111</location>
  <title>Tasnif- bahare delkash</title>
  <creator>6</creator>
    <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</track></trackList>

我想更改它的格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns = "http://xspf.org/ns/0/">
<item>
  <file>../ppa/1/50/01 - Taknavazi.mp3</file>
  <title>Taknavazi</title>
  <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</item>
<item>
  <file>../ppa/1/50/02 - Saz-o-Avaz - Daramad AbuAta.mp3</file>
  <title>Sazo avaz-Darama abouatta</title>
  <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</item>
<item>
  <file>../ppa/1/50/03 - Saz-o-Avaz - Hejaz-JameDaran-Hejaz.mp3</file>
  <title>Sazo avaz-Hejaz-jamehdaran-hejaz</title>
  <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</item>
<item>
  <file>../ppa/1/50/04 - Saz-o-Avaz - KordBayat-Ouj-Esfahanak-Forod.mp3</file>
  <title>Sazo avaz-bayate kord-ooj-esfahanak-foroud</title>
  <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</item>
<item>
  <file>../ppa/1/50/05 - Saz-o-Avaz - Dashtestani-KhosroShirin-Dashtestaniat.mp3</file>
  <title>Sazo avaz-dashtestani-khosroo va shirin-dashtestaniyat</title>
  <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</item>
<item>
  <file>../ppa/1/50/06 - Tasnif - Bahare Delkash.mp3</file>
  <title>Tasnif- bahare delkash</title>
  <image>../images/Albumssmall70/Eshgh danad70.jpg</image>
</item>

请告诉我,有没有一种方法可以纠正一个xml文件,然后将新格式应用于文件夹及其子文件夹中的所有其他xml文件?提前感谢

我会使用XSLT

首先,您需要为格式之间的转换创建一个样式表:

transform.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:pl="http://xspf.org/ns/0/"
    exclude-result-prefixes="pl"
>
  <xsl:template match="/">
  <playlist version="1" xmlns="http://xspf.org/ns/0/">
    <xsl:for-each select="//pl:track">
      <item>
        <file><xsl:value-of select="pl:location"/>.mp3</file>
        <xsl:copy-of select="pl:title" />
        <image><xsl:value-of select="pl:image"/></image>
      </item>
    </xsl:for-each>
  </playlist>
  </xsl:template>
</xsl:stylesheet>

注意,我已经注册了名称空间pl来寻址源xmls名称空间的元素。为了避免pl命名空间包含在输出xml中,我使用了exclude-result-prefixes="pl"

现在您可以测试单个文件的转换。我使用的是xsltprocxmllint,您可能需要先安装它们。在Debian/Ubuuntu上,你需要执行

sudo apt-get install xmllint

对于测试转换,请使用以下命令(注意,我使用xmllint进行格式化)

xsltproc transform.xml file123.xml | xmllint --pretty 1 -

您应该得到问题中描述的结果xml。如果你想把它保存到一个文件中,只需在shell中使用输出重定向:

xsltproc transform.xml file123.xml | xmllint --pretty 1 - > output.xml

要一次转换所有输入xml,您需要编写一个小的shell脚本。假设输入文件位于input,输出文件应存储在output:

for file in input/*.xml ; do
    output_path="output/$(basename "$file")"
    xsltproc test.xsl "$file" 
        | xmllint --pretty 1 - > "$output_path"
done
Saxon XSLT处理器的URI解析器提供了打开多个XML文件的能力:
<xsl:variable name="all"
              select="collection('./?select="*.xml;recurse=yes;on-error=ignore')"/>

然后,您可以遍历所有文档,为每个文档打开一个新结果:

<xsl:for-each select="$all">
  <xsl:result-document href="...determine output URI here...">
    <xsl:apply-templates... (or whatever)/>
  </xsl:result-document>
</xsl:for-each>

最新更新