Using XML code in PHP scripts with XHP
Rules
A blog post always possesses at least a title, an introduction, and an optional body text. To express such conditions, XHP has the keyword children
(see Listing 10).
Listing 10
Defining Permissible Child Elements
An admin:post
must contain at least one admin:header
and one admin:introduction
. The notation is based on regular expressions.
Table 1 lists all operators that XHP understands. During processing, XHP checks these conditions and aborts if they do not apply. In Listing 10,
<admin:post> <admin:header /> <admin:introduction /> </admin:post>
would be accepted, but the missing introduction,
<admin:post> <admin:header /> </admin:post>
would not.
Table 1
Operators for Children
Symbol | Meaning |
---|---|
|
Zero elements or one element |
|
Zero or multiple elements |
|
One or multiple elements |
|
Or (one of conditions listed to the left and right of | must apply) |
|
The elements listed with a comma must appear in this order |
|
Arbitrary child elements are allowed |
|
No child elements are allowed |
The XHP developers explicitly point out that the underlying "greedy" algorithm takes preference. The children (:elem*, :elem);
condition thus triggers an error: The *
already covers all :elem
; another :elem
thus cannot follow.
If you need to enumerate many elements, you can simplify the task with element categories. In Listing 11, admin:retweet
and admin:tweet
belong to the admin:twittercategory
category. admin:twitter
can have any number of child elements, but they must come from the admin:twittercategory
category.
Listing 11
Use of Element Categories
The x:element
and x:primitive
classes are defined in the code.php
file. The comments stored in these classes also provide the only existing reference documentation. However, the html.php
file contains classes for all HTML elements; for example, :ul
handles the ul
list. Incidentally, the html.php
file also defines the x:doctype
class, which in turn represents the previously mentioned HTML5 document type definition <!DOCTYPE html>
. You currently need to create your own classes for other doctypes, such as XHTML.
The XHP documentation leaves much to be desired. As an introduction, you can read the README.textile
file from the source code archive, which you will also find on GitHub [1]. Additionally, you will find a pretty spartan wiki [3] and the blog posts by Facebook developer Stefan Parker [4]. Among other things, Parker shows how to parameterize CSS code in quite an elegant way using XHP [5].
Conclusions
XHP simplifies programming and input filtering. For example, XHP checks for correct nesting. Unknown elements have no output, and expressions in braces {
… }
are directly processed by htmlspecialchars()
.
Additionally, XHP is quite strict and, for example, takes exception to forgotten or incorrect end tags. Finally, you can quickly create building blocks for a template system using your own elements and tags.
On the other hand, converting the tags into objects costs computing time; in fact, this can be massively expensive depending on the code and usage. According to a comment in the XHP source code, the developers actually expect the APC cache system to be used as an accelerator [6] [7].
Administrators also need to compile, provision, and maintain the extension itself, which is not possible with some web hosting providers. Finally, improved readability depends strongly on the HTML code.
Infos
- XHP: https://github.com/facebook/xhp/tree/master/php-lib
- Documentation on
htmlspecialchars()
: http://php.net/manual/de/function.htmlspecialchars.php - XHP wiki: https://github.com/facebook/xhp/wiki/_pages
- Stefan Parker's blog: http://codebeforethehorse.tumblr.com/
- Stefan Parker: Abstracting CSS with XHP: http://codebeforethehorse.tumblr.com/post/3504948746/abstracting-css-with-xhp
ext.cpp
file: https://github.com/facebook/xhp/blob/master/ext.cpp- APC: http://php.net/manual/en/book.apc.php
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.