!

Dette materialet blir ikke lenger vedlikeholdt. Du vil finne oppdatert materiale på siden: http://borres.hiof.no/wep/

lxml
XSLT
Børre Stenseth
Python > lxml >XSLT

XSLT

Hva
Transformasjoner med XSLT

lxml implementerr XSLT.

De aktuelle dataene er ordnet i en XML-fil: all_results.xml

Kopier datafila, transformasjoner og pythonkoden nedenfor og eksperimenter.

Uten parametere

XSLT-transformasjon uten parametere. Transformasjonen er slik:

_olymp.xsl

Merk grepet som må tas for å få ut html5 doctype: <!DOCTYPE html>

Pythonscriptet:

"""
working with XSLT-transformations
"""

"""
 Performing a transformation
 without parameters
"""
from lxml import etree
def testing():
    xmlfile='all_results.xml'
    xsltfile='olymp.xsl'
    tree=etree.parse(xmlfile)
    xsltree=etree.parse(xsltfile)
    transform=etree.XSLT(xsltree)
    resulttree=transform(tree)
    print str(resulttree)
testing()

Produserer

<!DOCTYPE html>
<html>
  <head>
    <title>Olympics</title>
    <meta charset="UTF-8"/>
    <link rel="STYLESHEET" href="olymp.css"/>
  </head>
  <body>
    <h1>Resultater sprint</h1>
    <h2>Barcelona - 1992</h2>
    <h3>100m</h3>
    <p>09.96 : Linford Christie</p>
    <p>10.02 : Frank Fredericks</p>
    <p>10.04 : Dennis Mitchell</p>
    <p>10.09 : Bruny Surin</p>
    <p>10.10 : Leroy Burrell</p>
    <p>10.12 : Olapade Adeniken</p>
    <p>10.22 : Raymond Stewart</p>
    <p>10.26 : Davidson Ezima</p>
    <h3>200m</h3>
    <p>20.01 : Mike Marsh</p>
    <p>20.13 : Frank Fredericks</p>
    <p>20.38 : Michael Bates</p>
    <p>20.45 : Robson Caetano da Silva</p>
    <p>20.50 : Olapade Adeniken</p>
    <p>20.55 : John Regis</p>
    <p>20.67 : Oluyemi Kayode</p>
    <p>20.80 : Marcus Adam</p>
    <h3>400m</h3>
    <p>43.50 : Quincy Watts</p>
    <p>44.21 : Steve Lewis</p>
    <p>44.24 : Samson Kitur</p>
    <p>44.25 : Ian Morris</p>
    <p>44.52 : Roberto Hernandez Prendes</p>
    <p>44.75 : David Grindley</p>
    <p>45.10 : Ibrahim Ismail</p>
    <p>45.18 : Susumu Takano</p>
  </body>
</html>

Med parametere

XSLT-transformasjon med parametere. Transformasjonen er slik:

_olymp_x.xsl

Pythonscriptet:

"""
working with XSLT-transformations
"""

"""
 Performing a transformation
 with parameters
"""
from lxml import etree
def testing(where,dist):
    xmlfile='all_results.xml'
    xsltfile='olymp_x.xsl'
    tree=etree.parse(xmlfile)
    xsltree=etree.parse(xsltfile)
    transform=etree.XSLT(xsltree)
    #control parameter format
    d=etree.XSLT.strparam(dist)
    p=etree.XSLT.strparam(where)   
    resulttree=transform(tree,sted=p,distanse=d)
    print str(resulttree)
testing("Sidney","400m")

Produserer

<!DOCTYPE html>
<html>
  <head>
    <title>Olympics</title>
    <meta charset="UTF-8"/>
    <link rel="STYLESHEET" href="olymp.css"/>
  </head>
  <body>
    <h1>Resultater sprint</h1>
    <h2>Sidney - 2000</h2>
    <h3>400m</h3>
    <p>43.84 : Michael Johnson</p>
    <p>44.40 : Alvin Harrison</p>
    <p>44.70 : Gregory Haughton</p>
    <p>45.01 : Sanderlei Parrela</p>
    <p>45.14 : Robert Mackowiak</p>
    <p>45.26 : Hendrick Mokganyetsi</p>
    <p>45.42 : Antonio Pettigrew</p>
    <p>45.55 : Danny Mcfarlane</p>
  </body>
</html>

Referanser
  1. lxml - XML and HTML with Python lxml.de/ 03-08-2011
Et eksempel på bruk av Python / lxml finner du i modulen XML2PDF
Vedlikehold
Børre Stenseth, september 2012
( Velkommen ) Python > lxml >XSLT ( Objekter )