1 / 62

Comprehensive Guide to XML Technologies in ASP.NET

Explore the essentials of XML and XSLT technologies in ASP.NET, covering XML editing tools, well-formed XML documents, and XSLT stylesheets. Learn to create, edit, and format XML files effectively. Includes hands-on practice and detailed insights.

Download Presentation

Comprehensive Guide to XML Technologies in ASP.NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter Objectives Introduction to ASP.NET, Second Edition

  2. XML Technologies • Extensible Markup Language (XML) • Like HTML, is a markup language that provides access to structured content • Is not limited to a set of tags or to being displayed in a browser • Allows you to define your own elements • Can be shared across applications, file systems, and operating systems • XML standards maintained by the W3C Introduction to ASP.NET, Second Edition

  3. XML Technologies (continued) • XSL Transformations (XSLT) • Is a language for transforming XML documents • XSLT stylesheets • Stylesheets formats XML documents • Use XSLT to interact with an XML document • Can format individual or groups of elements within the XML document • A form of XSL (Extensible Style Language) that transforms the XML data using processing rules in the XSLT stylesheet Introduction to ASP.NET, Second Edition

  4. XML Technologies (continued) Introduction to ASP.NET, Second Edition

  5. XML Editing Tools • View XML documents using an XML parser • Built into Internet Explorer and Office 2003 • Create the XML documents using: • Simple text editor such as Notepad • Visual Studio .NET provides HTML/XML editor • Microsoft XML NotePad called xmlpad.exe • SnapFiles – Updated URL • http://www.snapfiles.com/get/xmlnotepad.html • File extensions • Generally saved with the file extension .xml Introduction to ASP.NET, Second Edition

  6. Well-Formed XML Documents • An XML document must be well-formed • Follows XML standards • One root element - all other elements are nested within • You cannot mix nesting elements <b>Welcome to <i>Tara Store</b></i> <b>Welcome to <i>Tara Store</i></b> • Enclose the values within “ ” • Case sensitive; opening and closing tags must match Introduction to ASP.NET, Second Edition

  7. The Prologue • The first section <?xml version="1.0" encoding="utf-8" ?> • XML version, formatting information, and schema definitions • ? indicates this is a processing instruction and does not contain data • Character-encoding property describes any coding algorithms Introduction to ASP.NET, Second Edition

  8. The Prologue (continued) • Optional – add a reference to a CSS stylesheet or an XSL file to format the XML document <?xml-stylesheet type="text/css" href="taragifts.css" ?> <?xml-stylesheet type="text/xsl" href="taragifts.xsl" ?> Introduction to ASP.NET, Second Edition

  9. The XML Body • Comply with the XML DOM standards • XML documents must have a logical structure • Root element (node) must nest all the elements and data • Root node can contain many other elements • All tags must be nested within the root node • Root node is a container element Introduction to ASP.NET, Second Edition

  10. The XML Body (continued) • productlist is the root node; many product nodes <productlist> <product> <code>387-463-55-00</code> <name>Waterford Crystal Shamrock Paperweight</name> <price>99.00</price> <category>Waterford</category> <image>547.gif</image> <rating>4</rating> </product> … </productlist> Introduction to ASP.NET, Second Edition

  11. Modifying an XML File with the XML Designer products.xml (Page 146) • Edit XML data • Manually in XML view, or in Data view • XML view supports IntelliSense and color coding • Create a Chapter4 project • Create an images folder, import the project images, and import all the files • products.xml file contains eight products Introduction to ASP.NET, Second Edition

  12. products.xml (continued) Introduction to ASP.NET, Second Edition

  13. products.xml (continued) Introduction to ASP.NET, Second Edition

  14. Using Special Characters in an XML Document • &nbsp; represents a non-breaking space in HTML • Certain character entities are not supported within the tags because they are used to separate XML elements • Replace these characters with markup codes Introduction to ASP.NET, Second Edition

  15. XSLT Stylesheets • Contains format information • Can contain HTML, style rules, XSTL commands • First line identifies the version of XML • Root node indicates it is an XSTL stylesheet • root node name is xsl:stylesheet • Comments use <! - - and - - > tags • Usual file extension is .xsl Introduction to ASP.NET, Second Edition

  16. XSLT Stylesheets (continued) • xmlns:xsl attribute indicates the schema • Schema or rules are XSL Transform standards which are maintained by the W3C <xsl:stylesheet version="1.0" xmlns:xsl= "http://www.w3.org/1999/XSL/Transform"> <!- - Put your formatting code here - - > </xsl:stylesheet> Introduction to ASP.NET, Second Edition

  17. Formatting the Main Template • Indicated by the match attribute • Must match the root node • Indicates where to start formatting <xsl:template match="/"> </xsl:template> • Do not have to format all of the elements • Can nest basic HTML tags • CSS classes can format XML elements • xsl:for-each is a processing instruction Introduction to ASP.NET, Second Edition

  18. Formatting the Main Template (continued) • Displays product name: category <br /> <xsl:template match="/"> <html><head><title>Tara Store Product List</title> ...</head> <body> <H1>Products and their categories.</H1> <xsl:for-each select="//product"> <xsl:apply-templates select="name" />: <xsl:apply-templates select="category" /> <br /> </xsl:for-each> </body></html></xsl:template> Introduction to ASP.NET, Second Edition

  19. Formatting the Elements Using Element Templates • Template is outside of the main template • Define individual templates for individual elements • The match attribute indicates the element name • xsl:value-of retrieves and displays the value • <xsl:value-of select="." /> displays the contents of any node • Period means everything within the node is selected <xsl:template match="product"> <b><xsl:value-of select="." /> </b> </xsl:template> Introduction to ASP.NET, Second Edition

  20. Using Element Templates (continued) • If you want a template to apply to all the other elements, you can use an asterisk as the value for the match property <xsl:template match="*"> <div class="product"> <xsl:value-of select="."/> </div> </xsl:template> Introduction to ASP.NET, Second Edition

  21. Using Element Templates (continued) • The DIV tag uses the class defined in the embedded style sheet to determine how to format the output <xsl:template match="name"> <div class="product"> <b>Product Name: <xsl:value-of select="." /> </b><br /> </div> </xsl:template> Introduction to ASP.NET, Second Edition

  22. Creating an XSLT Stylesheetlistproducts.xsl (Page 153) Introduction to ASP.NET, Second Edition

  23. Using the XML Control • DocumentSource - identify a physical or virtual path to the XML document • TransformSource - identifies the physical or virtual path to the XSL or XSLT stylesheet <asp:Xml runat="server" id="Xml1" TransformSource="listproducts.xsl" DocumentSource="products.xml" /> Introduction to ASP.NET, Second Edition

  24. Inserting an XML Control in a Web Page listproducts.aspx (Page 157) Introduction to ASP.NET, Second Edition

  25. listproducts.aspx (continued) Introduction to ASP.NET, Second Edition

  26. Modifying XSLT Stylesheets • Specify HTML attributes with xsl:attribute • Create a hyperlink <xsl:template match="image"> <a> <xsl:attribute name="href"> <xsl:value-of select="." /> </xsl:attribute> Click here to go to the Web site. </a> </xsl:template> Introduction to ASP.NET, Second Edition

  27. Inserting a Table with an XSLT Stylesheet • Table tags outside the for-each loop <xsl:template match="/"> <table border="0" cellspacing="10"> <tr><th>Image</th> ... </tr> <xsl:for-each select="//product"> <tr> <td><xsl:apply-templates select="image"/></td> ... </tr> </xsl:for-each> </table> </xsl:template> Introduction to ASP.NET, Second Edition

  28. Inserting an Image with an XSLT Stylesheet <xsl:template match="image"> <img> <xsl:attribute name="src"> <xsl:value-of select="." /> </xsl:attribute> <xsl:attribute name="align"> left </xsl:attribute> </img> </xsl:template> Introduction to ASP.NET, Second Edition

  29. Processing XML Data with an XSLT Stylesheet • XSLT stylesheets can analyze the contents of the element and performs actions • xsl:choose - analyzes the value of the rating • xsl:when – if the condition in the test attribute is met • xsl:otherwise - applies when no choice listed is met • xsl:sort – sortS the data • xsl:if statement - similar to the xsl:if-else-end if • string-length - tests the length of the element Introduction to ASP.NET, Second Edition

  30. Processing XML Data with an XSLT Stylesheet <xsl:template match="rating"> ... <xsl:choose> <xsl:when test=". &gt; 3"> <span class="over"> <img src="clover.gif" align="bottom" hspace="5"/> Excellent! </span> </xsl:when> ... <xsl:otherwise> This is within current projections.<br /> </xsl:otherwise> </xsl:choose> ...</xsl:template> Introduction to ASP.NET, Second Edition

  31. XML Schemas • Schema • Set of rules or namespace • Used to define the structure, content, and semantics of XML documents • Can be written in a Document Type Definition (DTD) document or an XML Schema document • Parsers use the schema • To determine the rules to validate the XML data • Validate the structure of the XML document Introduction to ASP.NET, Second Edition

  32. XML Schemas (continued) • Schema property in an ASP.NET page used is called TargetSchema • DTD for Web pages strict HTML 4.0 standards. • First line before the <html> tag <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> Introduction to ASP.NET, Second Edition

  33. Validating an XML Document with a Schemainventory.xsd (Page 164) • Create a schema for inventory.xml • File extension is .xsd • DataSet view displays tables, • XML view displays the XML code • The xmlns attribute identifies the XML schema <elementname xmlns="http://tempuri.org/schemaname.xsd"> Introduction to ASP.NET, Second Edition

  34. inventory.xsd (continued) Introduction to ASP.NET, Second Edition

  35. inventory.xsd (continued) Introduction to ASP.NET, Second Edition

  36. The AdRotator Control • Display a banner ad on a Web page • banner ad is a hyperlinked image • Advertisement File • XML document & stores the ad information • File extension .xml • First line indicates the version of the XML • Root node named advertisements • Each ad element contains the properties to create the image and hyperlink Introduction to ASP.NET, Second Edition

  37. The AdRotator Control (continued) • Property names are case sensitive • ImageUrl - create the SRC property • NavigateUrl - create the href property • AlternateText - create the ALT property • Impressions - indicate the relative frequency the banner ad is displayed. • Keyword property - indicate one or more words that categorize the banner ad • KeywordFilter method – retrieves ads which match the Keyword Introduction to ASP.NET, Second Edition

  38. Creating the Advertisement Fileads.xml (Page 169) Introduction to ASP.NET, Second Edition

  39. ads.xml (continued) Introduction to ASP.NET, Second Edition

  40. Inserting the AdRotator Control • AdvertisementFile property - Advertisement File URL • Additional Properties • Height, Width, Style properties – change appearance • ToolTip property - stores a message to display the message when the user mouses over image • Target property - window to open the link. Default is _top • KeywordFilter property - retrieves ads where the Keyword matches exactly Introduction to ASP.NET, Second Edition

  41. Creating the Web Page to Display the AdRotator (Page 173) • getproducts.aspx - retrieves the value from the URL and displays the image Dim imagename As String imagename = Page.Request.QueryString("ID") Image1.ImageUrl = "images/" & imagename • AdRotator.aspx – contains the banner ad • KeywordFilter to Waterford • AdvertisementFile property to ads.xml Introduction to ASP.NET, Second Edition

  42. displayBanner.aspx Introduction to ASP.NET, Second Edition

  43. The Calendar Control • Displays a single calendar month • Change the appearance by setting style templates and properties • Can also assign a CSS class to the style property <asp:calendar id="MyCal" runat="server" /> Introduction to ASP.NET, Second Edition

  44. The Calendar Control (continued) • Some of the Style properties • DayHeaderStyle– sets style for days of the week • DayStyle– sets the style for individual dates • NextPrevStyle– sets style for navigation controls • OtherMonthDayStyle–for dates not in the current month • SelectedDateStyle– sets style for dates selected • SelectorStyle– sets style for month date selection column • TitleStyle sets the style for the title in the heading • TodayDayStyle – sets style for current date • WeekendDayStyle – sets style for weekend dates Introduction to ASP.NET, Second Edition

  45. The Calendar Control (continued) • Some of the Calendar properties • ShowDayHeader – shows/hides days of the week • ShowGridLines – shows/hides the gridlines • ShowTitle – shows/hides the heading title • ShowNextPrev – shows/hides the Navigation controls Introduction to ASP.NET, Second Edition

  46. The Calendar Control (continued) • Programs can interact with the calendar • SelectionChanged event occurs when the user clicks a new date - changes the selected date to a new selected date • SelectedDate property is the new selected date. The calendar control visually indicates to the user which date is selected • ToShortDateString - short date format as mm/dd/yyyy • VisibleMonthChanged event occurs when the user clicks on the next or previous month hyperlinks Introduction to ASP.NET, Second Edition

  47. Setting the Calendar Control Properties calendar.aspx (Page 177) Introduction to ASP.NET, Second Edition

  48. calendar.aspx (continued) • Calendar1_SelectionChanged • compares dates and displays a message Label1.Text = "You selected: " & _ Calendar1.SelectedDate.ToShortDateString() If Calendar1.SelectedDate.ToShortDateString _ = Date.Now.ToShortDateString Then Label2.Text = _ "Today all Waterford products are 30% off." Else Label2.Text = _ "All products are 10% off during this month" End If Introduction to ASP.NET, Second Edition

  49. Working with Multiple Dates • SelectedDates object –to retrieve the multiple values selected; use a For-Next loop to iterate through each date that was selected • Count property - number of dates selected • Index position – position (starts at 0) MyCalendar.SelectedDates(i) Introduction to ASP.NET, Second Edition

More Related