Chip's Tips for Developers

Contains coding, but not narcotic.

OPML Browser for WordPress gets a new parser

August 24th, 2009 3:36:49 pm pst by Sterling Camden

I thought of a new use for my OPML Browser plugin, but the OPML file I intended to use with it threw me a couple of curve balls.

First of all, the file contained nested outline elements, with xmlURL attributes on the containing elements.  Until now, I displayed the feed icon if an item had a feed, and only displayed the folder icon if it didn’t (and it had child elements).  Because the only way to expand/collapse the tree is by clicking on the folder icon, I’ve changed this to display both icons in that case.

Second, my file nests the outline tags more than two levels deep.  I was using preg_match to parse the XML, with a non-greedy search for the end tag.  Can you guess what the problem was with that approach?  I’d get the end tag for an internal outline item when I was looking for the end of the outer item.  This only happened, of course, when an inner item was terminated by </outline>.

Rather than trying to get even fancier with my regular expressions, I decided to implement a real XML parser instead.  I took a look at SimpleXML, but it requires PHP5 and allow_fopen_url.  Then I looked at PHP’s built-in XML parser functions, but that’s event-based and I needed to wade through the document on my own terms.  So I implemented my own simple XML DOM parser, and included that with the plugin.

Posted in OPML, PHP, WordPress | No Comments » RSS 2.0 | Sphere it!

Simple XML DOM parser for PHP

August 24th, 2009 2:57:47 pm pst by Sterling Camden

image I realize that I’ve just added to the pile of reinvented wheels here, but I needed something simpler than SimpleXML (particularly in the requirements department) that nevertheless translated the event-based model of PHP’s built-in parser to a DOM model.

Thus, I created the very simple parser which you can download below.  It’s only 67 lines long.  It’s partially based on the example from wolfon, but simplified even further by adding an xmlElement class instead of trying to force all of the relationships into an array format.

To use:

require("xmlparse.php");

$dom = new xmlDOM($xml_text);   // Parses $xml_text

$dom->root is an xmlElement with a zero-length tagName whose children member is an array of the top-level elements (of which there should only be 1) .

$dom->find_tag($tagName, $from);

Returns the first xmlElement whose tagName matches $tagName, starting with the children of $from.  If $from is null, it starts from the root.  If not found, false is returned.

An xmlElement has the following members:

parent – the parent element
tagName – the tag name
attributes – an associative array of name => value
children – an array of child elements
data – any character data within the element

find_tag($tagName) – finds a descendant element with the specified tag name.  Returns false if none found.

Posted in PHP, XML | 2 Comments » RSS 2.0 | Sphere it!

Extreme makeover for Chip’s Tips!

July 4th, 2006 4:19:35 pm pst by Sterling Camden

In case you didn’t notice, I’ve converted Chip’s Tips into a WordPress blog. How do you like the new look?

I started Chip’s Tips back in 2001, before most people had ever heard of blogs. Yet, that’s more or less what it was from the start. I created my own XML grammar for outlining the topics, and another grammar for the posts themselves. Then I wrote Active Server Pages (ASP) to transform those XML documents into HTML pages using XSLT. It was my first project in both ASP and XSLT, and it worked quite well.

Lately, though, it’s been showing its age. ASP (not ASP.NET) is now an old technology. Blogs have evolved rapidly, and I wanted to be able to easily implement several key features:

  1. An RSS feed for the posts
  2. Comments and trackback/pingback
  3. Tagging and multiple categories per post

Besides, my process for posting was less than smooth. I would copy an existing XML document for a post, modify its content, add an entry for it in the outline XML file, then upload both of those plus any download file. Often I would create unmatched tags in the body of the topic file so the XML parser would fail. Count on at least fifteen minutes of fiddling in addition to however long it took me to write a post. With a blog, all of that is automagic.

So, how did I migrate?

First, I needed a content migration strategy. I wrote a PHP script to read the existing outline and topic XML files and generate an RSS feed. That way, I could use WordPress’ RSS importer to pull the content.

Second, I set up the WordPress site and tweaked the theme I was using for Chip’s Quips to work here. Different color scheme, some new pages, etc. Add the Google ads and search, and the scripts for Technorati and ReefeRSS.

Third, I applied the same hack I used on Chip’s Quips to redirect the feeds, and burned the feeds at FeedBurner. Then I setup landing pages at FeedPass. Added the subscribe links along with the Chicklet generator and R-mail widget to the sidebar.

The hardest part was setting up all of the new categories (I wanted to reshuffle those) and tag all of the existing posts. I also included one unique tag per post that matched the original mnemonic from the old system.

Next, I modified showtopic.asp to permanently redirect to a tag search for the mnemonic requested, so any existing external topic links will be unbroken. Thanks to Steven Hargrove for providing the most excellent way to do this in ASP, as well as just about any other web language). Then I redirected default.asp to the main page, chip.asp to the “About” page, and resources.asp to the “Resources” page. I’m leaving topics.asp alone, because attempting to map from the old categories to the new proved too daunting. So, any external links to a general topic will still show the old outline page, but clicking on a link to a topic there will take you to the corresponding page on the new site.

Last but not least, I converted licensing to the CCD CopyWrite license, which I have also adopted for Chip’s Quips. No big change there from Creative Commons Attribution – you’re free to copy and reuse this content to your heart’s content, provided you give appropriate credit to yours truly.

Enjoy, and please feel free to comment!

Posted in ASP, PHP, Web, WordPress, XML, XSLT | 7 Comments » RSS 2.0 | Sphere it!

Escaping special characters in generated XML data

June 21st, 2001 2:22:52 pm pst by Sterling Camden

When generating XML data, you must be careful not to include special characters that will drive the parsers crazy. These are, as defined in section 2.4 (“Character Data and Markup”) of Extensible Markup Language (XML) 1.0 (Second Edition), “&”, “<", and ">“, which must be replaced by the escape sequences “&”, “<”, and “>”, respectively.

The downloadable code below includes separate examples in Synergy/DE and C++ of how this conversion can be performed. The Synergy/DE version is implemented as a function that converts the string in place. The C++ version implements a class, an instance of which can be assigned a string value that is immediately converted on assignment. An extractor allows the converted string to be retrieved.

Posted in C and C++, OpenVMS, SynergyDE, Unix, Wildly popular, Windows | No Comments » RSS 2.0 | Sphere it!

Passing parameters to XSLT

June 12th, 2001 11:24:18 am pst by Sterling Camden

XSLT provides an abstract and flexible means of formatting XML data for output. Going well beyond the concept of “style sheets”, XSLT contains powerful elements that almost qualify it as a full-blown programming language. The xsl:call-template in conjunction with xsl:with-param and xsl:param tags even allows for encapsulation and reuse of common routines with parameters. Sometimes, however, the parameters you need lie outside the XML documents in the external operating environment. For instance, when generating web pages it may be useful to have access to the parameters passed on the HTTP request.

While XML and XSLT do not provide a direct means of accessing external information, you can pass parameters to a transformation by adding nodes to one of the documents prior to performing the transformation. The downloadable example below shows how this can be done in an ASP, using the Microsoft DOM parser to load the XML documents, add the parameters as elements in the data document, and then perform the transformation to generate an HTML page.

To test the example, you will need to copy the files to an Internet Information Server (IIS) or Personal Web Server, and you must have installed the Microsoft DOM parser on the server. Click here for the link to Microsoft’s page for downloading the parser.

Once you have everything set up on the server, request the testparams.asp page in a browser. If the parameters contain “show=desc”, then the full description of each node of the testparams.xml document will be displayed. Otherwise, only the name of each node is displayed. All parameters passed to the page are displayed with their values at the top of the page. Try it with different parameters to see the results.

Posted in ASP, Web, Wildly popular, XML, XSLT | No Comments » RSS 2.0 | Sphere it!

Better Tag Cloud