Tables
The two earlier examples were both for the conversion of XML into HTML. Now it's time to convert an XML file into RTF. For this example, I thought I'd choose something that is often irritatingly difficult: a table. The XML input document is shown in Listing 19.12 and the DSSSL style sheet is shown in Listing 19.13.
|
Listing 19.12 XML Cookbook File 3 (Tables)
|
1: <?xml version="1.0"> 2: <!DOCTYPE DOC [ 3: <!ELEMENT DOC (TITLE | TABLE | PARA)*> 4: <!ELEMENT TITLE (#PCDATA)> 5: <!ELEMENT TABLE (ROW)* > 6: <!ELEMENT ROW (CELL)*> 7: <!ELEMENT CELL (#PCDATA)> 8: <!ELEMENT PARA (#PCDATA | TITLE)*> 9: <!ELEMENT CAUTION (#PCDATA)> 10: ]> 11: <DOC> 12: <TITLE>Simple XML to RTF Conversion</TITLE> 13: <PARA>This sample document demonstrates how you can 14: create tables.</PARA> 15: 16: <TABLE> 17: <ROW><CELL>Cell 1.1</CELL><CELL>Cell 1.2</CELL> 18: <CELL>Cell 1.3</CELL></ROW> 19: <ROW><CELL>Cell 2.1</CELL><CELL>Cell 2.2</CELL> 20: <CELL>Cell 2.3</CELL></ROW> 21: <ROW><CELL>Cell 3.1</CELL><CELL>Cell 3.2</CELL> 22: <CELL>Cell 3.3</CELL></ROW> 23: <ROW><CELL>Cell 4.1</CELL><CELL>Cell 4.2</CELL> 24: <CELL>Cell 4.3</CELL></ROW> 25: <ROW><CELL>Cell 5.1</CELL><CELL>Cell 5.2</CELL> 26: <CELL>Cell 5.3</CELL></ROW> 27: <ROW><CELL>Cell 6.1</CELL><CELL>Cell 6.2</CELL> 28: <CELL>Cell 6.3</CELL></ROW> 29: <ROW><CELL>Cell 7.1</CELL><CELL>Cell 7.2</CELL> 30: <CELL>Cell 7.3</CELL></ROW> 31: </TABLE> 32: 33: <PARA>Now we're back to normal text again.</PARA> 34: </DOC>
|
Listing 19.13 DSSSL Cookbook File 3 (Tables)
|
1: <!DOCTYPE style-sheet 2: PUBLIC "-//James Clark//DTD DSSSL Style Sheet//EN"> 3: (define debug 4: (external-procedure "UNREGISTERED::James Clark//Procedure::debug")) 5: 6: (root 7: (make simple-page-sequence 8: left-margin: 2cm 9: font-size: 12pt 10: line-spacing: 14pt 11: right-margin: 2cm 12: top-margin: 2cm 13: bottom-margin: 2cm 14: (process-children))) 15: 16: (element (DOC TITLE) 17: (make paragraph 18: quadding: 'center 19: font-size: 24pt 20: line-spacing: 36pt 21: space-after: 12pt 22: font-weight: 'bold 23: keep-with-next?: #t 24: (process-children))) 25: 26: (element PARA 27: (make paragraph 28: font-size: 12pt 29: line-spacing: 16pt 30: (process-children))) 31: 32: (element TABLE ;the processing of the table element 33: (make table ;a data-driven table 34: space-before: .25in 35: space-after: .5in 36: table-border: (make table-border line-thickness: 3pt) 37: (make table-column width: 1.5in) ;all columns before any rows 38: (make table-column width: 1.5in) 39: (make table-column width: 1.5in) 40: (process-children))) ;child elements make row flow objects 41: 42: (element ROW ;a row in the table 43: (make table-row 44: (process-children))) ;child elements make cell flow objects 45: 46: (element CELL ;a column in the row 47: (make table-cell 48: cell-before-row-border: #t 49: cell-before-column-border: #t 50: (make paragraph 51: quadding: 'center 52: (process-children)))) ;content of the table cell
| As you can see from Listing 19.12, I haven't needed to perform any special tricks to format the XML table. There's no point in looking at the RTF code, so Figure 19.9 simply shows the converted XML file loaded into Microsoft Word. |
Figure 19.9 The tables displayed in RTF.