使用Python或XSLT,我想知道如何将高度复杂的分层嵌套XML文件转换为CSV,包括所有子元素,而无需硬编码,尽可能少的元素节点,或者是否合理/有效
请查看附件中的简化XML示例和输出CSV,以便更好地了解我要实现的目标
实际的XML文件包含更多的元素,但数据层次结构和嵌套与示例中类似<InvoiceRow>
元素及其子元素是XML文件中唯一的重复元素,所有其他元素都是静态的,在输出CSV中重复的次数与XML文件中的<InvoiceRow>
元素相同
是重复的<InvoiceRow>
元素给我带来了麻烦。不重复的元素很容易转换为CSV,而无需硬编码任何元素
复杂的XML场景,分层数据结构和多个一对多关系都存储在单个XML文件中。结构化文本文件
XML输入示例:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Invoice>
<SellerDetails>
<Identifier>1234-1</Identifier>
<SellerAddress>
<SellerStreet>Street1</SellerStreet>
<SellerTown>Town1</SellerTown>
</SellerAddress>
</SellerDetails>
<BuyerDetails>
<BuyerIdentifier>1234-2</BuyerIdentifier>
<BuyerAddress>
<BuyerStreet>Street2</BuyerStreet>
<BuyerTown>Town2</BuyerTown>
</BuyerAddress>
</BuyerDetails>
<BuyerNumber>001234</BuyerNumber>
<InvoiceDetails>
<InvoiceNumber>0001</InvoiceNumber>
</InvoiceDetails>
<InvoiceRow>
<ArticleName>Article1</ArticleName>
<RowText>Product Text1</RowText>
<RowText>Product Text2</RowText>
<RowAmount AmountCurrencyIdentifier="EUR">10.00</RowAmount>
</InvoiceRow>
<InvoiceRow>
<ArticleName>Article2</ArticleName>
<RowText>Product Text11</RowText>
<RowText>Product Text22</RowText>
<RowAmount AmountCurrencyIdentifier="EUR">20.00</RowAmount>
</InvoiceRow>
<InvoiceRow>
<ArticleName>Article3</ArticleName>
<RowText>Product Text111</RowText>
<RowText>Product Text222</RowText>
<RowAmount AmountCurrencyIdentifier="EUR">30.00</RowAmount>
</InvoiceRow>
<EpiDetails>
<EpiPartyDetails>
<EpiBfiPartyDetails>
<EpiBfiIdentifier IdentificationSchemeName="BIC">XXXXX</EpiBfiIdentifier>
</EpiBfiPartyDetails>
</EpiPartyDetails>
</EpiDetails>
<InvoiceUrlText>Some text</InvoiceUrlText>
</Invoice>
CSV输出示例:
Identifier,SellerStreet,SellerTown,BuyerIdentifier,BuyerStreet,BuyerTown,BuyerNumber,InvoiceNumber,ArticleName,RowText,RowText,RowAmount,EpiBfiIdentifier,InvoiceUrlText
1234-1,Street1,Town1,1234-2,Street2,Town2,1234,1,Article1,Product Text1,Product Text2,10,XXXXX,Some text
1234-1,Street1,Town1,1234-2,Street2,Town2,1234,1,Article2,Product Text11,Product Text22,20,XXXXX,Some text
1234-1,Street1,Town1,1234-2,Street2,Town2,1234,1,Article3,Product Text111,Product Text222,30,XXXXX,Some text
我已经完成了类似于您的需求的案例,我已经基于
untangle
创建了一个包,这个包可以将XML解析为纯python对象,如:到
然后,您可以轻松地编写一些代码来遍历对象以获得所需的内容。 例如,您可以执行类似
get_items_by_tag(InvoiceRow)
的操作。 希望有帮助考虑下面的例子:
XML
XSLT1.0
结果
针对以下内容添加:
如果您愿意,您可以尝试以下方式:
XSLT1.0
结果是:
即在行字段之前列出所有发票字段
相关问题 更多 >
编程相关推荐