A Handy-Dandy XML Shortcut

I’ve been reading a lot on RDF lately in an effort to better understand how to keep from creating a brand-new resource format that no-one else will ever use for any web projects I work on for my job. During the research I’ve noticed a useful feature of XML (actually, it’s an SGML feature that is thankfully carried over to XML).

DOCTYPE declarations can be used to create your own entities! This in and of itself doesn’t sound like much, but a creative XML developer can use this to create abbreviations for commonly used bits of information.

For the CSS developer working on a style-sheet for an XHTML page, you can declare &ForegroundColor; to be “#ddaa00″ (if you like that sort of color) and instead of typing “#ddaa00″ several dozen times you can just use &ForegroundColor; instead. Later, when your boss decides that “#ddaa33″ is a more-appropriate shade of orange you only have to change it in one place.

But wait, there’s more (and there’s also a down-side which sucks big time …)

You can use these entity declarations to create shortcuts to URIs or email addresses. Why type http://www.example.org 10 times when &ex; works? And if you’re referring to several different pages by URI (that look the same from a distance) save yourself some brain-space by creating entities to represent them in the crafting of the page. http://www.example.org/X/24 vs http://www.example.org/X/25 can really become mind-numbing after a while. Entities allow you to call these two &ex24; or &ex;/X/24 (if you use the earlier declaration).

Now the nitty-gritty: implementation and caveats.

Implementing entities is fairly straightforward. For an existing XHTML document you can modify its current DOCTYPE in this manner: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”> becomes the following: <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” [
<!ENTITY taco "I love Tacos!">
]>

Each entity gets its own <!ENTITY taco “I love Tacos!”> where “taco” and “I love Tacos!” are the resulting entity name and value, respectively. Also note the lack of quotes around the identifier “taco.” Declared in this manner the resulting entity &taco; will be replaced with “I love Tacos!” in the document.

Now the caveat: this only works in XML. If you want to save yourself a ton of time with HTML documents you have to send them using the XHTML content types of either application/xhtml+xml or application/xml. These (correct) media types for XHTML tell the user agent to parse the page with an XML parser instead of a SGML parser (you may wonder, but if this is an SGML thingy, why the XML requirement? I have no clue).

So, to summarize XML DOCTYPE allows you to define entities that you can use in your XML documents. These entities can save you headaches and typing, and are worth trying out.

This entry was posted in web, xml. Bookmark the permalink.

3 Responses to A Handy-Dandy XML Shortcut

  1. Josh Peters says:

    I would update this page again instead of adding this comment, but boy oh boy WordPress messes up things when editing a post using HTML entities :)

    Wordpress tries to be helpful by translating all of the entities referenced into their characters which makes writing about markup quite tough.

  2. Bryan says:

    I have always wished CSS supported declaration of constants. The big win for CSS is not repeating myself, so why am I copy-pasting “#ddaa33″ 37 times? I’ve even tried parsing CSS through PHP so that I can use PHP variables to this end, which works but it just doesn’t feel right.

    Excellent use of taco in your example! :)

  3. Josh Peters says:

    My biggest beef with CSS is its lack of support for namespaces. I can deal with variables with PHP or some other language, but for styling XML-based markup CSS is all-or-nothing. I can’t serve an Atom Feed as my homepage without lots of hierarchal selectors matching my content.

    In other news, I’ve been working more to use “taco” whenever I can in examples over “foo” as it’s more Roach-friendly.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree