-1

在 python xq 语法中有一些用于将 xml 解析为 csv 的终端命令(也需要安装 python-yq):

xq -r '[.yml_catalog.shop.offers.offer[] | [."@id", ."@available", .price, .name, .description]] | . |= [["id", "available", "price", "name", "description"]] + . | .[] | @csv' source.yml > result.csv

这是xml文件示例:

<?xml version="1.0" encoding="UTF-8"?>
  <yml_catalog date="2020-07-29 09:39">
    <shop>
      <offers>
        <offer id="17489" available="true">
          <price>1500</price>
          <name>Item 1</name>
          <description>Item 1 description</description>
          <param name="category type">sale</param>
          <param name="size">big</param>
        </offer>
        <offer id="17490" available="false">
          <price>1100</price>
          <name>Item 2</name>
          <description>Item 2 description</description>
          <param name="category type">main</param>
          <param name="size">small</param>
        </offer>
        ...
      </offers>
    </shop>
  </yml_catalog>

我需要添加参数标签进行解析。我试图通过."param[@name="category type"]" , ."param[@name=category type]来处理它(对于具有 size 属性的 param 标签也是如此),但它不起作用.

必要的输出csv:

"id","available","price","name","description","category","size"
"17489","true","1500","Item 1","Item 1 description","sale","big"
"17490","false","1100","Item 2","Item 2 description","main","small"

请给我建议如何处理具有确切属性和值的标签并获取其文本文本内容?

谢谢!

4

1 回答 1

0

看起来您正在尝试将 XML 输出转换为 CSV,因为xq也使用下面的jq语法,您可以使用以下

xq -r '["id","available","price","name","description","category","size"], 
       (.yml_catalog.shop.offers.offer[] | 
       [."@id", ."@available", .price, .name, .description, .param[]."#text"]) | 
       @csv' xml
于 2020-07-29T17:24:17.030 回答