ASP.Net/XML/Transform
Содержание
- 1 Applying XSL Transformation on an XmlDataSource Control
- 2 Load tranformation file from the web
- 3 Passing a Nodeset as a Parameter in XML transformation
- 4 My Book Collection
- 5 Simplest XML transform
- 6 My Book Collection
- 7 XML transformation with extension
- 8 My Book Collection
- 9 XML transformation with parameter
- 10 My Book Collection
- 11 XML transformation with script in the xls
- 12 My Book Collection
Applying XSL Transformation on an XmlDataSource Control
<source lang="csharp">
<%-- Code Revised from
Professional ASP.NET 2.0 XML (Programmer to Programmer) (Paperback) by Thiru Thangarathinam
- Publisher: Wrox (January 18, 2006)
- Language: English
- ISBN: 0764596772
--%> <%@ Page Language="C#" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head>
<title>Applying XSL Transformation on an XmlDataSource Control</title>
</head> <body>
<form id="form1" runat="server">
<asp:TreeView ID="TreeView1" Runat="server" DataSourceID="XmlDataSource1" /> <asp:XmlDataSource ID="XmlDataSource1" Runat="server" DataFile="Bookstore.xml" TransformFile="Bookstore.xsl" />
</form>
</body> </html>
<%-- <bookstore>
<genre name="Fiction"> <book ISBN="10-861003-324" Title="title 1" Price="19.99"> <chapter num="1" name="Introduction"> A </chapter> <chapter num="2" name="Body"> B </chapter> <chapter num="3" name="Conclusion"> C </chapter> </book> <book ISBN="1-861001-57-5" Title="title " Price="24.95"> <chapter num="1" name="Introduction"> D </chapter> <chapter num="2" name="Body"> E </chapter> <chapter num="3" name="Conclusion"> F </chapter> </book> </genre> <genre name="NonFiction"> <book ISBN="10-861003-324" Title="title 2" Price="19.99"> <chapter num="1" name="Introduction"> G </chapter> <chapter num="2" name="Body"> H </chapter> <chapter num="3" name="Conclusion"> I </chapter> </book> <book ISBN="1-861001-57-6" Title="title 3" Price="27.95"> <chapter num="1" name="Introduction"> J </chapter> <chapter num="2" name="Body"> K </chapter> <chapter num="3" name="Conclusion"> L </chapter> </book> </genre>
</bookstore> --%>
<%-- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="bookstore"> <bookstore> <xsl:apply-templates select="genre"/> </bookstore> </xsl:template> <xsl:template match="genre"> <genre> <xsl:attribute name="name"> <xsl:value-of select="@name"/> </xsl:attribute> <xsl:apply-templates select="book"/> </genre> </xsl:template> <xsl:template match="book"> <book> <xsl:attribute name="ISBN"> <xsl:value-of select="@ISBN"/> </xsl:attribute> <xsl:element name="title"> <xsl:value-of select="title"/> </xsl:element> <xsl:element name="price"> <xsl:value-of select="price"/> </xsl:element> <xsl:apply-templates select="chapters/chapter" /> </book> </xsl:template> <xsl:template match="chapter"> <chapter> <xsl:attribute name="num"> <xsl:value-of select="@num"/> </xsl:attribute> <xsl:attribute name="name"> <xsl:value-of select="@name"/> </xsl:attribute> <xsl:apply-templates/> </chapter> </xsl:template>
</xsl:stylesheet>
--%>
</source>
Load tranformation file from the web
<source lang="csharp">
<%@ Page Language="C#" %> <%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.Xml" %> <%@ Import Namespace="System.Xml.Xsl" %> <%@ Import Namespace="System.Xml.XPath" %> <script runat="server">
void Page_Load(object sender, System.EventArgs e) { string xmlPath = Request.PhysicalApplicationPath + @"\Books.xml"; XmlSecureResolver resolver = new XmlSecureResolver(new XmlUrlResolver(), "http://localhost"); NetworkCredential cred = new NetworkCredential("Administrator", "thiru","THIRU-SERVER3"); resolver.Credentials = cred; XPathDocument xpathDoc = new XPathDocument(xmlPath); XslCompiledTransform transform = new XslCompiledTransform(); XsltSettings settings = new XsltSettings(); settings.EnableDocumentFunction = true; //Load the XSL stylsheet into the XslCompiledTransform object transform.Load("http://localhost/Books.xsl", settings, resolver); transform.Transform(xpathDoc, null, Response.Output); }
</script>
</source>
Passing a Nodeset as a Parameter in XML transformation
<source lang="csharp">
<%-- Code Revised from
Professional ASP.NET 2.0 XML (Programmer to Programmer) (Paperback) by Thiru Thangarathinam
- Publisher: Wrox (January 18, 2006)
- Language: English
- ISBN: 0764596772
--%>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e) { string xmlPath = MapPath("BooksWithStyle.xml"); string xslPath = MapPath("Books_with_nodeset_parameter.xsl"); XPathDocument xpathDoc = new XPathDocument(xmlPath); XslCompiledTransform transform = new XslCompiledTransform(); XsltArgumentList argsList = new XsltArgumentList(); XPathNavigator navigator = xpathDoc.CreateNavigator(); XPathNodeIterator iterator = navigator.Select("/bookstore/book"); argsList.AddParam("booklist", "", iterator); //Load the XSL stylsheet into the XslCompiledTransform object transform.Load(xslPath); transform.Transform(xpathDoc, argsList, Response.Output); }
</script>
<%-- Books_with_nodeset_parameter.xsl <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" /> <xsl:param name="booklist" select="/.."/> <xsl:template match="/"> <html> <title>Passing a Nodeset as a Parameter</title> <body>
My Book Collection
Title |
---|
<xsl:value-of select="title"/> |
</body> </html> </xsl:template>
</xsl:stylesheet> --%> <%-- BooksWithStyle.xml <?xml version="1.0"?> <bookstore>
<book genre="A"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>99.99</price> </book> <book genre="B"> <title>title 2</title> <author> <first-name>B</first-name> <last-name>C</last-name> </author> <price>11.99</price> </book>
</bookstore> --%>
</source>
Simplest XML transform
<source lang="csharp">
<%@ Page Language="C#" %> <%@ Import Namespace="System.Xml" %> <%@ Import Namespace="System.Xml.Xsl" %> <%@ Import Namespace="System.Xml.XPath" %> <script runat="server">
void Page_Load(object sender, System.EventArgs e) { string xmlPath = MapPath("BooksWithStyle.xml"); string xslPath = MapPath("Books.xsl"); XPathDocument xpathDoc = new XPathDocument(xmlPath); XslCompiledTransform transform = new XslCompiledTransform(); //Load the XSL stylsheet into the XslCompiledTransform object transform.Load(xslPath); transform.Transform(xpathDoc, null, Response.Output); }
</script> <%-- BooksWithStyle.xml <?xml version="1.0"?> <bookstore>
<book genre="A"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>99.99</price> </book> <book genre="B"> <title>title 2</title> <author> <first-name>B</first-name> <last-name>C</last-name> </author> <price>11.99</price> </book>
</bookstore> --%>
<%-- Books.xsl <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" /> <xsl:template match="/"> <html> <title>XSL Transformation</title> <body>
My Book Collection
Title | Price |
---|---|
<xsl:value-of select="title"/> |
<xsl:value-of select="price"/> |
</body> </html> </xsl:template>
</xsl:stylesheet> --%>
</source>
XML transformation with extension
<source lang="csharp">
<%-- Code Revised from
Professional ASP.NET 2.0 XML (Programmer to Programmer) (Paperback) by Thiru Thangarathinam
- Publisher: Wrox (January 18, 2006)
- Language: English
- ISBN: 0764596772
--%>
<%@ Page Language="C#" %> <%@ Import Namespace="System.Xml" %> <%@ Import Namespace="System.Xml.Xsl" %> <%@ Import Namespace="System.Xml.XPath" %> <script runat="server"> public class Discount {
public Discount() { } public string ReturnDiscount(string price) { decimal priceValue = Convert.ToDecimal(price); return (priceValue * 15 / 100).ToString(); }
}
void Page_Load(object sender, System.EventArgs e) { string xmlPath = MapPath("BooksWithStyle.xml"); string xslPath = MapPath("Books_with_extensions.xsl"); XPathDocument xpathDoc = new XPathDocument(xmlPath); XslCompiledTransform transform = new XslCompiledTransform(); XsltArgumentList argsList = new XsltArgumentList(); Discount obj = new Discount(); argsList.AddExtensionObject("urn:myDiscount", obj); //Load the XSL stylsheet into the XslCompiledTransform object transform.Load(xslPath); transform.Transform(xpathDoc, argsList, Response.Output); }
</script> <%-- <?xml version="1.0"?> <bookstore>
<book genre="A"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>99.99</price> </book> <book genre="B"> <title>title 2</title> <author> <first-name>B</first-name> <last-name>C</last-name> </author> <price>11.99</price> </book>
</bookstore> --%> <%-- <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myDiscount="urn:myDiscount">
<xsl:output method="html" /> <xsl:template match="/"> <html> <title>XSL Transformation</title> <body>
My Book Collection
Title | Price | Calculated Discount |
---|---|---|
<xsl:value-of select="title"/> |
<xsl:value-of select="price"/> |
<xsl:value-of select="myDiscount:ReturnDiscount(price)" /> |
</body> </html> </xsl:template>
</xsl:stylesheet> --%>
</source>
XML transformation with parameter
<source lang="csharp">
<%-- Code Revised from
Professional ASP.NET 2.0 XML (Programmer to Programmer) (Paperback) by Thiru Thangarathinam
- Publisher: Wrox (January 18, 2006)
- Language: English
- ISBN: 0764596772
--%>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<script runat="server">
void Page_Load(object sender, System.EventArgs e) { string xmlPath = MapPath("BooksWithStyle.xml"); string xslPath = MapPath("Books_with_parameters.xsl"); XPathDocument xpathDoc = new XPathDocument(xmlPath); XslCompiledTransform transform = new XslCompiledTransform(); XsltArgumentList argsList = new XsltArgumentList(); argsList.AddParam("discount", "", ".15"); //Load the XSL stylsheet into the XslCompiledTransform object transform.Load(xslPath); transform.Transform(xpathDoc, argsList, Response.Output); }
</script>
<%--BooksWithStype.xml <?xml version="1.0"?> <bookstore>
<book genre="A"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>99.99</price> </book> <book genre="B"> <title>title 2</title> <author> <first-name>B</first-name> <last-name>C</last-name> </author> <price>11.99</price> </book>
</bookstore> --%>
<%--Books_with_parameters.xsl <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" /> <xsl:param name="discount" select=".10" /> <xsl:template match="/"> <html> <title>XSL Transformation</title> <body>
My Book Collection
Title | Price | Calculated Discount |
---|---|---|
<xsl:value-of select="title"/> |
<xsl:value-of select="price"/> |
<xsl:value-of select="price * ($discount)"/> |
</body> </html> </xsl:template>
</xsl:stylesheet> --%>
</source>
XML transformation with script in the xls
<source lang="csharp">
<%@ Page Language="C#" %> <%@ Import Namespace="System.Xml" %> <%@ Import Namespace="System.Xml.Xsl" %> <%@ Import Namespace="System.Xml.XPath" %> <script runat="server">
void Page_Load(object sender, System.EventArgs e) { string xmlPath = MapPath("BooksWithStyle.xml"); string xslPath = MapPath("Books_with_script.xsl"); XPathDocument xpathDoc = new XPathDocument(xmlPath); XsltSettings settings = new XsltSettings(false, true); XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(xslPath, settings, null); transform.Transform(xpathDoc, null, Response.Output); }
</script>
<%-- BooksWithStyle.xml <?xml version="1.0"?> <bookstore>
<book genre="A"> <title>title 1</title> <author> <first-name>A</first-name> <last-name>B</last-name> </author> <price>99.99</price> </book> <book genre="B"> <title>title 2</title> <author> <first-name>B</first-name> <last-name>C</last-name> </author> <price>11.99</price> </book>
</bookstore> --%>
<%-- Books_with_script.xsl <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:myDiscount="urn:myDiscount">
<msxsl:script language="C#" implements-prefix="myDiscount"> // add cdData tag here public string ReturnDiscount(string price) { decimal priceValue = Convert.ToDecimal(price); return (priceValue * 15/100).ToString(); } </msxsl:script> <xsl:output method="html" /> <xsl:template match="/"> <html> <title>XSL Transformation</title> <body>
My Book Collection
Title | Price | Calculated Discount |
---|---|---|
<xsl:value-of select="title"/> |
<xsl:value-of select="price"/> |
<xsl:value-of select="myDiscount:ReturnDiscount(price)" /> |
</body> </html> </xsl:template>
</xsl:stylesheet> --%>
</source>