Você está na página 1de 9

What are XML Elements and Attributes?

Naming rules for Element Tags and Attribute Names Element tag names and Attribute names can begin with a letter, a colon, or an underscore, but NOT with numbers. Subsequent characters may include alphanumeric, underscores, hyphens, and colons. Names cannot contain certain XML-specific symbols like the ampersand (&), the "at" symbol (@), or the "less-than" symbol (<). Element tag names and Attribute names cannot contain white space. Names cannot contain parenthetic statements ( words enclosed in parentheses or brackets ). Which has a better usage, element or attribute ? The classic XML design question, asked by adopters is when to use elements and attributes in XML design. Elements are used to encapsulate pieces of data, and attributes are generally used to provide accompanying information about an element. So, given below are the benefits and disadvantages of both elements and attributes and these can be used as a guiding principle for using elements and attributes : Benefits of Using Elements They are more extensible because attributes can later be added to them without affecting a processing application. They can contain other elements. They can be repeated. You have more control over the rules of their appearance. For example, you can say that a product can either have a number or a product code child. This is not possible for attributes. Their order is significant if specified as part of a sequence, while the order of attributes is not. Obviously, this is only an advantage if you care about the order. When the values are lengthy, elements tend to be more readable than attributes. Disadvantages of Using Elements Elements require start and end tags, so are therefore more verbose. Note Not all elements require a start and end tag - elements can be declared in a single line. Benefits of Using Attributes They are less verbose. Attributes can be added to the instance by specifying default values. Attributes are atomic and cannot be extended and its existence should serve to remove any and all possible ambiguity of the element it describes. Disadvantages of Using Attributes Attributes may not be extended by adding children, whereas a complex element may be extended by adding additional child elements. If attributes are to be used in addition to elements for conveying business data, rules are required for specifying when a specific data item shall be an element or an attribute. ---------------------------------------------------------------------------

XML Tutorial - What are XML Schema and a DTD?


Document Type Definition The SGML formalizes the concept of a document type and provides for a separate file called a Document Type Definition (DTD), which identifies all of the elements in its respective document, and indicates the structural relationships among them.

So, unlike its predecessor SGML, XML does not absolutely require a Document Type Declaration (also known as DOCTYPE definition) in all circumstances. The DTD is a file that contains the necessary rules that the XML code in the file must follow. A DTD describes a model of the structure of the content of an XML document. This model says what elements must be present, which ones are optional what their attributes are, and how they can be structured with relation to each other. While HTML has only one DTD, XML allows you to create your own DTD's for your applications. This gives you complete control over the process of checking the content and structure of the XML documents created for that application. This checking process is called validation. The syntax of a DTD is like : Syntax < !DOCTYPE DTDname [options] > DTD name is the name of the DTD. The DTD name should be same as the root element of the document. The "options" include other specifications, for example, an indication of where DTD or schemas are located, their own types, etc. A DTD can be declared inline in your XML document, or as an external reference. Internal DTDs (also known as internal subset) If the DTD is included in your XML source file, it should be wrapped in a DOCTYPE definition, with the following syntax : Syntax < !DOCTYPE Rootname [element declaration] > With all the declarations in the internal DTD subset, the XML processor would not need to read and process external documents. When you start off the XML document the first line in the XML declaration, which can include a standalone document declaration: < ?xml version="1.0" standalone = "yes"? > The statement standalone = "yes" means that there are no mark up declarations external to the document entity. The keyword DOCTYPE must be in uppercase. Example <?xml version="1.0"? > <!DOCTYPE Vehicle [ <!ELEMENT Vehicle (two-wheeler, three-wheeler, four-wheeler)> <!ELEMENT two-wheeler (#PCDATA)> <!ELEMENT three-wheeler (#PCDATA)> <!ELEMENT four-wheeler (#PCDATA)> ]> <Vehicle> <two-wheeler>Bi-cycle</two-wheeler> <three-wheeler>Auto-rickshaw</three-wheeler> <four-wheeler>Car</four-wheeler> </Vehicle> External DTDs (also known as external subset) The DTD portion of the document doesn't always have to be stored inside the related XML document. Instead, it can be saved in a file for reference by one document or by several different documents. Syntax < !DOCTYPE Root element System "filename" > External DTD is intended for use with more than one XML document. For External DTD, the "standalone" is set to "no" in the XML version statement, which indicates that an external DTD must be processed as well as all internal declarations. Example A simple XML document : <?xml version = "1.0" ? >

<page> <head> <title> Fruits </title> </head> <body> <title> Select your favorite fruits </title> </body> </page> Developing a DTD from XML code : <!DOCTYPE page [ <!ELEMENT page ( head, body ) > <!ELEMENT head (title) > <!ELEMENT body (title) > ] Although DTDs are valuable and effective tools used in defining document types, the DTD has several drawbacks DTDs have their own syntax, which differs from a true XML, which means that a DTD cannot be processed with a standard XML parser. It would be better, and make learning much easier, if the tools used to process XML documents could also be used to process their document models. DTDs have limited ability to describe the data in elements and attributes. For example, you can't indicate when character data should be numbers, date-format, or currency. DTDs have limited support for namespaces, so they can't define or restrict the content of elements based on context sensitivity. ( Namespace declarations, which are special attributes, prevent name collisions. However, they require the insertion of appropriate declarations into the respective DTDs ). Therefore, a movement has developed to create a different system of writing the prototypes of XML-Data. It's a language used to create schemas, which are descriptions of the data in an XML file. The important thing about schemas is that the description of the data can be written in XML. ----------------------------------------------------------------

What is XML Schema?


A schema is a definition of the syntax of an XML based language, i.e., it defines a class of XML document. A schema language is a formal language for expressing schemas. They are composed of declarations for concepts and classes of objects with class hierarchies, properties, constraints, and relationships. Like a DTD, a schema is a model for describing the structure and content of data. But XML Schema was developed as a content modeling language, an application of XML, and not as an application of SGML. So XML Schema pertains only to XML and XML-related languages. Schemas define the elements that can appear in an XML document and the attributes that can be associated with those elements. Schemas define the document's structure, i.e., : which elements are children of others. the order the child elements can appear. and the number of child elements. Schemas specify if an element is empty or if it can include text. Schemas can also specify default values for attributes. Schemas are more powerful and flexible than DTDs and use XML syntax. Schemas supports scope enabled definitions. Schema standards are defined by the World Wide Web Consortium(W3C). The W3C site provides comprehensive reference of XML schemas. A Simple XML file : < ?xml version = "1.0"?> <Colors> <First>

RED </First> <Second> PINK </Second> <Body> Welcome to the world of colors </Body> </Colors> The XML Schema, which defines the Colors data Type : <?xml version = "1.0"?> <Schema name = "ColorsDef " xmlns = "http://www.xyz.com /XMLSchema"> <ElementType name = "First" content="textOnly"/> <ElementType name = "Second" content="textOnly"/> <ElementType name = "Body" content="textOnly"/> <ElementType name = "Colors" content="eltOnly"> <element type = "First"/> <element type = "Second "/> <element type = "Body"/> </ElementType> </Schema>

XML Tutorial - XML and Styles


Developing an XML-related documents does not guarantee that it will be displayed the way you want it to appear. Browsers are not particularly good at formatting XML, and only the very latest browsers support it all. Although most of the time XML will be used to define data, not to display it, but sometimes you want to format the XML data for viewing. There are two main ways of doing this : CSS XSL Using CSS with XML CSS is a style language that lets you separate content and style as you design web pages. CSS can 'redefine' HTML tags, allowing them to be presented in different ways. Similarly, it can be used to define how XML tags are displayed. You can use three methods to specify styles for XML documents : Using External Style Sheets with XML documents External style sheets are always recommended because, you can change a document's style without having to access and modify the document itself and external style sheets can be shared among documents and thereby become the most efficient technique for applying your specified styles to more than one document at a time. Syntax <?xml-stylesheet type = "text/css" href = "stylesheetname.css"?> Example <?xml version="1.0"?> <?xml-stylesheet type = "text/css" href = "Ex1.css"?> <To> <SMS> <header> <from> James </from> <to> Smith </to> <Topic> Jokes </Topic>

</header> <body> a a a a a b b b b b b a b a b a b a b a b a b a b a b a. . . . Long time no c (see). </body> </SMS> </To> Below is the specification of the style sheet that has been included above : Ex1.css To { background-color : #ff00ff ; width : 100% ; } SMS { background-color: #Green ; margin-bottom: 30pt; } header { background-color: #999999; margin-bottom: 10pt; } from { color: #0000FF; font-size: 12pt; } Specifying an Internal Style Sheet You can include style rules in the data document itself by including the style rules in the <head> element of an XHTML file. Specifying an internal style sheet is practical for small-scale XML projects only. Syntax <?xml version ="1.0"?> <html xmlns : "http ://www.xyz.com"> <head> <style type = "text/css"> ...........</style> </head> <body> ...........</body> </html> Specifying Inline Styles You can also use the STYLE attribute to add styles to individual elements inline, i.e., within the XML document. The generic inline style syntax is : Syntax <elementname STYLE = "propertyname : value">............</elementname> Various Cascading Style Sheet Properties are :
Category Background properties Font Properties background background-attachment background-color font Property Name background-image background-position background-repeat font-stretch

font-family font-size font-size-adjust color direction letter-spacing line-height text-decoration text-align border border-bottom border-bottom-color border-bottom-style border-bottom-width border-color border-left border-left-color border-left-style border-left-width border-right border-right-color border-right-style border-right-width border-style border-collapse border-spacing column-span list-style list-style-image height line-height max-height

font-style font-variant font-weight text-shadow text-transform vertical-align white-space word-spacing text-indent border-top border-top-color border-top-style border-top-width border-width margin margin-bottom margin-left margin-right margin-top padding padding-bottom padding-left padding-right padding-top empty-cells row-span table-layout list-style-position list-style-type min-height min-width max-width

Text properties

Box properties

Table properties List properties Element dimension properties

---------------------------------------------

XML Tutorial - Embedding XML in HTML


XML can be used to store data inside HTML documents. XML data can be stored inside HTML pages as "Data Islands". As HTML provides a way to format and display the data, XML stores data inside the HTML documents. The data contained in an XML file is of little value unless it can be displayed, and HTML files are used for that purpose. The simple way to insert XML code into an HTML file is to use the <xml> tag. The XML tag informs, the browser that the contents are to be parsed and interpreted using the XML parser. Like most other HTML tags, the <xml> tag has attributes. The most important attribute is the ID, which provides for the unique naming of the code. The contents of the XML tag come from one of two sources : inline XML code or an imported XML file. If the code appears in the current location , it's said to be inline. Example Embedding XML code inside an HTML File. <html> <xml Id = msg> <message> <to> Visitors </to> <from> Author </from> <Subject> XML Code Islands </Subject> <body> In this example, XML code is embedded inside HTML code </body> </message> </xml> </html> The efficient way is to create a file and import it. You can easily do so by using the SRC attribute of the XML tag.

Syntax <xml Id = msg SRC = "example1.xml"> </xml> Data Binding Data binding involves mapping, synchronizing, and moving data from a data source, usually on a remote server, to an end user's local system where the user can manipulate the data. Using data binding means that after a remote server transmits data, the user can perform some minor data manipulations on their own local system. The remote server does not have to perform all the data manipulations nor repeatedly transmit variations of the same data. Data binding involves moving data from a data source to a local system, and then manipulating the data, such as, searching, sorting, and filtering, it on the local system. When you bind data in this way, you do not have to request that the remote server manipulate the data and then retransmit the results; you can perform some data manipulation locally. In data binding, the data source provides the data, and the appropriate applications retrieve and synchronize the data and present it on the terminal screen. If the data changes, the applications are written so they can alter their presentation to reflect those changes. Data binding is used to reduce traffic on the network and to reduce the work of the Web server, especially for minor data manipulations. Binding data also separates the task of maintaining data from the tasks of developing and maintaining binding and presentation programs. XML Parser XML parser, also known as XML processor, is a software package, library, or module that is used to read XML documents. The XML parsed, makes it possible for an XML application, such as a formatting engine or a viewer, to access the structure and content of an XML document. Basically XML parsers are of two types : Non-Validating Parser The parser does not check a document against any DTD, it only checks that the document is well-formed, i.e., the document is properly marked up according to XML syntax rules. Validating Parser In addition to check whether it is well-formed, the parser verifies that the document conforms to a specific DTD ( either internal or external to the XML file being parsed ). Many parsers are available, including Alpha Works XML for Java, which is used by IBM, Microsoft XML Parser, which is used in Microsoft Internet Explorer, and a parser called expat, which is used in the Netscape Navigator 6 browser application. Various Validating Parsers are Xerces from APACHE The apache XML project is maintaining XML parsers in Java, C++, and Perl. XML4J from IBM Version 1 of IBM's XML Parser for Java was the highest rated Java XML parser in Java Report's February 1999 review of XML parsers. Version 2 adds these exciting new features: Configurable, Modular Architecture; High Performance; Revalidation; and XCatalog Support. Support for XML 1.0, DOM 1.0 and SAX 1.0 is also included. XML4J 3.0.1 is based on the Apache Xerces XML Parser Version 1.0.3. New features include experimental versions of DOM Level 2, SAX2 (beta 2), and parts of W3C Schema. Oracle XML parser Oracle released its XML parser for Java, a standalone XML component that enables parsing of XML documents through either SAX or DOM interfaces using validating or non-validating modes. Various Non-Validating Parsers are Lark Lark is a non-validating Java XML processor by Tim Bray, one of the authors of the W3C XML specifications. It implements all of the XML 1.0 Recommendation and reports violations of well-formedness.

Expat XML Parser Toolkit is James Clark's library for XML parsing in C. Expat (formerly called xmltok) is being used to add support for XML to Netscape Navigator 5 and Perl. Expat aims to be a fully conforming XML 1.0 parser and is written in C. How to load XML file in browser Microsoft XML parser? Microsoft has had several implementations of XML processor technology. But the latest version of such technology is Microsoft's Java XML processor, called "MS XML" or "MSXML" , in common usage. MSXML 4 supports the WWW consortium's final recommendation for XML Schema. Processing can be both event-driven as well as document-centric with the W3C Document Object Model (DOM) approach. Microsoft claims that the XSLT engine for processing XML documents with XML style sheet transformations is substantially 4 to 8 times faster than before. Complex transformations need less time and memory than before. Microsoft also includes an XML parser in C++ in IE3, which is a high performance, non-validating parser, that supports most of the W3C XML specifications. Microsoft's XML parser supports all the necessary functions to traverse the node tree, access the nodes and their attribute values, insert and delete nodes, and convert the node tree back to XML. The following table lists the most commonly used node types supported by Microsoft's XML parser : Node Type Example Processing instruction <?xml version = "1.0"?> Element <bird type="non-flying"> Penguin </bird> Attribute type= "non-flying" Text Penguin Unicode Unicode, the universal character set, is one of the foundation technologies of XML. The unicode standard is a character coding system designed to support the worldwide interchange processing, and display of the written texts of the diverse languages and technical disciplines of the modern world. In March 2005, the unicode consortium announced the release of version 4.1.0 of the unicode standard. Unicode is a fundamental component of all modern software and information technology protocols. It supports classical and historical texts of many written languages. It provides a uniform, universal architecture and encoding, and is the basis for processing, storage, and seamless data interchange of text data worldwide. It consists of around 100,000 encoded characters, currently. Unicode is required by modern standards such as XML, Java, C#, CORBA3.0 etc. Escape Characters for XML If you want to use any of the special characters, such as, &, <, >," , etc., as normal characters, you must "escape" them by using the general entities that present them. To escape a character means to conceal it from a subsequent software or process. It is often used in computing terms to refer to prefixing certain characters in programming languages with a special character string to prevent them from being interpreted as special characters. In the following table, some pre-defined general entities are shown : Character & Replacement &amp;

'&apos; > &gt; < &lt; " &quot; What is CDATA? CDATA is an acronym for Character DATA. CDATA section is a part of an XML document in which markup is not interpreted as markup, but is passed to the application as it is. In other words, CDATA sections are used to escape blocks of text containing characters which would otherwise be recognized as markup. You can escape markup characters by using the predefined entities and character references. Replacing every markup character in a piece of text could be a long and tedious process. Besides, there might be cases when you want to keep all those characters exactly as they are. And the way to do this is to use a CDATA section. All tags and entity references are ignored by an XML processor that treats them just like any character data. CDATA blocks has been provided as a convenience measure when you want to include large blocks of special characters as character data. Example <![CDATA[ This is a text containing <5 lines> character data &!%# and it leaves the XML processor alone!]]> You cannot put one CDATA section inside another. Nothing that appears between the opening tag (<![CDATA[ ) and the closing tag ( ]]> ) will be recognized as markup. Comments are not recognized in a CDATA section. CDATA does not work in HTML. All text in an XML document will be parsed by the parser. Only text inside a CDATA section will be ignored by the parser. How Parsing is different for CDATA? The Parsing of CDATA is different from any other data as the XML processor does not parse what is inside a CDATA section, except to look for the CDATA section's closing delimiter ']]>'. So, the user can include text just as he wants it to appear. Data inside a CDATA section is just plain character data, which is unparsed data. The XML parser skips the text within the CDATA section, pastes the enclosed text block into its output, and then "forgets" a CDATA section ever existed.

Você também pode gostar