[lxml-dev] How to get a header from xml file
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 :)
Hi, Wednesday 19 May 2010
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>
Try this one: ------------------ tree = etree.XML("""<?xml version="1.0" encoding="utf-8" ?> <?xml-stylesheet href="../xsl/index.xsl" type="text/xsl" ?> <root> </root> """) root = tree.getroottree() stylesheet = root.xpath("/processing-instruction('xml-stylesheet')")[0] print stylesheet.text ------------------ Probably there are other methods. :) Note, a processing instruction just knows a target (here: xml-stylesheet) and its content (everything in between "<?xml-stylesheet" and "?>"). A processing instruction has no concept of attributes. As such, you can get only text which you have to parse by yourself. This can be done by a regular expression. For example: ------------------ import re m=re.search('href\s*="([a-zA-Z0-9./\-\\+%]+)"', stylesheet.text) print m.groups() ('../xsl/index.xsl',) ------------------ Depending on what you expect in the href "pseudo-attribute", you have to improve the pattern in the above regular expression. Hope that helps, Tom
Thomas Schraitle, 19.05.2010 20:26:
Wednesday 19 May 2010
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>
tree = etree.XML(...) root = tree.getroottree()
I'd swap the variable names here.
stylesheet = root.xpath("/processing-instruction('xml-stylesheet')")[0]
getprevious() will do just fine.
Note, a processing instruction just knows a target (here: xml-stylesheet) and its content (everything in between "<?xml-stylesheet" and "?>"). A processing instruction has no concept of attributes.
lxml.etree special cases the xml-stylesheet PI, though, so you can use get() and set() on it, and ask it to parse the stylesheet. http://codespeak.net/lxml/api/lxml.etree._XSLTProcessingInstruction-class.ht... Stefan
participants (3)
-
Sosnovskiy Alexander -
Stefan Behnel -
Thomas Schraitle