c# code 39 checksum C HA PTER 6 TRAN SFORM IN G XML WITH XSLT in C#.NET

Encoding Code 39 Full ASCII in C#.NET C HA PTER 6 TRAN SFORM IN G XML WITH XSLT

C HA PTER 6 TRAN SFORM IN G XML WITH XSLT
Make ANSI/AIM Code 39 In C#
Using Barcode generator for VS .NET Control to generate, create Code 39 Full ASCII image in VS .NET applications.
www.OnBarcode.com
Code 39 Full ASCII Scanner In C#
Using Barcode scanner for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
<th>Home Phone</th> <th>Birth Date</th> <th>Notes</th> </tr> <xsl:for-each select="employees/employee"> <tr> <td> <xsl:value-of select="@employeeid"/> </td> <td> <xsl:value-of select="firstname"/> </td> <td> <xsl:value-of select="lastname"/> </td> <td> <xsl:value-of select="homephone"/> </td> <td> <xsl:value-of select="myscripts:GetBirthDate(@employeeid)"/> </td> <td> <xsl:value-of select="notes"/> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Notice the markup in bold. The <xsl:stylesheet> element now has two more attributes. The xmlns:msxsl attribute defines the msxml prefix for the urn:schemas-microsoft-com:xslt namespace. Similarly, the xmlns:myscripts attribute defines a myscripts prefix. The <msxml:script> block defines one or more functions that are used in the style sheet. The language attribute of the script block indicates the coding language (C# in our example). Your code may need to add a reference to external assemblies, which is done by using the <msxsl:assembly> element. The name attribute of this tag specifies the name of the assembly, excluding the extension. Similarly, the <msxml:using> tag specifies the namespaces to be imported. The actual function is placed in a CDATA section. In our example, we defined a function called GetBirthDate() that accepts the ID of the employee whose date of birth is to be retrieved and returns the birth date in MM/dd/yyyy format. The code of the function connects with the Northwind database, fires a SELECT query against the employees table, and retrieves the birth date.
GTIN - 13 Printer In Visual C#.NET
Using Barcode maker for VS .NET Control to generate, create EAN-13 image in .NET applications.
www.OnBarcode.com
Barcode Maker In Visual C#
Using Barcode printer for .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
CH APT ER 6 T RANS FO RMI NG XM L WI T H XS LT
Paint 2D In C#.NET
Using Barcode encoder for .NET framework Control to generate, create Matrix 2D Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Draw GS1-128 In C#.NET
Using Barcode printer for VS .NET Control to generate, create GS1-128 image in .NET applications.
www.OnBarcode.com
Note Make sure to change the database connection string to suit your development environment.
GS1 - 12 Maker In C#
Using Barcode maker for .NET Control to generate, create UPC-A image in .NET framework applications.
www.OnBarcode.com
Generating ISBN - 10 In Visual C#
Using Barcode generator for Visual Studio .NET Control to generate, create International Standard Book Number image in .NET framework applications.
www.OnBarcode.com
The GetBirthDate() function is called later in the style sheet by using the namespaceprefix: function_name(parameter list) syntax. The GetBirthDate() function expects the employee ID, which is passed by using the employeeid attribute (@employeeid) as a parameter. To test our new style sheet, we will develop an application like the one shown in Figure 6-7.
Code 3/9 Generation In Java
Using Barcode creation for Java Control to generate, create Code 39 image in Java applications.
www.OnBarcode.com
Paint Code 39 In None
Using Barcode generation for Online Control to generate, create USS Code 39 image in Online applications.
www.OnBarcode.com
Figure 6-7. Application for testing our embedded function The application user interface is the same as I discussed earlier (see Figure 6-5). However, the code inside the Transform button is slightly different. Listing 6-15 highlights these differences. Listing 6-15. Enabling Scripting private void button1_Click(object sender, EventArgs e) { XsltSettings settings = new XsltSettings(); settings.EnableScript = true; XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(textBox2.Text,settings,null); xslt.Transform(textBox1.Text, textBox3.Text); if (checkBox1.Checked) { System.Diagnostics.Process.Start(textBox3.Text); } } The XsltSettings class specifies the features to support during the transformation. The EnableScript property indicates whether to enable embedded script blocks. The Load() method of the XslCompiledTransform class accepts the XsltSettings object as one of its parameters. If you run the application and specify the three paths, you will get output as shown in Figure 6-8.
Draw European Article Number 13 In Java
Using Barcode generator for BIRT reports Control to generate, create EAN 13 image in BIRT applications.
www.OnBarcode.com
Barcode Maker In None
Using Barcode drawer for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
C HA PTER 6 TRAN SFORM IN G XML WITH XSLT
Barcode Generator In .NET Framework
Using Barcode generation for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
Scan GS1-128 In Visual C#.NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Figure 6-8. Output with the Birth Date column added
Reading Barcode In VS .NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Barcode Creator In None
Using Barcode generation for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Using Extension Objects
EAN-13 Supplement 5 Generation In None
Using Barcode generator for Software Control to generate, create EAN-13 image in Software applications.
www.OnBarcode.com
Printing Code 3/9 In None
Using Barcode encoder for Software Control to generate, create ANSI/AIM Code 39 image in Software applications.
www.OnBarcode.com
Embedding scripts in the style sheet does indeed provide a handy way to perform operations that are beyond the capabilities of XSLT. However, it doesn t provide a good mechanism for reusing your code. What if you wish to use the same function elsewhere in the application or in other style sheets This is where extension objects come into the picture. Simply put, extension objects are objects external to the style sheet that provide some functionality to the style sheet. Extension objects promote greater code reuse and are more flexible and maintainable than embedded script blocks. To illustrate the use of extension objects, we will modify our previous example. First, we will put the GetBirthDate() function in a separate class called Employee rather than embedding it in the style sheet. The newly created Employee class should look similar to Listing 6-16. Listing 6-16. Placing the GetBirthDate() Function in a Class class Employee { public string GetBirthDate(int employeeid) { SqlConnection cnn = new SqlConnection(@"data source=.\sqlexpress;initial catalog=northwind;integrated security=true"); SqlCommand cmd = new SqlCommand(); cmd.Connection = cnn; cmd.CommandText = "SELECT birthdate FROM employees WHERE employeeid=@id"; SqlParameter pDOB = new SqlParameter("@id", employeeid); cmd.Parameters.Add(pDOB);
Data Matrix Reader In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Reading Data Matrix 2d Barcode In Visual Basic .NET
Using Barcode reader for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
CH APT ER 6 T RANS FO RMI NG XM L WI T H XS LT
cnn.Open(); object obj = cmd.ExecuteScalar(); cnn.Close(); DateTime dob = DateTime.Parse(obj.ToString()); return dob.ToString("MM/dd/yyyy"); } } The function by itself is the same one that we used before, but it has been encapsulated in the Employee class. Next, you need to modify the Click event handler of the Transform button to resemble Listing 6-17. Listing 6-17. Using Extension Objects private void button1_Click(object sender, EventArgs e) { XsltSettings settings = new XsltSettings(); settings.EnableScript = true; XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load(textBox2.Text,settings,null); XsltArgumentList arguments = new XsltArgumentList(); Employee employee = new Employee(); arguments.AddExtensionObject("urn:myscripts", employee); FileStream stream = new FileStream(textBox3.Text, FileMode.Create); xslt.Transform(textBox1.Text, arguments,stream); stream.Close(); if (checkBox1.Checked) { System.Diagnostics.Process.Start(textBox3.Text); } } Notice the code marked in bold. After loading the style sheet using the Load() method as before, it creates an instance of the XsltArgumentList class, which we used when passing parameters to the style sheet. This time, however, the code uses the AddExtensionObject() method of the XsltArgumentList class. This method accepts the namespace URI and an instance of the extension object. In our case, the Employee class instance acts as an extension object. While calling the Transform() method of XslCompiledTransform, the XsltArgumentList object is passed to it. If you run the application now, you should get a result identical to the previous example.
Copyright © OnBarcode.com . All rights reserved.