Passing a Nodeset as a Parameter in XML transformation : Transform « XML « ASP.Net






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>

--%>
           
       








Related examples in the same category

1.Applying XSL Transformation on an XmlDataSource Control
2.Simplest XML transform
3.XML transformation with parameter
4.XML transformation with extension
5.XML transformation with script in the xls
6.Load tranformation file from the web