Nasıl yapılır: Derleme kullanarak XSLT dönüşümü gerçekleştirme
Not
Betik blokları yalnızca .NET Framework'te desteklenir. .NET Core veya .NET 5 veya üzeri sürümlerde desteklenmez.
XSLT derleyicisi (xsltc.exe), XSLT stil sayfalarını derler ve bir derleme oluşturur. Derleme doğrudan yöntemine XslCompiledTransform.Load(Type) geçirilebilir.
XML ve XSLT dosyalarını yerel bilgisayarınıza kopyalamak için
XSLT dosyasını yerel bilgisayarınıza kopyalayın ve Transform.xsl olarak adlandırın.
<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 string discount(string price){ char[] trimChars = { '$' }; //trim leading $, convert price to type double double discount_value = Convert.ToDouble(price.TrimStart(trimChars)); //apply 10% discount and round appropriately discount_value = .9*discount_value; //convert value to decimal and format as currency string discount_price = discount_value.ToString("C"); return discount_price; } ]]> </msxsl:script> <xsl:template match="catalog"> <html> <head></head> <body> <table border="1"> <tr> <th align="left">Title</th> <th align="left">Author</th> <th align="left">Genre</th> <th align="left">Publish Date</th> <th align="left">Price</th> </tr> <xsl:for-each select="book"> <tr> <td> <xsl:value-of select="title"/> </td> <td> <xsl:value-of select="author"/> </td> <td> <xsl:value-of select="genre"/> </td> <td> <xsl:value-of select="publish_date"/> </td> <xsl:choose> <xsl:when test="genre = 'Fantasy'"> <td> <xsl:value-of select="user:discount(price)"/> </td> </xsl:when> <xsl:otherwise> <td> <xsl:value-of select="price"/> </td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
XML dosyasını yerel bilgisayarınıza kopyalayın ve olarak adlandırın
books.xml
.<?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>$44.95</price> <publish_date>2000-10-01</publish_date> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>$5.95</price> <publish_date>2000-12-16</publish_date> </book> <book id="bk103"> <author>Corets, Eva</author> <title>Maeve Ascendant</title> <genre>Fantasy</genre> <price>$5.95</price> <publish_date>2000-11-17</publish_date> </book> <book id="bk106"> <author>Randall, Cynthia</author> <title>Lover Birds</title> <genre>Romance</genre> <price>$4.95</price> <publish_date>2000-09-02</publish_date> </book> <book id="bk107"> <author>Thurman, Paula</author> <title>Splish Splash</title> <genre>Romance</genre> <price>$4.95</price> <publish_date>2000-11-02</publish_date> </book> </catalog>
Betiğin etkin olduğu stil sayfasını derlemek için
Aşağıdaki komut ve Transform_Script1.dll
adlı Transform.dll
iki derleme oluşturur. Bu varsayılan davranıştır. Aksi belirtilmedikçe, sınıfın ve derlemenin adı varsayılan olarak ana stil sayfasının adıdır.
xsltc /settings:script+ Transform.xsl
Aşağıdaki komut açıkça sınıf adını Dönüştür olarak ayarlar:
xsltc /settings:script+ /class:Transform Transform.xsl
Kodunuzu derlerken derlenmiş derlemeyi başvuru olarak eklemek için
Çözüm Gezgini veya komut satırından bir başvuru ekleyerek Visual Studio'ya bir derleme ekleyebilirsiniz.
C# içeren komut satırı için aşağıdakileri kullanın:
csc myCode.cs /r:system.dll;system.xml.dll;Transform.dll
Visual Basic ile komut satırı için aşağıdakileri kullanın
vbc myCode.vb /r:system.dll;system.xml.dll;Transform.dll
Derlenmiş derlemeyi kodunuzda kullanmak için
Aşağıdaki örnekte, derlenmiş stil sayfasını kullanarak XSLT dönüştürmesinin nasıl yürütüldiği gösterilmektedir.
using System;
using System.Xml.Xsl;
class Example
{
static void Main()
{
//Create a new XslCompiledTransform and load the compiled transformation.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(typeof(Transform));
// Execute the transformation and output the results to a file.
xslt.Transform("books.xml", "discount_books.html");
}
}
Imports System.Xml.Xsl
Module Module1
Sub Main()
'Create a new XslCompiledTransform and load the compiled transformation.
Dim xslt As New XslCompiledTransform()
xslt.Load(GetType(Transform))
'Execute the transform and output the results to a file.
xslt.Transform("books.xml", "discount_books.html")
End Sub
End Module
Derlenmiş derlemeye dinamik olarak bağlanmak için
xslt.Load(typeof(Transform));
örneklerini şununla değiştirin:
xslt.Load(System.Reflection.Assembly.Load("Transform").GetType("Transform"));
yöntemi hakkında Assembly.Load
daha fazla bilgi için bkz Load. .