×
Clear all filters including search bar
Valeri Tandilashvili's XML Notes
CDATA
cannot contain the string ]]>
anywhere in the XML document
2. Nesting
is not allowed in CDATA sectiontarget
- Identifies the application to which the instruction is directed.
2. instruction
- A character that describes the information for the target
application to process.
...
PI's example in real XML document<?xml version="1.0" encoding="UTF-8"?>
<student>
<?sort alpha-ascending?>
<?textinfo whitespace is allowed ?>
<first-name>George</first-name>
<phone.mobile>(011) 123-4567</phone.mobile>
</student>
In this example we have two PIs
1. sort
with value - alpha-ascending
2. textinfo
with value - whitespace is allowed
...
Note: processing instructions can contain any data except the combination ?>
, which is interpreted as a closing delimiter1. XML Syntactical whitespace - is whitespace required by the XML in order to delimitate XML constructs.
2. Insignificant Whitespace - is whitespace, that is not considered part of an element, if two elements are separated by just whitespace its considered insignificant.
3. Significant Whitespace - is whitespace that XML element attribute contains.
- Comment must appear after XML declaration.
- Comment may appear anywhere in a document
- Comment must not appear within attribute values.
- Comment cannot be nested inside the other comment.
case-sensitive
- because of the case difference in two tags, which is treated as incorrect syntax in XML<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<student>
<name>George</name>
<city>Tbilisi</city>
<phone>(011) 123-4567</phone>
<address>Wrong syntax</ADdress>
<hr />
</student>
This will generate the error:error on line 6 at column 23: Opening and ending tag mismatch: address line 0 and ADdress
XML tags must be closed in an appropriate order
, which means an XML element opened inside another element must be closed before the outer element<outer_element>
<internal_element>
Right, because the inner tag is closed before the outer
</internal_element>
</outer_element>
address.xml
into an XML DOM object.
Then extracts contact
information using JavaScript
<!DOCTYPE html>
<html>
<body>
<h1>TutorialsPoint DOM example </h1>
<div>
<b>Name:</b> <span id = "name"></span><br>
<b>Company:</b> <span id = "company"></span><br>
<b>Phone:</b> <span id = "phone"></span>
</div>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
// else
// {// code for IE6, IE5
// xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
// }
xmlhttp.open("GET","address.xml",false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
document.getElementById("name").innerHTML = xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
document.getElementById("company").innerHTML = xmlDoc.getElementsByTagName("company")[0].childNodes[0].nodeValue;
document.getElementById("phone").innerHTML = xmlDoc.getElementsByTagName("phone")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
address.xml
file content<?xml version = "1.0"?>
<contact>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</contact>
UTF-8
- 8-bits are used to represent a character
2. UTF-16
- 16-bits are used to represent a character
...
UTF
stands for UCS Transformation Format
, and UCS itself means Universal Character Set
...
XML document example that has UTF-8
encoding<?xml version="1.0" encoding="UTF-8"?>
<student>
<first-name>George</first-name>
<phone.mobile>(011) 123-4567</phone.mobile>
</student>
Note: If an XML document has not specified encoding, the default is UTF-8
<?php
$my_xml_data =
'<?xml version="1.0" encoding="UTF-8"?>
<student>
<?sort alpha-ascending?>
<?textinfo whitespace is allowed ?>
<first-name>George</first-name>
<phone.mobile>(011) 123-4567</phone.mobile>
</student>';
$xml = simplexml_load_string($my_xml_data);
$doc = dom_import_simplexml($xml)->ownerDocument;
$xpath = new DOMXPath($doc);
# prints "/* processing instructions */ ", the value of the first PI: textinfo
echo $xpath->evaluate('string(//processing-instruction("textinfo")[1])');
The output of the above code is the following:whitespace is allowed
<!-- Our comment -->
XML comment example in XML document:<?xml version="1.0" encoding="UTF-8"?>
<student>
<!-- Some comment about the student -->
<first-name>George</first-name>
<phone.mobile>(011) 123-4567</phone.mobile>
<tive_language>English</tive_language>
<another_tag>some text</another_tag>
<city />
</student>
<?xml version="1.0" encoding="UTF-8"?>
<student>
<name>George</name>
<phone>(011) 123-4567</phone>
<address></address>
<city />
</student>
One way is <address></address>
element and another is <city />
. In other words, one with closing tag and another is self-closing tag.