XSL, or sometimesXSLT, 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 8.4.

Figure 8.4. A simple XML file is saved and then rendered with an XSL transform.


The XSLT file for this example is not very complicated. It can be seen in Listing 8.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 8.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% graphics/ccc.gif ">          <tr>              <th>Name</th>              <th>IceCream</th>              <th>Topping</th>              <th>Container</th>          </tr>          <xsl:for-each select='info/entry'>              <tr>                  <td class="value"><xsl:value-of select='name'/></td>                  <td class="value"><xsl:value-of select='icecream'/></td>                  <td class="value"><xsl:value-of select='topping'/></td>                  <td class="value"><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 8.9.

Listing 8.9 The Simple XML File That Will Be Rendered Onscreen
 <?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 ability to change or edit the simple XML file. You can see this code in Listing 8.10. 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 the HTML form and saved it out to an XML file. In this way when users go and click on the button to see the XML file they can actually see information that they themselves have typed in.

Listing 8.10 This C# Code Saves the User Input into the XML File
 public void Button1_Click(System.Object sender, System.EventArgs e)  {      try      {          XmlTextWriter Writer = new XmlTextWriter( Request.MapPath( "IceCreamInfo.xml" ), graphics/ccc.gif 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;      }  } 

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

Listing 8.11 This C# Code Loads the XML File and Does the XSLT Transform
 private void Button1_Click(object sender, System.EventArgs e)  {    Label2.Text = "";       try    {      XslTransform xslt = new XslTransform();      xslt.Load( Request.MapPath( "SimpleTransform.xsl" ) );      XPathDocument doc = new XPathDocument( Request.MapPath( "IceCreamInfo.xml" ) );      StringBuilder builder = new StringBuilder("");      StringWriter writer = new StringWriter(builder);      xslt.Transform( doc, null, writer );      for( int i=0; i<builder.Length; i++)      {        Label2.Text += builder[i];      }    }    catch( Exception ex )    {      Label2.Text = ex.Message;    }  } 

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 this example 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.


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

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