This example project shows how you can define mappings between a source and a target XML document and generate the corresponding XSLT. A number of examples are included to illustrate different aspects of mapping. The XML to XML mapping editor can take a variety of input formats (DTD, XSD, or XML). The following table summarizes what each example takes as input and output.
| Example Description | DTD as input/output | XSD as input/output | HTML template (ZML + DTD) |
|---|---|---|---|
| CD | yes | yes | no |
| Addresss | yes | yes | no |
| PO | yes | yes | no |
| Books | no | no | yes |
| OrgChart | no | no | yes |
| Othello | no | no | yes |
| City | yes | yes | no |
| School | yes | yes | no |
| Employee | yes | yes | no |
| Travel | yes | yes | no |
| Directions | yes | yes | no |
A simple example. In this example, information about CDs in cdtitle.xml is converted to another XML format described by the newcdtitle.dtd file.
This example shows how to convert an address description from one format to another. It provides an example of tag transformation and how to define an XPath function.
In this example, a purchase order received from a Web client is transformed into another format that can be processed by an organization's back-end system. It shows how you can call a Javaâ„¢ bean or an external JavaScriptâ„¢ to compute values such as generating an internal unique ID for the request.
This example shows how you can take a list of books that are described in Books.xml, and generates a stylesheet for converting it to HTML for rendering in a Web browser. A bookhtml.xml describes the target format which is an HTML table. This example also introduces the concept of sorting. The unordered list of books described in XML are displayed according to category and price in HTML.
This example is adapted from the XSLT Programmer's Reference book and uses one of the common stylexml.transformet patterns known as the "Fill in the blanks" stylesheet pattern. The orgchart.xml file represents an organizational chart of a company. It is organized as a recursive structure that reflects the management hierarchy. The generated stylesheet displays this data in an HTML table showing the person's name, title, and the name of his or her boss.
This example is also adapted from the XSLT Programmer's Reference book and uses the common stylesheet patten known as the "Rule-Based" stylesheet pattern. The othello.xml file contains a scene from Shakespeare's play Othello. The generated stylesheet renders the play as an HTML table with two columns with the name of the speaker in the first column, and the lines of speech in the second.
This is the first of three grouping examples and introduces the concept of grouping. This example is adapted from the XSLT Programmer's Reference. The cities.xml file lists all the cities and their respective countries. You can change the output format to list all the cities found in a particular country by defining a group and a key.
This is the second grouping example. The school.xml file shows the list of students in a school and the course that each student takes. You can convert this into a different format to list the courses that are offered by the school and the students that are registered for each course.
This is the third grouping example. The employee.xml file contains a list of employees that is created from a typical SQL query. This employee.xml file is generated by the SQL query builder. The result of this query is a flat structure showing the list of employees. You can convert this into a hierarchical structure that groups the list of employees by department.
This example shows you can aggregate data from multiple sources to produce a result document. The hotel.xml contains information about a hotel booking, and the airline.xml contains information about an airline reservation. The data from these two files are combined together to produce an itinerary. Its format is described by the itinerary.dtd file.
In this example, driving directions which handle both Canadian and American addresses, is converted from one format to another. The example demonstrates how to dynamically select or map different members of a choice during a mapping session. It also demonstrates how to add an <xsl:choose> instruction to any target element or attribute in your XMX to XML mapping.
Specifying DTD or XSD as input
Question: Why do I need a DTD or XSD file if I want to define mappings between two instance documents? I don't have a DTD or XSD file.
Response: A DTD or XSD contains the type information needed for generating the correct XSLT. An instance document alone is insufficient for determining if an element is repeatable or non-repeatable. With a DTD or XSD, this level of information that is needed for the transform becomes clear.
If you don't have a DTD, you can use the DTD file editor to generate a DTD from an XML document. With this generated DTD file, you can invoke the XML to XML mapping editor. You can also generate an XSD from the DTD, if desired.
Question: Can I specify an XML document?
Response: You can specify an XML document as input if the XML document includes either a DOCTYPE or an xsi:schemaLocation tag to indicate its corresponding DTD or XSD. If an instance document validates successfully with the XML editor, you can use it as input to the XML to XML mapping editor.
Question: What is the difference between using a DTD or an XSD as input?
Response: If you want to use namespace in your XSLT and your output document, then you should use XSD as input. Refer to the documentation in the XML Schema and XML Editor for a detail description on how namespace works. The XSD examples also provide different namespace usage patterns.
HTML templates
XSLT is commonly used to transform an XML document into an HTML document.
Question: XHTML and HTML cannot be completely described by a DTD because they have generic tags. Can I still use the XML to XML mapping editor to generate XSLT?
Response: Although XHTML and HTML cannot be completely described by a DTD, the DTD is used only for providing metadata and, therefore, does not have to be complete. As long as the DTD defines the portion of the document that you are interested in mapping, it is sufficient for the XML to XML mapping editor.
The Books, OrgChart, and Othello examples above show how you can define a DTD for an HTML fragment and also supplement the output HTML with template data in the target HTML document.