XML 使用 xmlstarlet 到 CSV

XML to CSV with xmlstarlet

我得到了一些 OAI-PMH XML 格式如下的文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/bundles/ojsoai/oai2.xsl" ?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/          http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
  <responseDate>2015-12-26T23:54:31Z</responseDate>
  <request verb="ListRecords" metadataPrefix="oai_dc">http://oai_dc/</request>
  <ListRecords>
    <record>
      <header>
        <identifier>identifier</identifier>
        <datestamp>2015-12-01T00:00:00Z</datestamp>
        <setSpec>iksad</setSpec>
      </header>
      <metadata>
        <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/      http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
          <dc:title xml:lang="en-US">title en</dc:title>
          <dc:title xml:lang="fr-FR">title fr</dc:title>
          <dc:creator>creator</dc:creator>
          <dc:subject>subject</dc:subject>
          <dc:description>description1</dc:description>
          <dc:description>description2</dc:description>
          <dc:publisher>publisher</dc:publisher>
          <dc:date>2015-12-01T00:00:00Z</dc:date>
          <dc:type>type</dc:type>
          <dc:format>application/pdf</dc:format>
          <dc:identifier>identifier</dc:identifier>
          <dc:identifier/>
          <dc:source xml:lang="en-US">source</dc:source>
          <dc:source>source</dc:source>
          <dc:source>source</dc:source>
          <dc:language>en</dc:language>
          <dc:relation>relation</dc:relation>
        </oai_dc:dc>
      </metadata>
    </record>
    <record>
      <header>
      ..
      ..

我试过了

xml sel -T -t -m '/OAI-PMH/ListRecords/record/metadata' test.xml

xml sel -t -v "//*[local-name()='metadata']" test.xml

我只想将 CSV 文件格式转换为但没有成功。

title lang us; title lang fr; description1; description2

谁能帮帮我?

提前感谢您的帮助...

这里有很多命名空间。有 100% 肯定有更好的方法来做到这一点(xmlstarlet 有名称空间支持)但这应该有效:

xml sel -T -t -m //*[name()='record'] -v "concat(*[contains(name(),'metadata')]/*/*[contains(name(),'title')][1],';',*[contains(name(),'metadata')]/*/*[contains(name(),'title')][2],';',*[contains(name(),'metadata')]/*/*[contains(name(),'description')][1],';',*[contains(name(),'metadata')]/*/*[contains(name(),'description')][2])" -n data.xml >toto.csv

输出:toto.csv

title en;title fr;description1;description2

我已经修复了您的示例 XML 以使用 xmlstarlet 进行测试。 data.xml :

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/bundles/ojsoai/oai2.xsl" ?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/          http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
  <responseDate>2015-12-26T23:54:31Z</responseDate>
  <request verb="ListRecords" metadataPrefix="oai_dc">http://oai_dc/</request>
  <ListRecords>
    <record>
      <header>
        <identifier>identifier</identifier>
        <datestamp>2015-12-01T00:00:00Z</datestamp>
        <setSpec>iksad</setSpec>
      </header>
      <metadata>
        <oai_dc:dc xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/      http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
          <dc:title xml:lang="en-US">title en</dc:title>
          <dc:title xml:lang="fr-FR">title fr</dc:title>
          <dc:creator>creator</dc:creator>
          <dc:subject>subject</dc:subject>
          <dc:description>description1</dc:description>
          <dc:description>description2</dc:description>
          <dc:publisher>publisher</dc:publisher>
          <dc:date>2015-12-01T00:00:00Z</dc:date>
          <dc:type>type</dc:type>
          <dc:format>application/pdf</dc:format>
          <dc:identifier>identifier</dc:identifier>
          <dc:identifier/>
          <dc:source xml:lang="en-US">source</dc:source>
          <dc:source>source</dc:source>
          <dc:source>source</dc:source>
          <dc:language>en</dc:language>
          <dc:relation>relation</dc:relation>
        </oai_dc:dc>
      </metadata>
    </record>
</ListRecords>
</OAI-PMH>

编辑:更优雅的命令行:

xml sel -N x="http://purl.org/dc/elements/1.1/" -t -m "//_:record" -v "concat(_:metadata/*/x:title[@xml:lang='en-US'],';',_:metadata/*/x:title[@xml:lang='fr-FR'],';',_:metadata/*/x:description[1],';',_:metadata/*/x:description[2])" -n data.xml >toto.csv