Xml Server Control"-->
only for RuBoard |
The Xml server control can be used to display either raw XML output or XML data with an XSL/T stylesheet. All in all, this is a rather simple server control with only a few properties. Table 12.1 shows the properties of the Xml server control.
Property | Get or Set | Description |
---|---|---|
DocumentSource | Get/Set | Gets or sets the path to the XML file to display. |
TransformSource | Get/Set | Gets or sets the path to the XSL/T stylesheet to use. |
Document | Get/Set | Gets or sets the XmlDocument to display. |
Transform | Get/Set | Gets or sets the XslTransform to display. |
To display an XML file in the browser using the Xml server control, you obviously need an XML file. Listing 12.1 shows the products.xml file, which is made up of 10 products from the Northwind database.
Note
The products.xml file can be downloaded from the companion Web site (www.samspublishing.com).
01: <?xml version="1.0"?> 02: <NorthwindInfo> 03: <Products> 04: <ProductID>1</ProductID> 05: <ProductName>Chai</ProductName> 06: <SupplierID>1</SupplierID> 07: <CategoryID>1</CategoryID> 08: <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit> 09: <UnitPrice>18</UnitPrice> 10: <UnitsInStock>39</UnitsInStock> 11: <UnitsOnOrder>0</UnitsOnOrder> 12: <ReorderLevel>10</ReorderLevel> 13: <Discontinued>false</Discontinued> 14: </Products> 15: <Products> 16: <ProductID>2</ProductID> 17: <ProductName>Chang</ProductName> 18: <SupplierID>1</SupplierID> 19: <CategoryID>1</CategoryID> 20: <QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit> 21: <UnitPrice>19</UnitPrice> 22: <UnitsInStock>17</UnitsInStock> 23: <UnitsOnOrder>40</UnitsOnOrder> 24: <ReorderLevel>25</ReorderLevel> 25: <Discontinued>false</Discontinued> 26: </Products> 27: <Products> 28: <ProductID>3</ProductID> 29: <ProductName>Aniseed Syrup</ProductName> 30: <SupplierID>1</SupplierID> 31: <CategoryID>2</CategoryID> 32: <QuantityPerUnit>12 - 550 ml bottles</QuantityPerUnit> 33: <UnitPrice>10</UnitPrice> 34: <UnitsInStock>13</UnitsInStock> 35: <UnitsOnOrder>70</UnitsOnOrder> 36: <ReorderLevel>25</ReorderLevel> 37: <Discontinued>false</Discontinued> 38: </Products> 39: <Products> 40: <ProductID>4</ProductID> 41: <ProductName>Chef Anton's Cajun Seasoning</ProductName> 42: <SupplierID>2</SupplierID> 43: <CategoryID>2</CategoryID> 44: <QuantityPerUnit>48 - 6 oz jars</QuantityPerUnit> 45: <UnitPrice>22</UnitPrice> 46: <UnitsInStock>53</UnitsInStock> 47: <UnitsOnOrder>0</UnitsOnOrder> 48: <ReorderLevel>0</ReorderLevel> 49: <Discontinued>false</Discontinued> 50: </Products> 51: <Products> 52: <ProductID>5</ProductID> 53: <ProductName>Chef Anton's Gumbo Mix</ProductName> 54: <SupplierID>2</SupplierID> 55: <CategoryID>2</CategoryID> 56: <QuantityPerUnit>36 boxes</QuantityPerUnit> 57: <UnitPrice>21.35</UnitPrice> 58: <UnitsInStock>0</UnitsInStock> 59: <UnitsOnOrder>0</UnitsOnOrder> 60: <ReorderLevel>0</ReorderLevel> 61: <Discontinued>true</Discontinued> 62: </Products> 63: <Products> 64: <ProductID>6</ProductID> 65: <ProductName>Grandma's Boysenberry Spread</ProductName> 66: <SupplierID>3</SupplierID> 67: <CategoryID>2</CategoryID> 68: <QuantityPerUnit>12 - 8 oz jars</QuantityPerUnit> 69: <UnitPrice>25</UnitPrice> 70: <UnitsInStock>120</UnitsInStock> 71: <UnitsOnOrder>0</UnitsOnOrder> 72: <ReorderLevel>25</ReorderLevel> 73: <Discontinued>false</Discontinued> 74: </Products> 75: <Products> 76: <ProductID>7</ProductID> 77: <ProductName>Uncle Bob's Organic Dried Pears</ProductName> 78: <SupplierID>3</SupplierID> 79: <CategoryID>7</CategoryID> 80: <QuantityPerUnit>12 - 1 lb pkgs.</QuantityPerUnit> 81: <UnitPrice>30</UnitPrice> 82: <UnitsInStock>15</UnitsInStock> 83: <UnitsOnOrder>0</UnitsOnOrder> 84: <ReorderLevel>10</ReorderLevel> 85: <Discontinued>false</Discontinued> 86: </Products> 87: <Products> 88: <ProductID>8</ProductID> 89: <ProductName>Northwoods Cranberry Sauce</ProductName> 90: <SupplierID>3</SupplierID> 91: <CategoryID>2</CategoryID> 92: <QuantityPerUnit>12 - 12 oz jars</QuantityPerUnit> 93: <UnitPrice>40</UnitPrice> 94: <UnitsInStock>6</UnitsInStock> 95: <UnitsOnOrder>0</UnitsOnOrder> 96: <ReorderLevel>0</ReorderLevel> 97: <Discontinued>false</Discontinued> 98: </Products> 99: <Products> 100: <ProductID>9</ProductID> 101: <ProductName>Mishi Kobe Niku</ProductName> 102: <SupplierID>4</SupplierID> 103: <CategoryID>6</CategoryID> 104: <QuantityPerUnit>18 - 500 g pkgs.</QuantityPerUnit> 105: <UnitPrice>97</UnitPrice> 106: <UnitsInStock>29</UnitsInStock> 107: <UnitsOnOrder>0</UnitsOnOrder> 108: <ReorderLevel>0</ReorderLevel> 109: <Discontinued>true</Discontinued> 110: </Products> 111: <Products> 112: <ProductID>10</ProductID> 113: <ProductName>Ikura</ProductName> 114: <SupplierID>4</SupplierID> 115: <CategoryID>8</CategoryID> 116: <QuantityPerUnit>12 - 200 ml jars</QuantityPerUnit> 117: <UnitPrice>31</UnitPrice> 118: <UnitsInStock>31</UnitsInStock> 119: <UnitsOnOrder>0</UnitsOnOrder> 120: <ReorderLevel>0</ReorderLevel> 121: <Discontinued>false</Discontinued> 122: </Products> 123: </NorthwindInfo>
The Xml server control can be used to display raw XML data very easily. Simply placing the Xml server control in a Web Form, and setting a value for the DocumentSource property will suffice. This is seen in Listing 12.2.
Warning
Netscape Navigator 4.x and prior do not have the ability to render raw XML files. The example in Listing 12.2 will not work in older Netscape browsers.
[VB & C#] 01: <%@ Page ContentType="text/xml" %> 02: <asp:xml id="myxml" runat="server" DocumentSource="products.xml" />
Notice in Listing 12.2 that you set the @ Page directive's ContentType attribute to text/xml . This ensures the browser will read and display the XML data correctly. On line 2 of Listing 12.2, you use the Xml server control, and set the DocumentSource property to the products.xml file from Listing 12.1. (This example assumes both this Web Form and the products.xml file are in the same directory.) The browser display is seen in Figure 12.1.
Although interesting, the capability to render XML data in the browser is not new. After all, pointing a browser to the actual XML file would achieve the same result. What would be more interesting and useful would be to easily apply an XSL/T transform on the XML data, and render the data in a viewer-friendly format.
The Xml server control makes this simple. The TransformSource property allows you to specify the path to an .xsl stylesheet that will be applied to the XML data. Listing 12.3 shows the NorthwindInfo.xsl stylesheet.
Note
The NorthwindInfo.xsl file can be downloaded from the companion Web site (www.samspublishing.com).
01: <xsl:stylesheet version='1.0' 02: xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> 03: <xsl:template match="/"> 04: <style> 05: .title { font: bold 10pt Verdana, Arial; color: black;} 06: .description { font: 8pt Verdana, Arial;} 07: .header { font: bold 9pt Verdana, Arial; background-color:tan; color: black;} 08: .subheader { font: bold 8pt Arial; color: black;} 09: .value { font: 8pt Arial;} 10: </style> 11: <table border="0" cellspacing="0" cellpadding="4"> 12: <tr> 13: <th class="header">ID</th> 14: <th class="header">Product Name</th> 15: <th class="header">Qty/Unit</th> 16: <th class="header">Unit Price</th> 17: </tr> 18: <xsl:for-each select='NorthwindInfo/Products'> 19: <tr> 20: <td class="value" nowrap="true" align="center"> 21: <xsl:value-of select='ProductID'/></td> 22: <td class="value" nowrap="true"> 23: <xsl:value-of select='ProductName'/></td> 24: <td class="value" nowrap="true"> 25: <xsl:value-of select='QuantityPerUnit'/></td> 26: <td class="value" nowrap="true" align="right"> 27: $<xsl:value-of select='UnitPrice'/></td> 28: </tr> 29: </xsl:for-each> 30: </table> 31: </xsl:template> 32: </xsl:stylesheet>
The XSL stylesheet shown in Listing 12.3 uses the xsl:for-each function to loop through the XML data and apply the defined HTML styles to the output. Listing 12.4 shows the revised Web Form from Listing 12.2. Listing 12.4 uses the Xml server control with the TransformSource property to apply the XSL/T stylesheet to the products.xml data.
[VB & C#] 01: <html> 02: <body> 03: <asp:xml id="myxml" runat="server" DocumentSource="products.xml" 04: TransformSource="NorthwindInfo.xsl" /> 05: </body> 06: </html>
In Listing 12.4, you add the TransformSource property to the Xml server control, and specify the XSL/T stylesheet to apply to the XML data. This example assumes that the XSL/T stylesheet, the XML file, and the Web Form are all in the same directory. Figure 12.2 shows the rendered output from Listing 12.4.
only for RuBoard |