Title: Transforming XML Part II
1Transforming XML Part II
- XML Document Transformation with XSLT
John Arnett, MSc Standards Modeller Information
and Statistics Division NHSScotland Tel 0131 551
8073 (x2073) mailtoJohn.Arnett_at_isd.csa.scot.nhs.u
k http//isdscotland.org/xml
2Contents
- XML Document Processing
- XSL Stylesheets
- XSLT
- Template Rules
- Processing Instructions
- Summary
- Find Out More
3XML Document Processing
- Transformation and formatting
- XPath used to locate nodes for input
- XSLT used to transform input and generate result
tree - XSL-FO used to format output document
4XSL Stylesheets
ltxslstylesheet version"1.0" xmlnsxsl"http//ww
w.w3.org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt ltxslapply-templates/gt lt/xsltemplat
egt ltxsltemplate match"pattern"gt lt!-- XSLT
processing instructions --gt lt/xsltemplategt lt!--
More template rules... --gt lt/xslstylesheetgt
5XSL Stylesheets
ltxslstylesheet version"1.0" xmlnsxsl"http//ww
w.w3.org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt ltxslapply-templates/gt lt/xsltemplat
egt ltxsltemplate match"pattern"gt lt!-- XSLT
processing instructions --gt lt/xsltemplategt lt!--
More template declarations... --gt lt/xslstyleshee
tgt
6XSL Stylesheets
ltxslstylesheet version"1.0" xmlnsxsl"http//ww
w.w3.org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt ltxslapply-templates/gt lt/xsltemplat
egt ltxsltemplate match"pattern"gt lt!-- XSLT
processing instructions --gt lt/xsltemplategt lt!--
More template declarations... --gt lt/xslstyleshee
tgt
7XSL Stylesheets
Source Tree
Style sheet
read template
yes
find source nodes
more templates?
no
Result Tree
evaluate processing instructions
Adapted from XSLT Basics slide presentation by
Paul Spencer, alphaXML
8XSLT Template Rules
- Template Rule
- XPath pattern specifies nodes to match
- ltxsltemplate matchAppointment"gt
- lt/xsltemplategt
- Output template contains XSLT processing
instructions - ie. for processing input and creating new element
and attribute nodes
9XSLT Template Rules
- Template rule for creating PatientId element
from Appointment
ltxsltemplate match"Appointment"gt ltPatientIdgt
ltIdgt ltxslvalue-of select"Patient/_at_upi"/gt lt/
Idgt ltNamegt ltxslvalue-of select"Patient/text
()"/gt lt/Namegt lt/PatientIdgt lt/xsltemplategt
10XSLT Template Rules
- PatientId output from template rule
ltPatientIdgt ltIdgtABC-123-456lt/Idgt ltNamegtJohn
Smithlt/Namegt lt/PatientIdgt
ltPatientIdgt ltIdgtABC-123-456lt/Idgt ltNamegtJohn
Smithlt/Namegt lt/PatientIdgt
11XSLT Template Rules
ltAppointmentListgt ltAppointment
deptCodeRADIOgt ltAppointment
deptCodePHYSIOgt ltAppointment
deptCodeGMEDgt ltAppointment
deptCodeGMEDgt ltAppointment
deptCodeCMEDgt ltAppointment
deptCodePHYSIOgt ltAppointment
deptCodeCMEDgt lt/AppointmentListgt
12XSLT Template Rules
- Applying Template Rules
- Creating a list of PatientIds
ltxsltemplate match"AppointmentList"gt ltPatientLi
stgt ltxslapply-templates/gt lt/PatientListgt lt/xsl
templategt ltxsltemplate match"Appointment"gt ltPa
tientIdgt ltIdgtltxslvalue-of select"Patient/_at_upi"
/gtlt/Idgt ltNamegtltxslvalue-of select"Patient/text
()"/gtlt/Namegt lt/PatientIdgt lt/xsltemplategt
13XSLT Template Rules
ltPatientListgt - ltPatientIdgt ltIdgtABC-123-456lt/
Idgt ltNamegtJohn Smithlt/Namegt
lt/PatientIdgt ltPatientIdgt ltPatientIdgt
ltPatientIdgt lt/PatientListgt
14XSLT Processing Instructions
- XSLT - rich syntax that enables
- Copying
- Conditional processing
- ltxslif test_at_deptCode PHYSIOgt...
- Iteration and sorting
- ltxslfor-each selectAppointmentgt...
- Application of XPath functions
- ltxslvalue-of select"concat(_at_deptCode,
generate-id(.))"gt... - Creation of new nodes
15XSLT Processing Instructions
- xsltext
- xslif
- xslchoose
- xslwhen
- xslotherwise
- xslforeach
- xslstylesheet
- xsloutput
- xsltemplate
- xslapply-templates
- xslvalue-of
16XSLT Processing Instructions
- Copying and conditional processing
- Creating a list of PHYSIO appointments
ltxsltemplate match"AppointmentList"gt ltPhysioApp
ointmentsgt ltxslapply-templates/gt
lt/PhysioAppointmentsgt lt/xsltemplategt
ltxsltemplate match"Appointment"gt ltxslif
test"_at_deptCode 'PHYSIO'"gt ltxslcopy-of
select"."/gt lt/xslifgt lt/xsltemplategt
17XSLT Processing Instructions
- PHYSIO appointments list output
ltPhysioAppointmentsgt - ltAppointment
deptCode"PHYSIO"gt ltPatient
upi"EFG-567-012"gt ltCliniciangtMark
Boyddlt/Cliniciangt ltSlot attendDate"05-08-200
2"gt lt/Appointmentgt ltAppointmentdeptCode"
PHYSIO"gt lt/PhysioAppointmentsgt
18XSLT Processing Instructions
- Iteration and sorting
- Creating an appointments summary table
lttable border"1"gt ... ltxslfor-each
select"Appointment"gt ltxslsort
select"_at_deptCode" order"ascending"/gt lttrgt lt
tdgtltxslvalue-of select"Patient/text()"/gtlt/tdgt
... lt/trgt lt/xslfor-eachgt lt/tablegt
19XSLT Processing Instructions
- Appointments summary table output
20XSLT Processing Instructions
- Creating new nodes
- XSLT instructions
ltxslelement name"MyNewElement"gt ltxslattribute
name"myNewAttribute"gt value lt/xslattributegt
lt/xslelementgt
ltMyNewElement myNewAttribute"value"/gt
21In Summary
- XSL stylesheets contain one or more XSLT template
rules - Template rules specify nodes to match and contain
XSLT processing instructions - XSLT has a rich syntax for transforming documents
into XML and non-XML outputs
22Find Out More
- W3C XSL Transformations (XSLT) v1.0
Specification - www.w3.org/TR/xslt
- Getting started with XSLT and XPath by Ken Holman
(Parts 1, 2 3) - www.xml.com/pub/a/2000/08/holman/s2_1.html
- ZVON.org - XSLT Tutorial
- www.zvon.org/xxl/XSLTutorial/
23Find Out More
- Robert Ducharmes Transforming XML column
- www.xml.com/pub/q/transformingxml
- TopXML XSLT Stylesheets
- www.topxml.com/xsltstylesheets/default.asp