首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xmlstarlet将XML转换为CSV

使用xmlstarlet将XML转换为CSV
EN

Stack Overflow用户
提问于 2020-04-27 22:06:38
回答 1查看 82关注 0票数 0

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

代码语言:javascript
复制
<?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>
      ..
      ..

我试过了

代码语言:javascript
复制
xml sel -T -t -m '/OAI-PMH/ListRecords/record/metadata' test.xml

代码语言:javascript
复制
xml sel -t -v "//*[local-name()='metadata']" test.xml

我只想转换成CSV格式的文件,但没有运气。

代码语言:javascript
复制
title lang us; title lang fr; description1; description2

有谁可以帮我?

提前感谢你的帮助……

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-28 00:08:32

这里有很多名称空间。有一个100%确定的更好的方法(xmlstarlet支持命名空间),但这应该是可行的:

代码语言:javascript
复制
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

代码语言:javascript
复制
title en;title fr;description1;description2

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

代码语言:javascript
复制
<?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>

编辑:更优雅的命令行:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61460740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档