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
-
Gnome Fans Everywhere Rejoice for the Latest Release
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
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.