Structured, Semi-Structured and Unstructured Data
- Structured data follows a fixed schema (rows and columns of a relational table).
- Semi-structured data has some structure but no rigid schema: it is self-describing with tags or keys, and records may differ in fields (XML, JSON, HTML).
- Unstructured data has no data model: plain text, images, audio and video.
Most data on the web is semi-structured or unstructured.
XML
XML (eXtensible Markup Language) is a text format with user-defined tags that describes data. A well-formed document has one root element, properly nested and closed tags and quoted attribute values. A valid document also follows a schema written as a DTD or XML Schema (XSD), which defines elements, attributes, types and cardinalities. Namespaces avoid name clashes. An XML document is a tree of elements, attributes and text.
Querying XML
- XPath selects nodes with path expressions: /library/book[price > 300]/title.
- XQuery is a full query language with FLWOR expressions (for, let, where, order by, return).
- XSLT transforms XML into other documents (HTML).
Storing XML
XML can be stored as text or a CLOB, shredded into relational tables, or kept in a native XML database. SQL/XML adds an XML data type and functions such as XMLELEMENT and XMLQUERY to relational systems.
Web Databases
A web database application uses a three-tier architecture: the browser sends an HTTP request, the web server passes it to an application server (servlets, PHP, Python), which queries the DBMS and builds an HTML or JSON reply. Related issues are session state, connection pooling, caching, security (SQL injection, authentication) and scalability. Internet data is also handled by search engines (crawl, index, rank) and NoSQL stores.
Definition: Semi-structured data. Data that carries its own structure in tags or keys but has no fixed schema, so records of the same kind may have different fields.
Table 4.5: Types of data
| Type | Schema | Example |
|---|---|---|
| Structured | Fixed | Relational table |
| Semi-structured | Flexible, self-describing | XML, JSON |
| Unstructured | None | Text, image, video |
Example 4.5: Querying an XML document
Problem: An XML library has three books with prices 250, 450 and 500. Using the path /library/book[price > 300], how many books are selected and what is their total price?
Solution:
- The books with price above 300 are those priced 450 and 500.
- The count is 2.
- The total price is 450 + 500 = 950.
Result: Two books are selected and their total price is 950.
import xml.etree.ElementTree as ET
xml = """<library>
<book id="1"><title>Algebra</title><price>250</price></book>
<book id="2"><title>Databases</title><price>450</price></book>
<book id="3"><title>Networks</title><price>500</price></book>
</library>"""
root = ET.fromstring(xml)
print("Root:", root.tag, " Books:", len(root.findall("book")))
def price(b):
return int(b.find("price").text)
sel = [b for b in root.findall("book") if price(b) > 300]
print("Selected:", [b.find("title").text for b in sel])
print("Count:", len(sel))
print("Total price:", sum(price(b) for b in sel))
print("Book ids:", [b.get("id") for b in root.iter("book")])
Output
Root: library Books: 3
Selected: ['Databases', 'Networks']
Count: 2
Total price: 950
Book ids: ['1', '2', '3']
Common mistakes
- Leaving an XML document with unclosed or wrongly nested tags.
- Mixing up well-formed (syntax) with valid (follows a schema).
- Building SQL for a web page by joining user input strings.
- Treating semi-structured data as unstructured; it still has tags or keys.
Key takeaways
- Structured, semi-structured (XML, JSON) and unstructured data differ in schema.
- XML is well-formed if properly nested; valid if it follows a DTD or XSD.
- XPath selects nodes; XQuery is the full query language.
- Web databases use a three-tier architecture.
Practice questions
- What is the difference between a well-formed and a valid XML document?
- Name two query languages for XML.
- Give an example of semi-structured data.
Answers to the practice questions
- Well-formed follows XML syntax; valid also conforms to a DTD or schema.
- XPath and XQuery.
- An XML or JSON document.
Sources
- Official BCA syllabus Advanced Database Systems, course code 25BCA406DS04, Unit IV
