Click here to Skip to main content
15,885,278 members
Articles / Productivity Apps and Services / Microsoft Office

Document and Code Generation by LINQ and XSL

Rate me:
Please Sign up or sign in to vote.
3.87/5 (8 votes)
3 Aug 2008CPOL15 min read 31.8K   339   43  
An article on how to generate source code as well as populate Excel Spreadsheets.
Public Module Xslt_Final

	Public Function ClassTransform_Final() As XElement

		Dim xslt = <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
					   xmlns:msxsl="urn:schemas-microsoft-com:xslt"
					   xmlns:user="urn:my-scripts">

					   <msxsl:script language="C#" implements-prefix="user">
						   <![CDATA[
public static string CreateProperty(string fieldType, string fieldName)
{
	StringBuilder sb = new StringBuilder();

	sb.Append("\n");
	sb.Append("		/// <summary>\n");
	sb.Append("		/// Get/Set for " + PropertyName(fieldName) + "\n");
	sb.Append("		/// </summary>\n");
	sb.Append("		public " + FieldType(fieldType) + " " + PropertyName(fieldName) + "\n" );
	sb.Append("		{\n");
	sb.Append("		    get { return (" + FieldName(fieldName) + "); }\n");
	sb.Append("		    set { " + FieldName(fieldName) + " = value; }\n");
	sb.Append("		}\n");

	return (sb.ToString());
}

public static string FieldType(string fieldtype)
{
	switch (fieldtype.ToLower())
	{
		case "smallint":
			return "short";
		case "uniqueidentifier":
			return "Guid";
		case "varchar":
			return "string";
		default:
			return fieldtype;
	}
}

public static  string FieldName(string fieldtype)
{
	int index = 0;

	StringBuilder sb = new StringBuilder(fieldtype);

	for (index = 0; index < sb.Length; index++)
	{
		if (char.IsUpper(sb[index]))
		{
			sb[index] = Char.ToLower(sb[index]);
		}
		else if (char.IsLower(sb[index]))
		{
			if (index > 0)
			{
				sb[index - 1] = Char.ToUpper(sb[index - 1]);
			}

			break;
		}
	}

    return "_" + sb.ToString();
}

public static string PropertyName(string fieldname)
{
	string propertyName = FieldName(fieldname);
	propertyName = Char.ToUpper(propertyName[1]) + propertyName.Substring(2);

	return propertyName;
}

public string InsertText(string text)
{
    return (text);
}
                            ]]>

					   </msxsl:script>

					   <xsl:template match="class_1">
						   <xsl:value-of select="data_1"/>

						   <xsl:for-each select="data_2/data_2a">
							   <xsl:value-of select="text()"/>
							   <xsl:value-of select="user:FieldType(fieldtype)"/>
							   <xsl:value-of select="user:InsertText(' ')"/>
							   <xsl:value-of select="user:FieldName(fieldname)"/>
							   <xsl:value-of select="user:InsertText(';')"/>
						   </xsl:for-each>


						   <xsl:value-of select="data_3"/>
						   <xsl:value-of select="data_4"/>
						   <xsl:value-of select="data_5"/>

						   <xsl:for-each select="data_6/data_6a">
							   <xsl:value-of select="user:CreateProperty(fieldtype, fieldname)" disable-output-escaping="yes"/>
						   </xsl:for-each>

						   <xsl:value-of select="data_7"/>

					   </xsl:template>
				   </xsl:stylesheet>

		Return xslt

	End Function

End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Webbert Solutions
United States United States
Dave is an independent consultant working in a variety of industries utilizing Microsoft .NET technologies.

Comments and Discussions