Recent Posts

Sunday, 4 September 2016

XML Entity References

Entity References
1. Some characters have a special meaning in XML.
2. If you place a character l ~ k e"< " inside an XML element, it will generate an error because the parser interprets it as the start of a new element.
3. This will generate an XML error
<message>if salary < 1000 then</message>
4. To avoid this error, replace the "<" character with an entity reference
<messaqe>if salary &lt; 1000 then</message>
     There are 5 predefined entity references in XML

     Only the characters "<" and "&" are strictly illegal in XML. The greater than character is legal, but it is a good habit to replace it.
Comments in XML
     Thc syntax For writing comments in XML is similar to that of HTML.
<!-- This is a comment -->
White-space is Preserved in XML
     HTML truncates multiple white-space characters to one single white-space.
HTML    Hello    Ashok..!!
Output  Hello Ashok..!!
     With XML, the white-space in a document is not truncated.
What is a well-formed XML document?
     If an XML document confirms the syntactical rules (above specified) of XML specification is said to be well formed. An XML document is said to be well-formed if it observes the following rules.
1. It must begin with the XML declaration
2. I t rrlust have one unique root element
3. Start-tags must have matching end-tags
4. Elements are case sensitive
5. All elements must be closed
6. All elements must be properly nested
7. All attribute values must be quoted
8. Entities must be used for special characters

What is an XML Element?
     An XML element is everything from (including) the ele'ment's start tag to (including) the element's end tag. An element can contain
1. other elements
2. text
3. attributes
4. or a mix of all of the above

What are the XML Naming Rules?
     XML elerncnts must follow these naming rules
1. Names can contain letters, numbers, and other characters
2. Names cannot start with a number or punctuation character
3. Names cannot start with the letters xml (or XML, or Xml, etc)
4. Names cannot contain spaces
     Any name can be used, no words are reserved.

What are the Best Naming Practices?
1. Make names descriptive. Names with an underscore separator are nice: <first-name>, <last-name>.
2. Narrles should be short and simple, like this: <book-title> not like this: <the-title-of-the-book>.
3. Avoid "-" characters. If you name something "first-name," some software may think you want to subtract name from first.
4. Avoid "." characters. If you name something "," some software may think that "name" is a property of the object "first."
5. Avoid ":" characters. Colons are reserved to be used for something called namespaces.

XML Attributes
1. XML elements can have attributes, just like HTIVIL.
2. Attributes provide additional information about an element.
3. Attributes can occur in any order in an XML element.
4. Attribute is unique in an XML element. i.e. It should not be repeated.
<x a="10" a="20" /> (wrong)
XML Attributes Must be Quoted
     Attribute values must always be quoted. Either single or double quotes can be used. For a person's gender, the person element can be written like this
<person gender="male">
or like this
<person gender="male">
     If the attribute value itself contains double quotes you can use single quotes, like in this example
<gangster name='George "Shotgun" Ziegler'>
     Or you can use character entities:
<gangster name='George &quot;Shotgun&quot; Ziegler'>
Use of Elements vs. Attributes
     Take a look at these examples
<person gender="male">
   <lastname>Ashok Kumar</lastname>


   <lastname>Ashok Kumar</lastname>
     In the first example gender is an attribute. I n the last, gender is an element. Both examples provide the same inforrnation.
    There are no rules about when to use attributes or when to use elements. Attributes are handy in HTML. In XML my advice is to avoid them. Use elements instead.

Avoid using attributes? (I say yes!)
     These are some of the problems using attributes
1. attributes cannot contain multiple values (elements can)
2. attributes are not expandable (for future changes)
3. attributes cannot describe structures (like child elements can)
4. altributes are more difficult to manipulate by program code
5. attribute values are not easy to test against a DTD/XSD

XML Attributes for Metadata
     Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements in much the same way as the id attribute in HTML. This example demonstrates this.
<note id="501">
   <body>Don't forgot me this weekend.!!</body>
<note id="502">
   <heading>Re : Reminder</heading>
   <body>I will not</body>
     The id attributes above are for identifying the different notes. It is not a part of the note itself. What I'm trying to say here is that metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.

XML Documents Form a Tree Structure
     The elements in an XML document form a document tree. All elements can have sub elements (child elements).
     The terms parent, child, and sibling are used to describe the relationships between elements. Parent elements have , children. Children on the same level are called siblings (brothers or sisters). All elements can have text content and attributes (just like in HTML).

The imagc above represents one book in the XML below
<?xml version="1.0" encoding="UTF-8"?>
   <book category="cooking">
      <title lang="en">Everyday Italian</title>
      <author>Giada De Laurentiis</author>
   <book category="children">
      <title lang="en">Harry Potter</title>
      <author>J K. Rowling</author>
   <book category="web">
      <title lang="en">Learning XML</title>
      <author>Erik T. Ray</author>

Next Tutorial  XML Parser Tutorial

Previous Tutorial  XML Basic Tutorial

No comments:

Post a Comment