Browse DevX
Sign up for e-mail newsletters from DevX


Converting Fixed-Width Text Records to XML-3 : Page 3




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Display the Resulting XML
The ASP file ProcessTextFile.asp generates the file XML record set, saves it, then passes the XML file to another transformation (ProcessRecordset.xsl):

<records> <record><id type="xsd:id">31A201</id> <firstname type="xsd:string">Kurt</firstname> <lastname type="xsd:string">Cagle</lastname> <amount type="xsd:currency">3242.27</amount> <type type="xsd:string">Basic</type> </record> <record><id type="xsd:id">31A202</id> <firstname type="xsd:string">Aleria</firstname> <lastname type="xsd:string">Delamare</lastname> <amount type="xsd:currency">6250.54</amount> <type type="xsd:string">Advanced</type> </record> <record><id type="xsd:id">31A203</id> <firstname type="xsd:string">Gina</firstname> <lastname type="xsd:string">Delgadio</lastname> <amount type="xsd:currency">317.12</amount> <type type="xsd:string">Advanced</type> </record> <!-- more records --> </records>

The processTextFile.asp page accepts a showType parameter. --It passes the value to the FixedLengthRoutines.xsl to determine whether to include the XML Schema data type information in the final output. You control the setting by using an optional query string parameter. For example, using the URL processTextFile.asp?showType=yes tells the application to include the type definitions, while setting showType to "no" in the URL removes them.

After you have the information in XML form, you can essentially do anything that you can do with any other XML file. In the sample code, the processTextFile.asp passes the XML to another stylesheet, processRecordset.xsl (see Listing 3), which formats the output in a table and provides a rudimentary way of filtering the output list.

The processRecordset.xsl file displays the records as a table. You can use the $matchTerm parameter as a rudimentary filter on the list. You can also subclass the templates to provide additional functionality, such as currency formatting or color-coding for different types of accounts. To avoid potential namespace conflicts, I defined all these templates as being of "format" mode. To subclass your own templates, you need to use the same mode indicator in your code. You can view the output of the ASP page in Figure 1.

The successful use of XSLT sometimes comes down to knowing when to avoid using XSLT. While it is possible to accomplish the same, transformation within XSLT, (especially using extensions) there is still a basic need for DOM to handle limitations in the XSLT model. However, regardless of the processing mechanism, by converting fixed records into even simple XML you can bootstrap your development dramatically and gain the robust processing capabilities that XSLT brings.

Kurt Cagle is the author or co-author of twelve books and several dozen articles on web technologies, XML and web services. He is the president of Cagle Communications (Olympia, WA), which specializes in the production of training materials for XML and Web Services education. He can be reached at kurt@kurtcagle.net.
Thanks for your registration, follow us on our social networks to keep up-to-date