How to Write a Custom Report Using XML Language

How to Write a Custom Report Using XML Language thumbnail
Use XSL to transform XML into custom reports.

XML, or eXtensible Markup Language, describes data in a standardized way. XML does not specify how data should be displayed; instead, XML uses user-defined tags that define the data's meaning. XHTML, the standard version of Hypertext Markup Language, defines how data should be displayed. Using eXtensible Stylesheet Language and XSL Transformations, Web developers can transform XML into XHTML, building user-friendly, custom reports that take advantage of standardized data without sacrificing appearance.

Instructions

    • 1

      Open a text editor and create a file containing the report's data described with XML. The data describes a car with a specific make and model. The first line in the file is the XML declaration. Save the file with the name "cars.xml".

      <?xml version="1.0" encoding="UTF-8" ?>

      <car>

      <make>Honda</make>

      <model>Civic</model>

      </car>

    • 2

      Create a new file that contains the XSL style sheet. This style sheet describes how to transform the report data when the cars.xml file loads. The first line in the file is the XML declaration. Declare that the file is an XSL style sheet and save the file with the name "cars.xsl".

      <?xml version="1.0" encoding="ISO-8859-1"?>

      <xsl:stylesheet version="1.0"

      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      </xsl:stylesheet>

    • 3

      Add the XSL template element to the file. Since this is a simple XML file with only a few data elements, use the match attribute to apply it to the entire document (="/"). Enter XHTML tags to format the final report.

      <?xml version="1.0" encoding="ISO-8859-1"?>

      <xsl:stylesheet version="1.0"

      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:template match="/">

      <html>

      <body>

      <h1>Cars Report</h1>

      </body>

      </html>

      </xsl:template>

      </xsl:stylesheet>

    • 4

      Use XSLT value-of elements to identify the data in the XML file. Place the elements inside the final report's XHTML tags.

      <?xml version="1.0" encoding="ISO-8859-1"?>

      <xsl:stylesheet version="1.0"

      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:template match="/">

      <html>

      <body>

      <h1>Cars Report</h1>

      <ul>

      <li><xsl:value-of select="car/make"/></li>

      <li><xsl:value-of select="car/model"/></li>

      </ul>

      </body>

      </html>

      </xsl:template>

      </xsl:stylesheet>

    • 5

      Edit cars.xml. Assign the cars.xsl style sheet after the XML declaration and save the file.

      <?xml version="1.0" encoding="UTF-8" ?>

      <?xml-stylesheet type="text/xsl" href="cars.xsl"?>

      <car>

      <make>Honda</make>

      <model>Civic</model>

      </car>

    • 6

      Open cars.xml in a Web browser and view the formatted report.

Tips & Warnings

  • Use XHTML table tags to give the report its final format.

  • This example uses only one item in the root node. For more complicated XML transformations, use more advanced XPath expressions.

  • Be sure to include the root element in the XML file.

Related Searches:

References

Resources

  • Photo Credit illustration with personal computer and diagram image by Alexander Potapov from Fotolia.com

Comments

You May Also Like

Related Ads

Featured