Rémi Desgrange
Rémi Desgrange

Reputation: 908

lxml missing node when xslt transform

I have an XML and XSLT. Weather opendata from france

XML : https://donneespubliques.meteofrance.fr/donnees_libres/Pdf/BRA/BRA.CHABLAIS.20190514130953.xml XSLT : http://donneespubliques.meteofrance.fr/donnees_libres/Pdf/BRA/bra.xslt

I tested with the website https://xslttest.appspot.com/ which seems to use http://saxon.sourceforge.net/ and it gives me an two html element, link and a div.

With lxml it only gives me the link element :

import requests
import lxml.etree as ET

xml = ET.fromstring(requests.get('https://donneespubliques.meteofrance.fr/donnees_libres/Pdf/BRA/BRA.CHABLAIS.20190514130953.xml').content)
xslt = ET.fromstring(requests.get('http://donneespubliques.meteofrance.fr/donnees_libres/Pdf/BRA/bra.xslt').content)
transform = ET.XSLT(xslt)
generated_html = transform(xml)
print(ET.tostring(generated_html, pretty_print=True))

output:

b'<link rel="stylesheet" type="text/css" href="BRA.css"/>\n'

I have the feeling that maybe XSLT generate two XML elements, with no root and that's maybe why lxml gives me only one link element.

using lxml 4.3.3

Upvotes: 1

Views: 258

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

I get the complete result fragment with lxml in Python if I use str(generated_html) or generated_html.write_output(sys.stdout) (although for the latter to work it seems the encoding declared in xsl:output needs to match the encoding of stdout, on Windows it seems to be UTF-8, not sure about other platforms).

See also https://lxml.de/xpathxslt.html#xslt-result-objects warning that

it is possible to use the .write() method (known from ElementTree objects) to serialise the XSLT result into a file, it is better to use the .write_output() method. The latter knows about the tag and writes the expected data into the output file.

So I think your assumption that the generated fragment with more than one top level elements doesn't work well with the used tostring method is right.

Upvotes: 2

Related Questions