How to convert XML to something else using xslt stylesheet?

徘徊边缘 提交于 2019-12-01 10:51:34

Pseudocode:

Load SOURCE file as XML
Load STYLESHEET file as XML
Apply STYLESHEET to SOURCE, generating RESULT
Write RESULT out to file as XML

In Python, libxml and libxslt are my personal choices for this kind of functionality.


(Edit) Here is a simple example of performing a transformation using libxml and libxslt:

#!/usr/bin/python

import sys
import libxml2
import libxslt


def getXSLT(xsl_filename):
    # parse the stylesheet xml file into doc object
    styledoc = libxml2.parseFile(xsl_filename)

    # process the doc object as xslt
    style = libxslt.parseStylesheetDoc(styledoc)

    return style


if __name__ == '__main__':
    style = getXSLT("stylesheet.xsl")
    doc = libxml2.parseFile("data.xml")
    result = style.applyStylesheet(doc, None)

    print result

here is an example using C and libxslt: http://xmlsoft.org/XSLT/tutorial/libxslttutorial.html

In .NET, you may want to look at this article. In C++, you can use Xalan-C++. Xalan-C++ even has some handy examples of how to use it.

in PHP take a look at this

DomXsltStylesheet->process
and also read the last note at the bottom which has a working example ...

W3Schools has my favorite XSLT tutorial ..

http://www.w3schools.com/xsl/

Good luck!

Ash

The correct library in Python now is lxml. Please see this answer on StackOverflow. It is similar syntax, and you wont have issues in installing it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!