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
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.
-
Fedora 41 Released with New Features
If you're a Fedora fan or just looking for a Linux distribution to help you migrate from Windows, Fedora 41 might be just the ticket.
-
AlmaLinux OS Kitten 10 Gives Power Users a Sneak Preview
If you're looking to kick the tires of AlmaLinux's upstream version, the developers have a purrfect solution.
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.