ASP.Net/XML/Transform
Содержание
Applying XSL Transformation on an XmlDataSource Control
<%--
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">
<div>
<asp:TreeView ID="TreeView1" Runat="server"
DataSourceID="XmlDataSource1" />
<asp:XmlDataSource ID="XmlDataSource1" Runat="server"
DataFile="Bookstore.xml"
TransformFile="Bookstore.xsl" />
</div>
</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>
--%>
Load tranformation file from the web
<%@ 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>
Passing a Nodeset as a Parameter in XML transformation
<%--
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" />
<!-- Set the Discount parameter -->
<xsl:param name="booklist" select="/.."/>
<xsl:template match="/">
<html>
<title>Passing a Nodeset as a Parameter</title>
<body>
<h2>My Book Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
</tr>
<xsl:for-each select="$booklist">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
</tr>
</xsl:for-each>
</table>
</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>
--%>
Simplest XML transform
<%@ 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>
<h2>My Book Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Price</th>
</tr>
<xsl:for-each select="bookstore/book">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
--%>
XML transformation with extension
<%--
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>
<h2>My Book Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Price</th>
<th align="left">Calculated Discount</th>
</tr>
<xsl:for-each select="bookstore/book">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
<td>
<xsl:value-of select="myDiscount:ReturnDiscount(price)" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
--%>
XML transformation with parameter
<%--
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" />
<!-- Set the Discount parameter -->
<xsl:param name="discount" select=".10" />
<xsl:template match="/">
<html>
<title>XSL Transformation</title>
<body>
<h2>My Book Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Price</th>
<th align="left">Calculated Discount</th>
</tr>
<xsl:for-each select="bookstore/book">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
<td>
<xsl:value-of select="price * ($discount)"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
--%>
XML transformation with script in the xls
<%@ 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>
<h2>My Book Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Price</th>
<th align="left">Calculated Discount</th>
</tr>
<xsl:for-each select="bookstore/book">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
<td>
<xsl:value-of select="myDiscount:ReturnDiscount(price)" />
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
--%>