开发者

apply an xslt function to some nodes from libxml2 in Python

In a python 开发者_JS百科script, I want to aplly the following XSLT functions : normalize-space and translate to some nodes from an libxml2 tree

Can it be done without writing a stylesheet ?

If a stylesheet is needed, what will it look like ?

how to set the context node to a given node from python ?


Both normalize-space() and translate() are standard XPath functions, so they can be used in any XPath expression -- without XSLT.

Just use the API for your XPath engine.


in fact it's very easy :D

context = xmlDoc.xpathNewContext()
extractedContent = context.xpathEval( normalize-space( translate( /html[1]/body[1]/div[1]/div[2]/div[2]/div[1]/div[2]/div[1]/form[1]/div[2]/h3[1] , ' ' , ' ' ) )
context.xpathFreeContext()

I thought normalize-space and translate could be accessed only from an XSLT engine I was wrong

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜