Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert XML to something else using xslt stylesheet?

How to convert XML to something else using xslt stylesheet?

In C++ C# PHP or ActionScript?

For example I have this html2wiki xslt stylesheet I want to send to my programm my XML (in this case HTML file ) and get back a file (in this case Wiki mark up text )

So How to translate one text file into another text file using XSLT stylesheet in any language?


2 Answers

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
like image 136
AJ. Avatar answered Feb 02 '26 19:02

AJ.


Pseudocode:

Load SOURCE file as XML
Load STYLESHEET file as XML
Apply STYLESHEET to SOURCE, generating RESULT
Write RESULT out to file as XML
like image 21
Ignacio Vazquez-Abrams Avatar answered Feb 02 '26 18:02

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!