1 / 23

Transforming XML Part II

Transforming XML Part II. John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel: 0131 551 8073 (x2073) mailto:John.Arnett@isd.csa.scot.nhs.uk http://isdscotland.org/xml. XML Document Transformation with XSLT. Contents. XML Document Processing

igor-hester
Download Presentation

Transforming XML Part II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Transforming XML Part II John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel: 0131 551 8073 (x2073) mailto:John.Arnett@isd.csa.scot.nhs.uk http://isdscotland.org/xml XML Document Transformation with XSLT

  2. Contents • XML Document Processing • XSL Stylesheets • XSLT • Template Rules • Processing Instructions • Summary • Find Out More

  3. 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

  4. 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>

  5. 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>

  6. 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>

  7. XSL 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

  8. 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

  9. 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>

  10. 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>

  11. 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>

  12. 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>

  13. XSLT: Template Rules • PatientId’s list output <PatientList> -<PatientId> <Id>ABC-123-456</Id> <Name>John Smith</Name> </PatientId> +<PatientId> +<PatientId> … +<PatientId> </PatientList>

  14. 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

  15. XSLT: Processing Instructions • XSLT Elements • xsl:text • xsl:if • xsl:choose • xsl:when • xsl:otherwise • xsl:foreach • xsl:stylesheet • xsl:output • xsl:template • xsl:apply-templates • xsl:value-of

  16. 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>

  17. 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>

  18. 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>

  19. XSLT: Processing Instructions • Appointments summary table output

  20. XSLT: Processing Instructions • Creating new nodes • XSLT instructions <xsl:element name="MyNewElement"> <xsl:attribute name="myNewAttribute"> value </xsl:attribute> </xsl:element> • XSLT output <MyNewElement myNewAttribute="value"/>

  21. 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

  22. 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/holman/s2_1.html • ZVON.org - XSLT Tutorial • www.zvon.org/xxl/XSLTutorial/

  23. Find Out More • Robert Ducharme’s Transforming XML column • www.xml.com/pub/q/transformingxml • TopXML XSLT Stylesheets • www.topxml.com/xsltstylesheets/default.asp

More Related