SPECIAL TOPIC XML. Introducing XML XML (eXtensible Markup Language) ◦A language used to create...
-
Upload
oscar-tenney -
Category
Documents
-
view
269 -
download
0
Transcript of SPECIAL TOPIC XML. Introducing XML XML (eXtensible Markup Language) ◦A language used to create...
SPECIAL TOPICXML
Introducing XML XML (eXtensible Markup Language)
◦ A language used to create structured documents
XML vs HTML◦ XML is designed to transport and store data (carry)◦ HTML was designed to display data (show)◦ Shares common structure with HTML documents
◦ Elements are indicated with markup tags that contain textual content; element names are descriptive
◦ A markup tag can contain attributes that describe a feature of the element, and a single root element contains all other elements in the document
Introducing XML XML does not DO anything. XML was created to structure, store, and transport information.
The following example is a note to Tove, from Jani, stored as XML<note>
< to>Tove</to>< from>Jani</from>< heading>Reminder</heading>< body>Don't forget me this weekend!</body>
< /note>
XML Tree Structure With XML You Invent Your Own Tags
◦ the XML language has no predefined tags
XML documents form a tree structure that starts at "the root" and branches to "the leaves".
<root><child>
<subchild>...
Introducing XML
XML declarationIt defines the XML version (1.0) and the encoding used (UTF-8).
Single root element (students)
Student, name, and photo elements and the id and grade attributes provide information about individual students
Syntax Ruleso Must have closing tags
o Case sensitive tags
o Proper tag nesting
o Must have a Root element
o Attributes must be quoted
o Uses HTML comments
o Special symbols > < & ‘ “o Use entity references: < >
Syntax Rules Comments are similar to HTML comment
<!-- comment -->
White-space is Preserved in XML
Naming rules◦ XML elements must follow these naming rules:◦ Names can contain letters, numbers, and other characters◦ Names cannot start with a number or punctuation character◦ Names cannot start with the letters xml (or XML, or Xml, etc)◦ Names cannot contain spaces◦ Any name can be used, no words are reserved
XML Elements An XML element is everything from (including) the element's start tag to (including) the element's end tag
An element can contain:◦ other elements◦ text◦ attributes◦ or a mix of all of the above
<bookstore>< book category="CHILDREN">
<title>Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price>
< /book>< /bookstore>
XML attributes XML elements can have attributes.
◦ Attributes provide additional information about an element.<file type="gif">computer.gif</file>
XML Elements vs. Attributes
Take a look at these examples:
< person sex="female"><firstname>Anna</firstname><lastname>Smith</lastname>
< /person>
< person><sex>female</sex><firstname>Anna</firstname><lastname>Smith</lastname>
< /person>
Attributes vs. Elements Some of the problems with using attributes are:
◦ attributes cannot contain multiple values (elements can)◦ attributes cannot contain tree structures (elements can)◦ attributes are not easily expandable (for future changes)
Attributes are difficult to read and maintain ◦ Use elements for data, and attributes for information that is not
relevant to the data
Namespaces XML Namespaces provide a method to avoid element name conflicts
Defined by the xmlns attribute
Tags that are not HTML tags have the prefix xsl, identified by the namespace xmlns:xsl="http://www.w3.org/1999/XSL/Transform":
Name conflicts in XML can easily be avoided using a name prefixxmlns:h="http://www.w3schools.com/html4">
xmlns:f="http://www.w3schools.com/furniture"><h:table><f:table>
eXtensible Style sheet Language Translation.XSLT
Styles Can use a CSS style sheet to format an XML document
<?xml-stylesheet type="text/css" href=“style.css"?>
Better approach is to use XSL (eXtensible Stylesheet Language)◦ can transform an XML document into HTML, using◦ XSLT (eXtensible Stylesheet Language Transformation)
<?xml-stylesheet type="text/xsl" href=“style.xsl"?>
Transforming XML with XSLT
Extensible Stylesheet Language Transformation (XSLT)◦ A style language developed for XML documents◦ Allows developers to easily transform contents of an XML
document into another document format◦ A more efficient approach to transforming an XML
document into HTML format than working with the document object model
The <xsl:template> Element
The match attribute is used to associate a template with an XML element
<xsl:template match="/">
match="/" indicates the stylesheet template applies to the entire XML file
<?xml version="1.0" encoding="ISO-8859-1"?>< xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
< xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td>.</td> <td>.</td> </tr> </table> </body> </html> < /xsl:template>< /xsl:stylesheet>
<xsl:value-of> Used to extract the value of an XML element and add to output.
<?xml version="1.0" encoding="ISO-8859-1"?>< xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
< xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td> <xsl:value-of select="catalog/cd/title"/></td> <td> <xsl:value-of select="catalog/cd/artist"/></td> </tr></table></body></html>< /xsl:template>
< /xsl:stylesheet>
The <xsl:for-each> Element
Allows you to do looping
<?xml version="1.0" encoding="ISO-8859-1"?>< xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> < xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td> <xsl:value-of select="title"/> </td> <td> <xsl:value-of select="artist"/> </td> </tr> </xsl:for-each> </table> </body> </html>< /xsl:template>< /xsl:stylesheet>
XSLT <xsl:sort> Element
used to sort the output
<?xml version="1.0" encoding="ISO-8859-1"?>< xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">< xsl:template match="/"><html><body><h2>My CD Collection</h2><table border="1"><tr bgcolor="#9acd32"><th>Title</th><th>Artist</th></tr><xsl:for-each select="catalog/cd"><xsl:sort select="artist"/><tr><td> <xsl:value-of select="title"/> </td><td> <xsl:value-of select="artist"/> </td></tr></xsl:for-each></table></body></html>< /xsl:template>< /xsl:stylesheet>
XSLT <xsl:if> Element
used to put a conditional test against the content of the XML file
<?xml version="1.0" encoding="ISO-8859-1"?>< xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">< xsl:template match="/"><html><body><h2>My CD Collection</h2><table border="1"><tr bgcolor="#9acd32"><th>Title</th><th>Artist</th></tr><xsl:for-each select="catalog/cd"> <xsl:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if></xsl:for-each></table></body></html>< /xsl:template>< /xsl:stylesheet>