.NET
  Home arrow .NET arrow Page 8 - Outputting Code
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Outputting Code
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2004-10-04

    Table of Contents:
  • Outputting Code
  • Understanding Script Directives
  • Understanding Other Features of the Generation Harness
  • Extending the Harness
  • Examining Code Generation Mechanics
  • Exploring Details of XSLT Code Generation
  • Creating Named Templates
  • Creating Match Templates
  • Supporting Stylesheets
  • Exploring Details of Brute-Force Generation
  • Creating a Class
  • Creating the Support Template
  • Understanding Types
  • Building a CodeDOM Graph
  • Building the Code Structure
  • Outputting Assignments
  • Creating Arrays
  • Exploring Other Features
  • Working with the CodeDOM

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Outputting Code - Creating Match Templates


    (Page 8 of 19 )

    Match templates provide a mechanism to segment your XSLT stylesheet and simultaneously loop through a set of nodes. This is very handy. Using match templates instead of xsl:for-each directives will not only show the world that you’re a competent XSLT jock but also will result in cleaner, more readable stylesheets.

    The Orders.vb file contains a columnNameColumnInfo method and a columnName property for each column in the table. This function and property are shown near the end of Listing 3-2. The columnNameColumnInfo method returns an object containing information about the column. This type of information is frequently helpful to UI programmers using the data container class. The columnName property wraps the actual data.

    Building a match template is much like building a named template. The key differences are that named templates run only once each time they’re called and don’t change the context within the XML metadata input file. Match templates, on the other hand, may run zero, one, or many times each time they’re called. Match templates execute in the context of the selected node—the current member of the node list. This is similar to the xsl:for-each directive’s behavior.

    To build this template, copy the method and property blocks for any one of the columns from the target file into the template and identify the changeable items. These changeable items are retrieved from column’s information in the metadata file, which is the current context when the output file is generated.

    This template handles the problem with missing .NET types a little differently. Rather than skipping the code, it outputs explanatory comments within an xsl:choose directive if the NETType attribute is missing or empty:

    <xsl:template match="dbs:TableColumn" mode="ColumnMethods" >
    <xsl:choose>
    <xsl:when test="string-length(@NETType)=0">
    ' TODO: Column <xsl:value-of select="@Name"/> is not included because it uses

    ' a SQLType (<xsl:value-of select="@SQLType"/>) that is not yet supported
    </xsl:when>
    <xsl:otherwise>

    Public Function <xsl:value-of select="@Name"/>
    <xsl:text/>ColumnInfo As ColumnInfo
    Dim columnInfo As New ColumnInfo
    columnInfo.FieldName = "<xsl:value-of select="@Name"/>" columnInfo.FieldType = GetType(</xsl:text><xsl:value-of select="@NETType"/>)
    columnInfo.SQLType = "<xsl:value-of select="@SQLType"/>" columnInfo.Caption = "<xsl:value-of select="@Caption"/>" columnInfo.Desc = "<xsl:value-of select="@Desc"/>"
    Return columnInfo

    End Function

    Public Property <xsl:value-of select="@Name"/> As <xsl:text/>
    <xsl:value-of select="@NETType"/><xsl:call-template name="NewLine"/>
    Get
    Return m<xsl:value-of select="@Name"/><xsl:call-template name="NewLine"/>
    End Get
    Set(ByVal Value As <xsl:value-of select="@NETType"/>)
    m<xsl:value-of select="@Name"/> = Value
    End Set
    End Property
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>

    NOTE: The XPath expressions used by the xsl:value-of directive allows you to access any piece of information in the XML meta-data file, even if it’s nowhere close to the node you’re currently processing. If you manage to find a problem you can’t solve with XPath, Microsoft’s implementation of XSLT supports both script and calling back to .NET objects. The sky is the limit of what you can do with XSLT—but it won’t always be easy.

     

    This chapter is from Code Generation in Microsoft .NET by Kathleen Dollard (Apress, 2004, ISBN: 1590591372). Check it out at your favorite bookstore today.

    Buy this book now.

    More .NET Articles
    More By Apress Publishing


     

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek