lxml
XSLT
Python
Børre Stenseth
lxml >XSLT

XSLT

Hva

lxml implementerer 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='../lxml/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='../lxml/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>

[1]
Referanser
  1. lxml - XML and HTML with Python lxml.de/ 03-03-2014
lxml >XSLT