gpt4 book ai didi

python - 如何在 Python 中使用 XSLT 转换 XML 文件?

转载 作者:IT老高 更新时间:2023-10-28 20:24:01 26 4
gpt4 key购买 nike

美好的一天!需要在 Python 中使用 xslt 转换 xml。我在 php 中有一个示例代码。

如何在 Python 中实现这一点,或者在哪里可以找到类似的东西?谢谢!

$xmlFileName = dirname(__FILE__)."example.fb2";
$xml = new DOMDocument();
$xml->load($xmlFileName);

$xslFileName = dirname(__FILE__)."example.xsl";
$xsl = new DOMDocument;
$xsl->load($xslFileName);

// Configure the transformer
$proc = new XSLTProcessor();
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml);

最佳答案

使用 lxml ,

import lxml.etree as ET

dom = ET.parse(xml_filename)
xslt = ET.parse(xsl_filename)
transform = ET.XSLT(xslt)
newdom = transform(dom)
print(ET.tostring(newdom, pretty_print=True))

关于python - 如何在 Python 中使用 XSLT 转换 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16698935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com