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. |
Next: Supporting Stylesheets >>
More .NET Articles
More By Apress Publishing