transforming xml part ii xml document transformation with xslt john arnett, msc standards modeller...
TRANSCRIPT
![Page 1: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/1.jpg)
Transforming XML Part II
XML Document Transformation with XSLT
John Arnett, MScStandards ModellerInformation and Statistics DivisionNHSScotlandTel: 0131 551 8073 (x2073)mailto:[email protected]://isdscotland.org/xml
![Page 2: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/2.jpg)
Contents
• XML Document Processing• XSL Stylesheets• XSLT
– Template Rules– Processing Instructions
• Summary• Find Out More
![Page 3: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/3.jpg)
XML 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
![Page 4: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/4.jpg)
XSL Stylesheets
• The Stylesheet element
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"><xsl:apply-templates/>
</xsl:template><xsl:template match="pattern">
<!-- XSLT processing instructions --></xsl:template><!-- More template rules... -->
</xsl:stylesheet>
![Page 5: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/5.jpg)
XSL Stylesheets
• Template Rules
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"><xsl:apply-templates/>
</xsl:template><xsl:template match="pattern">
<!-- XSLT processing instructions --></xsl:template><!-- More template declarations... -->
</xsl:stylesheet>
![Page 6: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/6.jpg)
XSL Stylesheets
• Processing Instructions
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"><xsl:apply-templates/>
</xsl:template><xsl:template match="pattern">
<!-- XSLT processing instructions --></xsl:template><!-- More template declarations... -->
</xsl:stylesheet>
![Page 7: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/7.jpg)
XSL Stylesheets
read template
find source nodes
evaluate processing instructions
more templates?
yes
no
Adapted from XSLT Basics slide presentation by Paul Spencer, alphaXML
Source Tree
Source Tree
Style sheet
Style sheet
Result Tree
Result Tree
![Page 8: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/8.jpg)
XSLT: Template Rules
• Template Rule – XPath pattern specifies nodes
to match <xsl:template match=“Appointment"></xsl:template>
– Output template contains XSLT processing instructions•ie. for processing input and creating new element and attribute nodes
![Page 9: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/9.jpg)
XSLT: Template Rules
• Template rule for creating PatientId element from Appointment<xsl:template match="Appointment">
<PatientId><Id>
<xsl:value-of select="Patient/@upi"/></Id><Name>
<xsl:value-of select="Patient/text()"/></Name>
</PatientId></xsl:template>
![Page 10: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/10.jpg)
XSLT: Template Rules
• PatientId output from template rule
<PatientId><Id>ABC-123-456</Id><Name>John Smith</Name>
</PatientId>
<PatientId><Id>ABC-123-456</Id><Name>John Smith</Name>
</PatientId>
<PatientId><Id>ABC-123-456</Id><Name>John Smith</Name>
</PatientId>
![Page 11: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/11.jpg)
XSLT: Template Rules
• Appointment List
<AppointmentList>+ <Appointment deptCode=”RADIO”> + <Appointment deptCode=”PHYSIO”>+ <Appointment deptCode=”GMED”> + <Appointment deptCode=”GMED”> + <Appointment deptCode=”CMED”> + <Appointment deptCode=”PHYSIO”>+ <Appointment deptCode=”CMED”>
</AppointmentList>
<AppointmentList>+ <Appointment deptCode=”RADIO”> + <Appointment deptCode=”PHYSIO”>+ <Appointment deptCode=”GMED”> + <Appointment deptCode=”GMED”> + <Appointment deptCode=”CMED”> + <Appointment deptCode=”PHYSIO”>+ <Appointment deptCode=”CMED”>
</AppointmentList>
![Page 12: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/12.jpg)
XSLT: Template Rules
• Applying Template Rules– Creating a list of PatientId’s<xsl:template match="AppointmentList">
<PatientList><xsl:apply-templates/>
</PatientList></xsl:template><xsl:template match="Appointment">
<PatientId><Id><xsl:value-of select="Patient/@upi"/></Id><Name><xsl:value-of
select="Patient/text()"/></Name></PatientId>
</xsl:template>
![Page 13: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/13.jpg)
XSLT: Template Rules
• PatientId’s list output
<PatientList>- <PatientId>
<Id>ABC-123-456</Id> <Name>John Smith</Name>
</PatientId>+ <PatientId>+ <PatientId> … + <PatientId>
</PatientList>
<PatientList>- <PatientId>
<Id>ABC-123-456</Id> <Name>John Smith</Name>
</PatientId>+ <PatientId>+ <PatientId> … + <PatientId>
</PatientList>
![Page 14: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/14.jpg)
XSLT: Processing Instructions
• XSLT - rich syntax that enables– Copying– Conditional processing
<xsl:if test=“@deptCode = ‘PHYSIO’“>...
– Iteration and sorting<xsl:for-each select=“Appointment”>...
– Application of XPath functions<xsl:value-of select="concat(@deptCode,
generate-id(.))">...
– Creation of new nodes
![Page 15: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/15.jpg)
XSLT: Processing Instructions
• XSLT Elements– xsl:styleshe
et– xsl:output– xsl:templat
e– xsl:apply-
templates– xsl:value-of
– xsl:text– xsl:if– xsl:choose– xsl:when– xsl:otherwis
e– xsl:foreach
![Page 16: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/16.jpg)
XSLT: Processing Instructions
• Copying and conditional processing – Creating a list of “PHYSIO”
appointments <xsl:template match="AppointmentList">
<PhysioAppointments> <xsl:apply-templates/>
</PhysioAppointments> </xsl:template> <xsl:template match="Appointment">
<xsl:if test="@deptCode = 'PHYSIO'"> <xsl:copy-of select="."/>
</xsl:if> </xsl:template>
![Page 17: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/17.jpg)
XSLT: Processing Instructions
• “PHYSIO” appointments list output
<PhysioAppointments> - <Appointment deptCode="PHYSIO">
+ <Patient upi="EFG-567-012"> <Clinician>Mark Boydd</Clinician> + <Slot attendDate="05-08-2002">
</Appointment>+ <AppointmentdeptCode="PHYSIO">
</PhysioAppointments>
<PhysioAppointments> - <Appointment deptCode="PHYSIO">
+ <Patient upi="EFG-567-012"> <Clinician>Mark Boydd</Clinician> + <Slot attendDate="05-08-2002">
</Appointment>+ <AppointmentdeptCode="PHYSIO">
</PhysioAppointments>
![Page 18: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/18.jpg)
XSLT: Processing Instructions
• Iteration and sorting– Creating an appointments
summary table<table border="1"> ...
<xsl:for-each select="Appointment"><xsl:sort select="@deptCode"
order="ascending"/><tr>
<td><xsl:value-of select="Patient/text()"/></td> ...
</tr></xsl:for-each>
</table>
![Page 19: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/19.jpg)
XSLT: Processing Instructions
• Appointments summary table output
![Page 20: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/20.jpg)
XSLT: Processing Instructions
• Creating new nodes– XSLT instructions
<xsl:element name="MyNewElement"><xsl:attribute name="myNewAttribute">
value </xsl:attribute>
</xsl:element>
<MyNewElement myNewAttribute="value"/>
– XSLT output
![Page 21: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/21.jpg)
In 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
![Page 22: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/22.jpg)
Find 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/h
olman/s2_1.html• ZVON.org - XSLT Tutorial
– www.zvon.org/xxl/XSLTutorial/
![Page 23: Transforming XML Part II XML Document Transformation with XSLT John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel:](https://reader033.vdocuments.mx/reader033/viewer/2022051416/56649e865503460f94b88c61/html5/thumbnails/23.jpg)
Find Out More
• Robert Ducharme’s Transforming XML column– www.xml.com/pub/q/transformin
gxml• TopXML XSLT Stylesheets
– www.topxml.com/xsltstylesheets/default.asp