Using XML and XSL


During the past couple of years , XML has truly emerged as a core standard for a large number of applications, including a platform-independent way of representing data with associated metadata, separating data from its actual visualization and presentation, standardizing system and business connectivity using Web services, and so on. Consider a scenario where order information is coming from a back-end ERP (Enterprise Resource Planning) system, such as SAP, Oracle, or PeopleSoft. This information comes as XML and you would like to present it in a Web application for an Order Entry/Customer Self-Service application. You have two choices: Either programmatically parse the XML using its DOM (Document Object Model), or utilize an XML technology called XSLT (XSL Transformations) to do the same. In a previous section of this chapter, you took a brief look at the ASP.NET Web control <asp:Xml> , which provides the necessary capability to do the actual transformation. Following is an ASP.NET page (Order.aspx) that uses the ASP.NET Web Server XML control (see Figure 8.5).

 
 <html> <body>   <form runat="server">    <asp:Xml id="orderXml"      DocumentSource="OrderInfo.aspx"      TransformSource="Order.xsl"      runat="server"/>    </asp:Xml>   </form> </body> 
Figure 8.5. Using the ASP.NET XML Web server control.

Again, notice the simplicity achieved with the inclusion of the XML control. The actual data still comes from the back-end Web service or a relational database using the OrderInfo.aspx page, and the style is picked up from the Order.xsl located on the Web server. Listing 8.4 shows what the actual returned XML looks like.

Listing 8.4 Order Information Represented in XML
 <?xml version="1.0"?> <Order>       <Header>              <from>ABC Company</from>              <date>08/01/2004</date>        </Header>        <Lines>              <Line>                    <no>1</no>                    <item>ABC Camera</item>                    <qty>2</qty>                    <price>0</price>              </Line>              <Line>                     <no>2</no>                     <item>XYZ Camera Roll</item>                     <qty>20</qty>                     <price></price>              </Line>        </Lines> </Order> 

Listing 8.5 shows the associated style sheet.

Listing 8.5 Stylesheet for Converting Order XML Data into HTML
 <xsl:stylesheet version="1.0"    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:template match="Order">       <xsl:apply-templates select="Order" />    </xsl:template>    <xsl:template match="Order">        <b>Customer:</b> <xsl:value-of select="Header/from"/><br/>        <b>Order Date:</b> <xsl:value-of select="Header/date"/><br/>        <b>Line Items</b><br/>        <xsl:apply-templates select="Lines"/>    </xsl:template>    <xsl:template match="Lines">      <table border="1">       <tr>          <td>Line No</td>          <td>Item</td>          <td>Quantity</td>          <td>Price</td>       </tr>       <xsl:for-each select="Line">        <tr>          <td><xsl:value-of select="no"/></td>          <td><xsl:value-of select="item"/></td>          <td><xsl:value-of select="qty"/></td>          <td><xsl:value-of select="price"/></td>        </tr>       </xsl:for-each>      </table>    </xsl:template> </xsl:stylesheet> 

SHOP TALK : USING XML AND XSLT FOR PRESENTATION

XML and XSLT provide an excellent way of separating the data to be presented and how it needs to be presented based on the appropriate medium. For instance, a practical use of XML/XSLT together is to generate an invoice in HTML for Web-based delivery and to PDF (using a standard called XSL-FO ”XSL Formatting Objects) for print media delivery. Whenever you have a scenario where multiple output formats are required, think about the XML/XSL combination. A key thing to remember is that XML is not just for file-based data; XML can be dynamically generated from practically any data source ”a relational database, a Web service, and so on.




Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net