Jak przekształcić plik XML za pomocą XSLT w Pythonie?
Dzień dobry! Trzeba przekonwertować xml za pomocą xslt w Pythonie. Mam przykładowy kod w php.
Jak zaimplementować to w Pythonie lub gdzie znaleźć coś podobnego? Dziękuję!
$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);
50
3 answers
Za pomocą 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))
81
Author: unutbu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 18:23:50
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 18:23:50
LXML jest szeroko używaną biblioteką o wysokiej wydajności do przetwarzania XML w Pythonie opartą na libxml2 i libxslt-zawiera udogodnienia dla XSLT oraz.
5
Author: miku,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 18:22:40
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-22 18:22:40
Najlepszym sposobem jest użycie lxml, ale obsługuje tylko XSLT 1
import os
import lxml.etree as ET
inputpath = "D:\\temp\\"
xsltfile = "D:\\temp\\test.xsl"
outpath = "D:\\output"
for dirpath, dirnames, filenames in os.walk(inputpath):
for filename in filenames:
if filename.endswith(('.xml', '.txt')):
dom = ET.parse(inputpath + filename)
xslt = ET.parse(xsltfile)
transform = ET.XSLT(xslt)
newdom = transform(dom)
infile = unicode((ET.tostring(newdom, pretty_print=True)))
outfile = open(outpath + "\\" + filename, 'a')
outfile.write(infile)
Aby użyć XSLT 2 możesz sprawdzić opcje z Użyj saxon z Pythonem
1
Author: Maliqf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-17 10:27:29
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-17 10:27:29