Beautiful Soup for the lxml parser allows you to work with XML documents.
You can also parse your HTMLS, XML, and use XPath with it.
So what is XML?
XML stands for Extensible Markup Language.
And it's a markup language just like HTML.
It has some similarities to HTML,
but it's commonly used for information transfer.
So in that context, XML is more like the CSV format or the JSON format,
which we use for serialization and data transfer.
It's not meant for display like HTML is.
Beautiful Soup works not just with HTML, but can also work with XML documents.
The lxml parser allows Beautiful Soup to parse and work with XML,
just like you would with HTML.
We've spoken of the fact that XML and HTML are very similar.
Let's compare and contrast the two.
XML stands for Extensible Markup Language.
HTML is Hypertext Markup Language.
Both specify a markup, but the markup is used for different purposes.
XML is used as a data serialization and transfer protocol.
If you want to serialize information out to disk or
transfer them across the wire to another node,
that's when you use XML.
HTML is used almost exclusively to set up the
structure and content of web pages.
XML markup needs to be well formed.
There is no room for missing tags and other errors in the XML.
Well, HTML, if you've ever worked with HTML in the real world,
you know that browsers are quite lax about ill-formed markup,
such as missing tags.
The tags that you specify within your XML structure tends to be user defined.
The tags have a meaning based on the content that you're storing.
If you're storing the name of an individual, the tag will be name.
If you're storing the age, the tag will be age.
On the other hand, when working with HTML,
you typically don't use user-defined tags.
The tags are those that are understood and interpreted by web browsers.
Even though XML and HTML structure tends to be the same,
both have an inherent hierarchy.
XML tends to be quite human readable,
especially when you compare XML files to CSV or JSON files.
Production-grade HTML is almost never human readable.
Despite these differences,
XML and HTML are similar enough in structure that XPath works with both.
So what exactly is XPath?
XPath gives you a way to navigate a tree structure to access a particular node.
XPath is a query language that can be used to select nodes from
an XML document or even an HTML document.
In order to use XPath, you need to have an XML parse tree,
which is similar to an HTML parse tree.
XPath allows you to select specific nodes,
based on the location of that node relative to the root of the document.
You can use XPath to navigate up and down the XML parse tree.
XPath gives you access to parent nodes and child nodes.
You can also filter and search within an XML tree.
You can find all nodes that meet a certain predicate that you specified.
Later in this module,
you'll see examples where we'll use XPath to select nodes from an XML tree.
The XPath syntax is relatively intuitive,
and it's very similar to the notation used in UNIX file paths.
There is additional functionality that XPath offers though to
apply predicates and for other use cases.