Útmutató: XSLT-átalakítás végrehajtása szerelvény használatával
Feljegyzés
A szkriptblokkok csak .NET-keretrendszer támogatottak. A .NET Core vagy a .NET 5 vagy újabb verziók nem támogatják őket.
Az XSLT fordító (xsltc.exe) lefordítja az XSLT stíluslapokat, és létrehoz egy szerelvényt. A szerelvény közvetlenül a metódusba XslCompiledTransform.Load(Type) továbbítható.
Az XML- és XSLT-fájlok másolása a helyi számítógépre
Másolja az XSLT-fájlt a helyi számítógépre, és nevezze el Transform.xsl néven.
<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>
Másolja az XML-fájlt a helyi számítógépre, és nevezze el.
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>
A stíluslap fordítása engedélyezett szkripttel
A következő parancs két szerelvényt hoz létre, és Transform.dll
Transform_Script1.dll
a következőt: Ez az alapértelmezett viselkedés. Ha másként nincs megadva, az osztály és a szerelvény neve alapértelmezés szerint a fő stíluslap neve lesz.
xsltc /settings:script+ Transform.xsl
Az alábbi parancs explicit módon átalakítja az osztály nevét:
xsltc /settings:script+ /class:Transform Transform.xsl
A lefordított szerelvény hivatkozásként való felvétele a kód lefordításakor
Szerelvényt a Visual Studióba is felvehet, ha hozzáad egy hivatkozást a Megoldáskezelő vagy a parancssorból.
A C#-tal rendelkező parancssorhoz használja a következőket:
csc myCode.cs /r:system.dll;system.xml.dll;Transform.dll
A Visual Basic parancssorához használja a következőt:
vbc myCode.vb /r:system.dll;system.xml.dll;Transform.dll
A lefordított szerelvény használata a kódban
Az alábbi példa bemutatja, hogyan hajthatja végre az XSLT-átalakítást a lefordított stíluslap használatával.
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
A lefordított szerelvényhez való dinamikus csatoláshoz cserélje le a
xslt.Load(typeof(Transform));
nevű és
xslt.Load(System.Reflection.Assembly.Load("Transform").GetType("Transform"));
A metódussal kapcsolatos további információkért Assembly.Load
lásd: Load.
Lásd még
Visszajelzés
https://aka.ms/ContentUserFeedback.
Hamarosan elérhető: 2024-ben fokozatosan kivezetjük a GitHub-problémákat a tartalom visszajelzési mechanizmusaként, és lecseréljük egy új visszajelzési rendszerre. További információ:Visszajelzés küldése és megtekintése a következőhöz: