2

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?

Rella
  • 65,003
  • 109
  • 363
  • 636
  • if I'm not mistaken, you've been inspired by my answer to your previous question: http://stackoverflow.com/questions/2162386/is-there-any-html-to-wikitext-translator/2162443#2162443 . It would have been nice to vote for it. – Pierre Jan 29 '10 at 16:09

7 Answers7

4

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
AJ.
  • 27,586
  • 18
  • 84
  • 94
2

Pseudocode:

Load SOURCE file as XML
Load STYLESHEET file as XML
Apply STYLESHEET to SOURCE, generating RESULT
Write RESULT out to file as XML
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Yeah, I mean each language provides different methods - but the above pseudocode is correct. – mr-sk Jan 29 '10 at 15:47
2

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

Pierre
  • 34,472
  • 31
  • 113
  • 192
1

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.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
1

in PHP take a look at this

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

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
1

W3Schools has my favorite XSLT tutorial ..

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

Good luck!

Wes Floyd
  • 346
  • 3
  • 6
0

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.

Community
  • 1
  • 1
Ash
  • 1,614
  • 1
  • 10
  • 6