<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Prolog i.e. <?xml version="1.0" encoding="UTF-8"?>. This is optional but if added, should go on top.UTF-8<Apple> is different from the tag <apple><b><i>This text is bold and italic</b></i><note date=12/11/99>, <note date="12/11/99">< should be << < less than
> > greater than
& & ampersand
' ' apostrophe
" " quotation mark
<!-- This is a comment -->XML documents that conform to the syntax rules above are said to be "Well Formed" XML documents.
An XML element is everything from (including) the element's start tag to (including) the element's end tag.
An element can contain:
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
</description>
<calories>950</calories>
</food>
</breakfast_menu>
<food><person id="3344"><person sex="female"> or <person><sex>female</sex></person><namespace:element_name>xmlns attribute i.e. <namespace:element_name xmlns="some_url"><root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="https://www.w3schools.com/furniture">
<table xmlns="namespaceURI">
CDATA: (Unparsed Character data): CDATA contains the text which is not parsed further in an XML document. Tags inside the CDATA text are not treated as markup and entities will not be expanded.
<?xml version="1.0"?>
<employee>
<![CDATA[
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>vimal@javatpoint.com</email>
]]>
</employee>
Output:
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>vimal@javatpoint.com</email>
PCDATA: (Parsed Character Data): XML parsers are used to parse all the text in an XML document. PCDATA stands for Parsed Character data. PCDATA is the text that will be parsed by a parser. Tags inside the PCDATA will be treated as markup and entities will be expanded.
In other words you can say that a parsed character data means the XML parser examine the data and ensure that it doesn't content entity if it contains that will be replaced.
Known as XML Schema Definition (XSD)
Defines the elements, attributes and data types
Describes and validates the structure and the content of XML data
An XML Schema describes the structure of an XML document
An XML document with correct syntax is called "Well Formed".
An XML document validated against an XML Schema is both "Well Formed" and "Valid".
You can define XML schema elements in the following ways −
Simple type element is used only in the context of the text. Some of the predefined simple types are: xs:integer, xs:boolean, xs:string, xs:date. For example −
<xs:element name = "phone_number" type = "xs:int" />
A complex type is a container for other element definitions. This allows you to specify which child elements an element can contain and to provide some structure within your XML documents. For example −
<xs:element name = "Address">
<xs:complexType>
<xs:sequence>
<xs:element name = "name" type = "xs:string" />
<xs:element name = "company" type = "xs:string" />
<xs:element name = "phone" type = "xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
In the above example, Address element consists of child elements. This is a container for other <xs:element> definitions, that allows to build a simple hierarchy of elements in the XML document.
With the global type, you can define a single type in your document, which can be used by all other references. For example, suppose you want to generalize the person and company for different addresses of the company. In such case, you can define a general type as follows −
<xs:element name = "AddressType">
<xs:complexType>
<xs:sequence>
<xs:element name = "name" type = "xs:string" />
<xs:element name = "company" type = "xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
Now let us use this type in our example as follows −
<xs:element name = "Address1">
<xs:complexType>
<xs:sequence>
<xs:element name = "address" type = "AddressType" />
<xs:element name = "phone1" type = "xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name = "Address2">
<xs:complexType>
<xs:sequence>
<xs:element name = "address" type = "AddressType" />
<xs:element name = "phone2" type = "xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
Instead of having to define the name and the company twice (once for Address1 and once for Address2), we now have a single definition. This makes maintenance simpler, i.e., if you decide to add "Postcode" elements to the address, you need to add them at just one place.
Attributes in XSD provide extra information within an element. Attributes have name and type property as shown below −
<xs:attribute name = "x" type = "y"/>
Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
The following example defines an element called "age" with a restriction. The value of age cannot be lower than 0 or greater than 120:
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Above example defines an element called "car" with a restriction. The only acceptable values are: Audi, Golf, BMW
<xs:element name="color" type="xs:string" default="red"/>
<xs:element name="color" type="xs:string" fixed="red"/>
Well Formed.Valid.<!DOCTYPE element DTD identifier
[
declaration1
declaration2
........
]>
<!DOCTYPE delimiter.In a DTD, elements are declared with an ELEMENT declaration.
<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>
<!ELEMENT element-name EMPTY>
<!ELEMENT element-name (#PCDATA)>
Example:
<!ELEMENT from (#PCDATA)>
<!ELEMENT element-name (child1)>
<!ELEMENT element-name (child1,child2,...)>
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
+)<!ELEMENT element-name (child-name+)>
*)<!ELEMENT element-name (child-name*)>
<!ELEMENT note (#PCDATA|to|from|header|message)*>
In a DTD, attributes are declared with an ATTLIST declaration.
<!ATTLIST element-name attribute-name attribute-type attribute-value>
<!ATTLIST payment type CDATA "check">
<payment type="check" />
value - The default value of the attribute
#REQUIRED - The attribute is required
#IMPLIED - The attribute is optional
FIXED - The attribute value is fixed
CDATA - The value is character data
(en1|en2|..) The value must be one from an enumerated list
ID The value is a unique id
IDREF The value is the id of another element
IDREFS The value is a list of other ids
NMTOKEN The value is a valid XML name
NMTOKENS The value is a list of valid XML names
ENTITY The value is an entity
ENTITIES The value is a list of entities
NOTATION The value is a name of a notation
xml: The value is a predefined xml value
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">
<square width="100" />
In the example above, the "square" element is defined to be an empty element with a "width" attribute of type CDATA. If no width is specified, it has a default value of 0.
A DTD is referred to as an internal DTD if elements are declared within the XML files. To refer it as internal DTD, standalone attribute in XML declaration must be set to yes. This means, the declaration works independent of an external source.
Following is the syntax of internal DTD −
<!DOCTYPE root-element [element-declarations]>
Where root-element is the name of root element and element-declarations is where you declare the elements.
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<!DOCTYPE address [
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
]>
<address>
<name>Rohit Sharma</name>
<company>Swastik Collge</company>
<phone>9841000000</phone>
</address>
In external DTD elements are declared outside the XML file. They are accessed by specifying the system attributes which may be either the legal .dtd file or a valid URL. To refer it as external DTD, standalone attribute in the XML declaration must be set as no. This means, declaration includes information from the external source.
Following is the syntax for external DTD −
<!DOCTYPE root-element SYSTEM "file-name">
Where file-name is the file with .dtd extension.
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
<!DOCTYPE address SYSTEM "address.dtd">
<address>
<name>Rohit Sharma</name>
<company>Swastik Collge</company>
<phone>9841000000</phone>
</address>
The content of the DTD file address.dtd is as shown −
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
A system identifier enables you to specify the location of an external file containing DTD declarations. Syntax is as follows −
<!DOCTYPE name SYSTEM "address.dtd" [...]>
As you can see, it contains keyword SYSTEM and a URI reference pointing to the location of the document.
Public identifiers provide a mechanism to locate DTD resources and is written as follows −
-<!DOCTYPE name PUBLIC "-//Beginning XML//DTD Address Example//EN">
As you can see, it begins with keyword PUBLIC, followed by a specialized identifier. Public identifiers are used to identify an entry in a catalog. Public identifiers can follow any format, however, a commonly used format is called Formal Public Identifiers, or FPIs.
XPath specifies seven types of nodes that can be output of the execution of the XPath expression.
Index Expression Description
1) node-name It is used to select all nodes with the given name "nodename"
2) / It specifies that selection starts from the root node.
3) // It specifies that selection starts from the current node that match the selection.
4) . Select the current node.
5) .. Select the parent of the current node.
6) @ Selects attributes.
7) student Example - selects all nodes with the name `student`.
8) class/student Example - selects all student elements that are children of class
9) //student Selects all student elements no matter where they are in the document
XSL stands for EXtensible Stylesheet Language. It is similar to XML as CSS is to HTML. In case of HTML document, tags are predefined such as table, div, and span; and the browser knows how to add style to them and display those using CSS styles. But in case of XML documents, tags are not predefined. In order to understand and style an XML document, World Wide Web Consortium (W3C) developed XSL which can act as XML based Stylesheet Language. An XSL document specifies how a browser should render an XML document.
XSLT, Extensible Stylesheet Language Transformations, provides the ability to transform XML data from one format to another automatically.
An XSLT stylesheet is used to define the transformation rules to be applied on the target XML document. XSLT stylesheet is written in XML format. XSLT Processor takes the XSLT stylesheet and applies the transformation rules on the target XML document and then it generates a formatted document in the form of XML, HTML, or text format. This formatted document is then utilized by XSLT formatter to generate the actual output which is to be displayed to the end-user.
Syntax of .xsl file:
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "xpath">
<xsl:for-each select="xpath">
<xsl:value-of select = "xpath"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Now include/ink the xsl file into the XML
<?xml-stylesheet type = "text/xsl" href = "file.xsl"?>
<?xml-stylesheet tag is used to import the XSL file into the XML file with type=text/xsl and href=file.xsl attributes.
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<html>
<body>
<h2>Students</h2>
<table border = "1">
<tr bgcolor = "#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Marks</th>
</tr>
<xsl:for-each select="class/student">
<xsl:sort select = "firstname"/>
<xsl:if test = "marks > 90">
<tr>
<td>
<xsl:value-of select = "@rollno"/>
</td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
<td><xsl:value-of select = "marks"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "students.xsl"?>
<class>
<student rollno="393">
<firstname>Ram</firstname>
<lastname>Sharma</lastname>
<nickname>Ram</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Rohit</firstname>
<lastname>Yadav</lastname>
<nickname>Rohit</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Shishir</firstname>
<lastname>Gautam</lastname>
<nickname>Sisi</nickname>
<marks>90</marks>
</student>
</class>
Defines a way to reuse templates in order to generate the desired output for nodes of a particular type/context.
Defines a way to reuse templates in order to generate the desired output for nodes of a particular type/context.
Tag puts the value of the selected node as per XPath expression, as text.
Tag applies a template repeatedly for each node.
Tag specifies a sort criteria on the nodes.
Tag element helps to debug an XSLT processing. It is similar to javascript alerts. <xsl:> tag buffers a message to XSLT processor which terminates the processing and sends a message to the caller application to display the error message.
<xsl:if test = "firstname = ''">
<xsl:message terminate = "yes">A first name field is empty.
</xsl:message>
</xsl:if>
Tag specifies a multiple conditional tests against the content of nodes in conjunction with the <xsl:otherwise> and <xsl:when> elements.
<xsl:choose>
<xsl:when test = "marks > 90">
High
</xsl:when>
<xsl:when test = "marks > 85">
Medium
</xsl:when>
<xsl:otherwise>
Low
</xsl:otherwise>
</xsl:choose>
book.xml
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
txt = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
And following book.html example loads a text string into an XML DOM object book.xml, and extracts the info from it with JavaScript:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var xmlDoc = this.responseXML;
var p = document.getElementById("demo");
var titles = xmlDoc.getElementsByTagName("title");
var titlesHtml = ''
;
for(var i = 0; i < titles.length; i++){
var cat = titles[i].parentNode.getAttribute('category');
titlesHtml += "" + titles[i].childNodes[0].nodeValue + " (" + cat +")";
}
titlesHtml += '';
p.innerHTML = titlesHtml;
}
};
xhttp.open("GET", "book.xml", true);
xhttp.send();
function myFunction(xml) {
var xmlDoc = xml.responseXML;
document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
}
</script>
</body>
</html>
This example parses a text string into an XML DOM object, and extracts the info from it with JavaScript:
load_string.html
<html>
<body>
<p id="demo"></p>
<script>
var text, parser, xmlDoc;
text = "" +
"Everyday Italian" +
"Giada De Laurentiis" +
"2005" +
"";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
These are some typical DOM properties:
Note: In the list above, x is a node object.
rootparent nodechildrenno childrenSiblings are nodes with the same parentnodeValue property is used to get the text value of a node.getAttribute('attribute_name') method returns the value of an attribute.x = xmlDoc.getElementsByTagName("title")[0];
txt = x.getAttribute("lang");
newElement = xmlDoc.createElement("edition");
newEle = xmlDoc.createElement("edition");
xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);
XQuery is a standardized language for combining documents, databases, Web pages and almost anything else. It is very widely implemented. It is powerful and easy to learn. XQuery is replacing proprietary middleware languages and Web Application development languages. XQuery is replacing complex Java or C++ programs with a few lines of code. XQuery is simpler to work with and easier to maintain than many other alternatives.