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

any

Arbitrary child elements are allowed

empty

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.

The Author

Tim Schürmann is a freelance computer scientist and author of many books and articles on practical IT topics.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Cascading Style Sheets

    Cascading Style Sheets (CSS) help you polish up your websites without taking a crash course in programming.

  • CSS Tricks

    Cascading Style Sheets (CSS) can do much more than define the color and font of your web text. We'll show you how to build the power of CSS into your web creations.

  • Hidden Meaning: Working with Microformats

    Programs aren’t as smart as humans when it comes to interpreting the meaning of web information. If you want to maximize your search rank, you might want to dress up your HTML documents with microformats and microdata.

  • Mindmapping

    View Your Mind lets you draw and manage mind maps that can help you structure your thoughts and ideas and record notes.

  • Perl: AJAX

    AJAX technology adds dynamic elements to enhance sluggish websites. All it takes is a server-side Perl program and some client-side JavaScript code.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News