XML RDF
RDF/XML is an XML serialization of graph statements. Understanding its element tree is useful, but the graph meaning requires RDF rules beyond generic XML parsing.
How it works
A subject, predicate and object form a statement. rdf:about can identify a subject and rdf:resource can identify an object resource; ordinary property text can represent a literal. Different RDF/XML trees may describe the same graph. A generic XPath extraction in this lesson reads one known serialization only. It does not perform graph normalization, ontology reasoning or arbitrary RDF query evaluation.
Run the code
Use Python 3 with lxml 6.1.1 in a separate virtual environment. The complete listing embeds its own XML and any schema or stylesheet; run python rdf-links.py. It does not need Apache or MySQL. The shared restrictive parse function rejects declarations for untrusted input. The trusted DTD/entity and streaming lessons explicitly explain their separate fixed-input boundaries.
Download the complete rdf-links.py source. Keep each lab in its own folder. Sources are served as text, not as public executable server scripts. Review the starting setup.
Example
from lxml import etree as E
class DenyExternal(E.Resolver):
def resolve(self, url, public_id, context):
raise ValueError("External resources are disabled")
def parse(text):
if "<!DOCTYPE" in text.upper() or "<!ENTITY" in text.upper():
raise ValueError("Declarations are disabled for untrusted XML")
parser = E.XMLParser(resolve_entities=False, load_dtd=False,
no_network=True, huge_tree=False)
parser.resolvers.add(DenyExternal())
return E.fromstring(text.encode("utf-8"), parser)
source = "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:seed=\"urn:bhylu:seed:\"><rdf:Description rdf:about=\"urn:packet:p1\"><seed:category rdf:resource=\"urn:category:flower\"/></rdf:Description></rdf:RDF>"
root = parse(source)
ns = {"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "seed": "urn:bhylu:seed:"}
print(root.xpath("string(rdf:Description/@rdf:about)", namespaces=ns))
print(root.xpath("string(rdf:Description/seed:category/@rdf:resource)", namespaces=ns))
Expected result
urn:packet:p1 urn:category:flower
The fixture states a relationship between two identified resources. A full RDF tool interprets that relationship as a graph statement, whereas this lab only verifies the selected source fields. Use a graph library when the application depends on graph equivalence.
Check the exact contract in the XML specification, XPath 1.0, the XSD 1.0 primer, lxml validation documentation and Saxon’s processor documentation.