[lxml-dev] Fwd: How to get a header from xml file
Sorry, I should have replied to the list as well for future readers. ---------- Forwarded message ---------- From: Chuck Bearden <cfbearden@gmail.com> Date: Wed, May 19, 2010 at 10:50 AM Subject: Re: [lxml-dev] How to get a header from xml file To: Sosnovskiy Alexander <alecs.box@gmail.com> On Wed, May 19, 2010 at 6:45 AM, Sosnovskiy Alexander <alecs.box@gmail.com> wrote:
Library is perfect , but I have a small problem. Can someone give me an advice of how to get href = ...... - path to xsl file from the following xml-file :
<?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet href="../xsl/index.xsl" type="text/xsl" ?> <root> </root>
Thanks in advance :)
Here are two approaches:
from StringIO import StringIO from lxml import etree parser = etree.XMLParser(remove_pis=False) xString = """<?xml version="1.0" encoding="utf-8" ?> ... <?xml-stylesheet href="../xsl/index.xsl" type="text/xsl" ?> ... <root> ... </root> ... """ xTree = etree.parse(StringIO(xString), parser) # The ElementTree way xTree.getroot().getprevious() <?xml-stylesheet href="../xsl/index.xsl" type="text/xsl" ?>
# The XPath way
xTree.xpath('/processing-instruction()') [<?xml-stylesheet href="../xsl/index.xsl" type="text/xsl" ?>] procInstruction = xTree.xpath('/processing-instruction()')[0] procInstruction.text 'href="../xsl/index.xsl" type="text/xsl" '
XML and related technologies don't define an API for access to the innards of a PI, so you'll have to use text processing tools to parse the pseudo attribute/value pairs. Best wishes, Chuck
participants (1)
-
Chuck Bearden