Tai lieu= Tieng anh

14 485 1
Tai lieu= Tieng anh

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

How XML can be used XML can keep data separated from your HTML HTML pages are used to display data Data is often stored inside HTML pages With XML this data can now be stored in a separate XML file This way you can concentrate on using HTML for formatting and display, and be sure that changes in the underlying data will not force changes to any of your HTML code XML can be used to store data inside HTML documents XML data can also be stored inside HTML pages as Data Islands You can still concentrate on using HTML for formatting and displaying the data XML can be used as a format to exchange information In the real world, computer systems and databases contain data in incompatible formats One of the most time consuming challenges for developers has been to exchange data between such systems over the Internet Converting the data to XML can greatly reduce this complexity and create data that can be read by different types of applications XML can be used to store data in files or in the databases Applications can be written to store and retrieve information from the store, and generic applications can be used to display the data XML Example Tove Jani Reminder Don't forget me this weekend!!! Line-by-line code Explanation The XML declaration should always be included It defines the XML version of the document In this case, the document conforms to the 1.0 specification of XML Defines the first element (the root element) of the document Tove Jani Reminder Don't forget me this weekend!!! Defines elements of the root (to, from, heading and body) The last line defines the end of the root element What's an XML doc looks like Let's save a piece of code above as note.xml (by the way, XML document should have xml as it's extension) and open it in the IE Below is what you actually see on the browser XML Syntax - General Idea All XML elements must have a closing tag In HTML some elements not have to have a closing tag The following code is legal in HTML:

This is a paragraph

This is another paragraph In XML all elements must have a closing tag like this:

This is a paragraph

This is another paragraph

XML tags are case sensitive XML tags are case sensitive Opening and closing tags must therefore be written with the same case This is incorrect This is correct Important: Tags should begin with either a letter, an underscore (_) or a colon ( followed by some combination of letters, numbers, periods (.), colons, underscores, or hyphens (-) but no white space, with the exception that no tags should begin with any form of "xml" It is also a good idea to not use colons as the first character in a tag name even if it is legal Using a colon first could be confusing Here are some legal and illegal tags examples: Legal tags Illegal tags All XML elements must be properly nested In HTML some elements can be improperly nested within each other like this: This text is bold and italic In XML, all elements must be properly nested within each other like this: This text is bold and italic All XML documents must have a root tag All XML documents must contain a single tag pair to define the root element All other elements must be nested within the root element All elements can have sub (children) elements Sub elements must be in pairs and correctly nested within their parent element eg Attribute values must always be quoted XML elements can have attributes in name/value pairs just like in HTML In XML the attribute value must always be quoted eg Correct Incorrect Avoid using attributes? Attributes are handy in HTML But in XML you should try to avoid them (you could easily substitute attributes by elements - I will show you later so you could get the idea!!!) Why? Below are some of the problems using attributes Attributescan not contain multiple values Attribute are not expandable Attribute are more difficult to manipulate by program code Attribute values are not easy to test against DTD Let me clear up your doubt by looking at the following example: An XML example 12/11/00 Tove Jani> Reminder Don't forget me this weekend If you look at the element above, how you interpret it??? Is this 12 of November or 11 of December??? Now, let see how you can expand the element: 12 11 99 Tove Jani Reminder Don't forget me this weekend Got the idea??? XML Well-formed If you have read the XML Syntax - General Idea section above, by now you should have a very fair idea about XML in general So, I am going to move on to more interesting topic that is XML well-formed XML Documents consider well formed should satisfy three simple rules: The document must contain one or more elements It must contain a uniquely name element, no part of which appears in the content of any other element, known as the root element All other elements within the root element must be correctly nested So, according to these rules, the following are examples of well formed documents: example1.xml example2.xml Mammalia example3.xml Mammalia example4.xml Note: example1.xml and example4.xml are the same The following is example of not well formed documents: bad_example.xml Some text info Explanation: If you look carefully, you can see that the element overshoots the end of the element, which should encapsulate the element completely (According to rule above) XML doc structure Physically, documents are composed of a set of entities (we will talk about this topic in a bit) that are identified by unique names All documents begin with a root or document entity All other entities are optional As opposed to physical structure, XML documents have a logical structure as well Logically, documents are composed of declarations, elements, comments, character references and processing instructions, all of which are indicated in the document by explicit markup Data vs Markup All XML documents may be understood in terms of the data they contain and the markup that describe that data Data is typically "character data" (i.e anything within the boundaries of valid Unicode such as letters, numbers, punctuation and so on) but can also be binary data as well Markup includes tags, comments, processing instructions, DTDs and references and so forth For example: John Smith Explanation: and tags comprise the markup and "John Smith" comprises the character data XML Declaration To begin an XML document, it is a good idea to include the XML declaration at the very first line of the document Though the XML declaration is optional, but the W3C specification (World Wide Web Consortium - the group developed XML) suggests that we should include it to indicate the version of XML, used to construct the document so that an appropriate parser or parsing process can be matched to the document Essentially, the XML declaration is a processing instruction that notifies the processing agent (browser) that the following document has been marked up as an XML document It will look something like the following: OR having a white space in between as shown below We will talk more about the gory details of processing instructions later, for now we concentrate on explaining how the XML declaration works okie! All processing instructions, including the XML declaration should have the following syntax: It must begin with Following the initial The Version Attribute As we have mentioned before, if you decide to use the optional XML declaration, you must define the version attribute As of this writing, the current version of XML is 1.0 If you include the optional attributes, version must be specified first The STANDALONE Attribute The standalone attribute specifies whether the document has any markup declarations that are defined in a separate document Thus, if standalone is set to "yes", the document is effectively self-contained and there are no extra markup declarations in external DTD's However, setting the standalone to "no" leaves the issue open Remember that the document may or may not access external DTD's For examples: standalone_yes.xml Professional XML Design and Implementation Paul Spencer Wrox $83.95 standalone_no.xml Professional XML Design and Implementation Paul Spencer Wrox $83.95 Note: As you can see, if standalone="no" which means the XML document should use an external DTD In this case, use book.dtd file to check for validating document The ENCODING Attribute All XML parsers must support 8-bit and 16-bit Unicode encoding (UTF-8 and UTF-16 respectively) corresponding to ASCII However, XML parsers may support a larger set Character Data XML defines the text between the start and end tags to be character data and the text within the tags to be markup Since the "" are the reserved characters for the start and end of a tag respectively Thus character data may be any legal (Unicode) character except the "" can not be used The following example is incorrect 12 < 13 Alternative solution: 12 < 13 Here is the question you might ask yourself How am I supposed to know which characters that legal or illegal to use? Well, not too worries - XML provides a couple of useful entity references that you can use: Character Entity Reference Meaning > > Greater than < < Less than & & Ampersand " " Double quote ' ' Apostrophe (Single quote) Obviously, the < entity reference is useful for character data The other entity references can be used within markup in cases in which there could be confusion such as: DOCTYPE Declarations If you want to declare entities, you must so within the document DOCTYPE declaration that always follows the prolog (DTD and xml Declaration) and looks like the following: here is the body of your document Thus, you might have something like the following: ]> Willy Smith &nintha_floor_address; x345 Jet Li &ninth_floor_address; x111 John Drayton &ninth_floor_ddress; x346 David Drinkwater &seventh_floor_address; x289 Kristin Kelly &eighth_floor_address; x945 Consider how much easier changing office addresses is when you use entities Entity References Entity Reference refers to two types: Entity reference to General Entities and Parameter Entities Let's us cover them one by one Entity Reference - Referring to General Entities Well we have pretty much let the cat out of the bag already We have shown several examples of entity references above Let's us take even a closer look of what we have covered before! oh! I am trying to make it short and simple, okay! So what is entity reference? Entity References refer to the key that unlocks an entity which has been declared in an Entity Declaration Entity References follow the simple syntax of: &entity_name; such as &letterhead; Entities must be declared in an XML document before they are referenced Should not have any whitespace embedded in an entity reference In other words, & letterhead; or &letterhead ; will cause errors Though entities may refer to other entities, they may not be self-referential either directly or indirectly So the following declarations are not allowed Example1 Explanation Entity contains references to itself (direct) Example2 Explanation Entities contains reference to themselves (indirect) But entities can refer to other entities as long as there is no direct or indirect selfreference of the type above So we can have: If we then used the reference &who; within the content of an element, the entity references would be expanded by the parser, and the replacement text formed to give "The cat sat on the mat." References to entities may not appear in the DOCTYPE declaration The text that the entity references must be well-formed XML Entity Reference - Referring to Parameter Entities As you might expect parameter entity references work much like general entity references In this case, we use a "%" sign instead of a "&" %parameter_entity_name; Before we leave the subject, I would only mention that you could also use entity references within tag attributes For example, consider the following: You may not reference an external entity from within element attributes The referenced text may not contain the < character because it would cause a well-formed error in the element when replaced ... consider well formed should satisfy three simple rules: The document must contain one or more elements It must contain a uniquely name element, no part of which appears in the content of any... "Talk about the &net;"> Entity contains references to itself (direct) Example2 Explanation Entities contains reference to themselves... text is bold and italic All XML documents must have a root tag All XML documents must contain a single tag pair to define the root element All other elements must be nested within the root

Ngày đăng: 12/01/2013, 15:43

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan