开发者

apply an xslt function to some nodes from libxml2 in Python

开发者 https://www.devze.com 2023-03-15 17:17 出处:网络
In a python 开发者_JS百科script, I want to aplly the following XSLT functions : normalize-space and translate to some nodes from an libxml2 tree

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

精彩评论

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