schema-org #
What a search engine reads from a small business's website: schema.org
JSON-LD, Sitemaps 0.9 and robots.txt (RFC 9309).
A hotel is found through search engines that read three things off its
site. Its pages describe the hotel, its rooms, their prices and its ratings
in the schema.org vocabulary, and its confirmation mails carry the
reservation in the same vocabulary. A sitemap lists the pages a crawler
should visit, and robots.txt says which of them it may fetch. This library
writes the first and parses and writes the other two.
Installation #
$ opam install schema-org
If opam cannot find the package, it has not reached the public
opam-repository yet. Add the overlay repository first:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install schema-org
schema.org markup #
Each schema.org type a small business's site uses has a constructor whose
labels are the type's properties in snake case. A node's OCaml type lists
its schema.org type and supertypes, so a hotel goes wherever a lodging
business, a local business or an organization is expected, and a postal
address goes nowhere a rating does:
let room =
Schema_org.hotel_room "Double room"
~bed:[ Schema_org.bed_details ~number_of_beds:1 "Double" ]
~offers:[ Schema_org.offer ~price_currency:"EUR" "119.00" ]
let hotel =
Schema_org.hotel "Hotel Example"
~address:
(Schema_org.postal_address ~street_address:"1 Harbour Road"
~address_locality:"Exampleton" ~address_country:"GB" ())
~checkin_time:(Schema_org.Time.v 15 0)
~star_rating:(Schema_org.rating 4.)
~contains_place:[ room ]
Schema_org.Document writes a list of nodes as JSON-LD 1.1 in compacted
form with the schema.org context, one node as it is and several under
@graph. Document.script wraps it in
the HTML element a page embeds, with every < escaped so that no text in
it can close the element:
# print_endline (Schema_org.Document.to_string [ Schema_org.node room ]);;
{"@context":"https://schema.org","@type":["HotelRoom","Product"],"name":"Double room","bed":{"@type":"BedDetails","numberOfBeds":1,"typeOfBed":"Double"},"offers":{"@type":"Offer","price":"119.00","priceCurrency":"EUR"}}
- : unit = ()
A confirmation mail carries the reservation:
let mail_markup =
Schema_org.Document.script
[
Schema_org.node
(Schema_org.lodging_reservation "abc456"
~reservation_status:Confirmed
~under_name:(Schema_org.person "Jo Example")
~reservation_for:hotel
~checkin_time:
(Schema_org.Datetime.v 2027 4 11
(Schema_org.Time.v ~tz:60 15 0)));
]
The library writes JSON-LD and does not process it: it expands no context
and reads no document.
Sitemaps #
Schema_org.Sitemap parses and writes urlsets and sitemap indexes with the
protocol's limits (50,000 entries, 50 MiB, a loc under 2,048 characters,
a priority from 0.0 to 1.0) and lastmod dates in W3C Datetime, and checks
that a sitemap lists only URLs under its own directory:
# let sitemap =
Schema_org.Sitemap.Urlset
[
Schema_org.Sitemap.url "https://hotel.example/"
~lastmod:(Schema_org.Datetime.date 2026 11 28) ~priority:1.0;
]
in
print_string (Schema_org.Sitemap.to_string sitemap);;
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://hotel.example/</loc>
<lastmod>2026-11-28</lastmod>
<priority>1.0</priority>
</url>
</urlset>
- : unit = ()
robots.txt #
Schema_org.Robots parses robots.txt as RFC 9309 defines it, answers
whether a crawler may access a URL (the groups naming its product token,
else the * groups; the longest matching rule, Allow winning a tie) and
writes a file back:
# let robots =
Schema_org.Robots.of_string
"User-agent: *\nDisallow: /booking/\nAllow: /booking/rooms\n"
in
( Schema_org.Robots.allowed robots ~user_agent:"examplebot"
"https://hotel.example/booking/rooms",
Schema_org.Robots.allowed robots ~user_agent:"examplebot"
"https://hotel.example/booking/pay" );;
- : bool * bool = (true, false)
Robots.of_status maps the status of the robots.txt fetch to what a
crawler obeys: the file on a 2xx, everything allowed on a 4xx, everything
disallowed on a 5xx (RFC 9309, section 2.3.1).
Command line #
schema-org robots check --user-agent TOKEN FILE URL... prints whether the
crawler may access each URL, schema-org robots format FILE writes a
robots.txt back in its canonical form, and schema-org sitemap check [--location URL] FILE validates a sitemap and lists its entries.
References #
- schema.org, the vocabulary.
- JSON-LD 1.1, W3C Recommendation,
2020-07-16, and its Processing Algorithms and
API, whose compaction writes a
one-element array as its element. - Sitemaps XML format 0.9.
- W3C Datetime, W3C Note,
1998-09-15. - RFC 9309, Robots Exclusion
Protocol. - Google's robots.txt parser, whose
test cases the robots.txt tests replay.
License #
ISC. See LICENSE.md.