XslTransform

XslTransform

XSL, or XSLT, as it will be referred to in this section, stands for extensible style sheet language transformation. XSLT is a formatting language. It is based on template rules, which specify how XML documents should be processed. Although conventional programming languages are often sequential, template rules can be based in any order because XSLT is a declarative language. The style sheet declares what output should be produced when a pattern in the XML document is matched.

For example, a style sheet could declare that when the XSL transformation engine finds a NAME element, it should add markup by calling the name template. You can see a simple example of what this name template might look like in the following code:

 <xsl:template match="NAME">     ... </xsl:template> 

I have created an example that can be found on www.UsingASP.NET. It takes a simple XML file and renders it using an XslTransform. You can see the final rendered output in Figure 9.4.

Figure 9.4. A Simple XML File Is Saved and Then Rendered with an XSLTransform.

graphics/09fig04.jpg

The XSLT file for this example is not very complicated. It can be seen in Listing 9.8. You can see in the example that the XML will be rendered in a table. Note the XSLT construct with the for-each syntax. This will cause each set of matching patterns in the XML file to be rendered as a row in the table.

Listing 9.8 This Simple XSL Transform File Is What Determines How the XML Data Will Be Rendered.
 <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>     <xsl:template match="/">     <style>         .value { width:"25%";font-family:courier new; font-size:.8em; white-space=pre;}     </style>     <table border="1" cellspacing="0" cellpadding="3"       bordercolor="gainsboro" width="100%">         <tr>             <th>Name</th>             <th>IceCream</th>             <th>Topping</th>             <th>Container</th>         </tr>         <xsl:for-each select='info/entry'>             <tr>                 <td ><xsl:value-of select='name'/></td>                 <td ><xsl:value-of select='icecream'/></td>                 <td ><xsl:value-of select='topping'/></td>                 <td >                    <xsl:value-of select='container'/></td>             </tr>         </xsl:for-each>     </table>     </xsl:template> </xsl:stylesheet> 

The XML file that is used to create the display on the screen is extremely simple. It has four data elements: name, ice cream, topping, and container. You could increase the number of entries by doing a copy and paste on the entry tag and editing them to create additional names, ice creams, toppings, and containers. This simple XML file can be seen in Listing 9.9.

Listing 9.9 This Is the Simple XML File, Which Will Be Rendered on the Screen.
 <?xml version="1.0" encoding="Windows-1252"?> <info>   <entry>     <name>Rick Leinecker</name>     <icecream>Chocolate</icecream>     <topping>Whipped Cream</topping>     <container>Waffle Cone</container>   </entry> </info> 

In this example, I also gave users the capability to change or edit the simple XML file. The source code can be seen in Listing 9.10. You can see this code in Listing 9.7. In this code I create an XmlTextWriter, as I did before in the section explaining XmlTextWriter. I then took the data that the users typed in in the HTML form and saved it out to an XML file. In this way, when users click the button to see the XML file, they can actually see information that they themselves have typed in.

Listing 9.10 This C# Code Saves the User Input to the XML File.

C#

 public void Transform_Click(System.Object sender, System.EventArgs e) {     try     {         XmlTextWriter Writer =           new XmlTextWriter( Request.MapPath( "IceCreamInfo.xml" ),           System.Text.Encoding.Default );         Writer.Formatting = Formatting.Indented;         Writer.WriteStartDocument();         Writer.WriteStartElement( "info" );         Writer.WriteStartElement( "entry" );         Writer.WriteStartElement( "name", "" );         Writer.WriteString( TextBox1.Text );         Writer.WriteEndElement();         Writer.WriteStartElement( "icecream", "" );         Writer.WriteString( TextBox2.Text );         Writer.WriteEndElement();         Writer.WriteStartElement( "topping", "" );         Writer.WriteString( TextBox3.Text );         Writer.WriteEndElement();         Writer.WriteStartElement( "container", "" );         Writer.WriteString( TextBox4.Text );         Writer.WriteEndElement();         Writer.WriteEndDocument();         Writer.Close();         Label1.Text = "Done!";     }     catch( Exception ex )     {         Label1.Text = ex.Message.ToString();     } } 

VB

 Public Sub Transform_Click(ByVal sender As System.Object,   ByVal e As System.EventArgs)     Try         Dim Writer As New _           XmlTextWriter(Request.MapPath("IceCreamInfo.xml"), _           System.Text.Encoding.Default );         Writer.Formatting = Formatting.Indented         Writer.WriteStartDocument()         Writer.WriteStartElement("info")         Writer.WriteStartElement("entry")         Writer.WriteStartElement("name", "")         Writer.WriteString(TextBox1.Text)         Writer.WriteEndElement()         Writer.WriteStartElement("icecream", "")         Writer.WriteString(TextBox2.Text)         Writer.WriteEndElement()         Writer.WriteStartElement("topping", "")         Writer.WriteString(TextBox3.Text)         Writer.WriteEndElement()         Writer.WriteStartElement("container", "")         Writer.WriteString(TextBox4.Text)         Writer.WriteEndElement()         Writer.WriteEndDocument()         Writer.Close()         Label1.Text = "Done!"     Catch ex As Exception ex         Label1.Text = ex.Message.ToString()     End Try End Sub 

The code that renders the XML can be seen in Listing 9.11. An XmlDocument object is created. Then the XmlDocument's Load() method is used to load the IcecreamInfo.Xml file. Next, an XslTransform object is created and its Load() method is called to load the XSL file. A DocumentNavigator class is created, which is used for the transform. Finally, the XslTransform object's Transform() method is called to carry out the transform.

Listing 9.11 The Following C# Code Loads the XML File and Does the XSLT Transform.

C#

 public void Button2_Click(System.Object sender, System.EventArgs e) {     Label2.Text = "";     try     {         StringBuilder builder = new StringBuilder("");         StringWriter writer = new StringWriter(builder);         XmlDocument doc = new XmlDocument();         doc.Load( Request.MapPath( "IceCreamInfo.xml" ) );         XslTransform tr = new XslTransform();         tr.Load( Request.MapPath( "SimpleTransform.xsl" ) );         DocumentNavigator nav = new DocumentNavigator( doc );         tr.Transform( nav, null, writer );         for( int i=0; i<builder.Length; i++)         {             Label2.Text += builder[i];         }     }     catch( Exception ex )     {         Label2.Text = ex.Message.ToString();     } } 

VB

 Public Sub Button2_Click(ByVal sender As System.Object, _   ByVal e As System.EventArgs)     Label2.Text = ""     Try         Dim builder As New StringBuilder("")         Dim writer As New StringWriter(builder)         Dim doc As New XmlDocument()         doc.Load(Request.MapPath("IceCreamInfo.xml"))         Dim rs As New XslTransform()         tr.Load(Request.MapPath("SimpleTransform.xsl"))         Dim nav As New DocumentNavigator(doc)         tr.Transform(nav, null, writer)         Dim i As Integer         For i = 0 To builder.Length - 1             Label2.Text += builder(i);         Next     Catch ex As Exception         Label2.Text = ex.Message.ToString()     End Try End Sub 

The combination of XML and XSLT provide you with a very powerful way to render XML data to the output. And beyond that, when you use XSLT, changing the XSLT data and modifying the rendering is extremely simple. Although it might take you some time to get up to speed using XSLT, it will be well worth the effort. For more information on XSLT transforms I suggest you visit www.WebMoney.com.



ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
EAN: N/A
Year: 2003
Pages: 175

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