Blog

2008 posts (36)

Pages: < Previous 1–10 11–20 21–30 31–40 Next >
Ordering: Ascending Descending

1. Grouping in XSLT

2008-12-16 15:34:15 by Martynas Jusevičius

Together with joining, grouping is one of the most used techniques in XSLT, at least in my practice. Unfortunately, there is no native support in XSLT 1.0 (there is <xsl:for-each-group> construct in XSLT 2.0).
However, grouping can be implemented in XSLT 1.0 as well using the Muenchian method. For that you will need a key to compute values by which elements will be grouped, and (as I do it) one template to handle the whole group of elements, and one template to handle each element, single entry in the group.

Suppose we have a list of keywords used in a book, containing the text itself and the number of the page it appears on:

<keywords>
	<keyword>
		<text>XML</text>
		<page>25</page>
	</keyword>
	<keyword>
		<text>PHP</text>
		<page>10</page>
	</keyword>
	<keyword>
		<text>XSLT</text>
		<page>30</page>
	</keyword>
	<keyword>
		<text>W3C</text>
		<page>2</page>
	</keyword>
</keywords>

Now, as it is common in publishing, we want to use them to build an aplhabetically-grouped index of keywords to be added to the appendix of the book.
First we define a key, which assigns each keyword a value equal to the first letter of its text (and can be used to select keywords starting with the same letter):

<xsl:key name="keywords-by-letter" match="keyword" use="substring(text, 1, 1)"/>

Then, a group template which displays the current letter and a list of keywords starting with it:

<xsl:template match="keyword" mode="group">
	<xsl:variable name="letter" select="substring(text, 1, 1)"/>
	<h1>
		<xsl:value-of select="$letter"/>
	</h1>
	<ul>
		<xsl:apply-templates select="key('keywords-by-letter', $letter)" mode="entry"/>
	</ul>
</xsl:template>

And a template to show a single keyword as a list item (notice how mode is different from the group template):

<xsl:template match="keyword" mode="entry">
	<li>
		<xsl:value-of select="text"/>, page <xsl:value-of select="page"/>
	</li>
</xsl:template>

Finally, we can put this all to use with a master template which applies templates for each keyword group, or in other words, for each unique starting letter found in the keyword list:

<xsl:template match="keywords">
	<html>
		<head>...</head>
		<body>
			<xsl:apply-templates select="keyword[generate-id(.) = generate-id(key('keywords-by-letter', substring(text, 1, 1))[1])]" mode="group">
				<xsl:sort select="text"/>
			</xsl:apply-templates>
		</body>
	</html>
</xsl:template>

XML and XSLT will be both grouped under X.

Add a comment Comments (1572)

2. W3C released mobileOK validator

2008-12-09 12:14:22 by Martynas Jusevičius

W3C released mobileOK checker, which is intended to help developers created clean websites suited for mobile devices and help them reach mobile community, supporting strategy that the Web should be accessible on a variety of heterogeneous clients.

Among other things, mobileOK validator seems to analyze:

This blog seems to be 78% mobileOK already :)

Add a comment Comments (3)

3. Method overloading in PHP 5

2008-12-02 13:21:47 by Martynas Jusevičius

Method overloading (a feature of object-oriented programing which allows having several class methods with the same name but different signatures) is not implemented in PHP, which is a drawback compared to Java.
However, PHP 5 provides a way to imitate overloading by catching calls to inaccessible methods with magic method __call.

Here is an example with constructor overloading (idea taken from DZone Snippets):

class YearResource extends TimeIntervalResource
{
	public function __construct() // dispatches calls to specific constructors
	{
		$num = func_num_args();
		$args = func_get_args();
		if ($num == 0) $this->__call('__construct0', null);
		if ($num == 1) $this->__call('__construct1', $args);
	}

	private function __call($name, $arg) // calls a user function given with an array of parameters
	{
		return call_user_func_array(array($this, $name), $arg);
	}

	public function __construct0() // no parameter constructor
	{
		parent::__construct();
		$this->setType(FrontEndResourcePeer::CLASSKEY_YEARRESOURCE);
	}

	public function __construct1(Year $year) // single parameter constructor
	{
		parent::__construct(YearListResource::getInstance());
		$this->setType(FrontEndResourcePeer::CLASSKEY_YEARRESOURCE);
		$this->setRelativeURI(rawurlencode($year->getStartDateTime(null)->format("Y")));
		$year->setFrontEndResource($this);
	}
}

Now the YearResource can be constructed with one parameter or none at all:

$resource = new YearResource($year);
$resource = new YearResource();

Note that in parent classes you might have to use self::__call instead of $this->__call to get the desired effect.

Add a comment Comments (1232)

4. Query interfaces for the Semantic Web

2008-11-26 01:12:05 by Martynas Jusevičius

An interesting presentiation at Google Tech Talks about different interfaces to query semantic data.
Casual users were presented with 4 increasingly formal systems: keyword search, natural language search, controlled language search, and a graphical interface to build query patterns. Interestingly enough, the users liked natural language best, although keyword queries gave more accurate results.

Add a comment Comments (2)

5. Portable Web framework

2008-11-17 12:06:43 by Martynas Jusevičius

I've been working more with Java than PHP recently, using a Java port of the DIY Framework. So effectively it is a portable framework now, at least to some extent — it is a matter of language syntax and model layer to move a Web applicaton between Java and PHP.

Not that this is going to be extensively used killer feature. But I think it is a positive outcome of our Web framework design: it has to be based on HTTP and REST principles and concepts. Since they are uniform for all Web applications, such a framework makes them portable.

We think it is essential to incorporate HTTP at the heart of the design, and in the DIY Framework an URI-adressed Resource is an actual class. It is also for the same reason we don't like or use third-party frameworks, since instead of HTTP, they are built on concepts taken out of the blue. Symfony structures an application as modules and actions and throws a mess of routing on top. Who said that a Web application must have structure like that? Apache Struts uses some abstract notion of action and tons of configuration. ASP.NET goes even further and pretends it is used to build event-driven Windows applications and not Web applications of request/response nature.

Add a comment Comments (1)

6. REST webservice for public data in Denmark

2008-10-06 11:19:01 by Martynas Jusevičius

This is pretty old news dating back to this spring, but the Danish IT and Telecommunications board (IT- og Telsestyrelsen) has published an experimental REST interface OIOREST for public data such as regions, municipalities, post codes, schools, streets, and addresses. And example of a query identifying a street: http://oiorest.dk/danmark/veje?q=frankrigsgade.

OIOREST is a great initiative, and we hope it will be expanded to other datasources. There is one big concern though — it is not employing established data standards but instead using its own custom XML schemas.
What they should really consider is publishing the data as RDF in the Linked Data manner. That would make Denmark one of the pioneers not only in REST adoption, but also in the Semantic Web.

Add a comment Comments (12)

7. Computing path of a node in a hierarchy

2008-09-16 13:21:05 by Martynas Jusevičius

One of the outstanding issues in the updated (so far internally) version of the DIY Framework, which now keeps resources in a hierarchy, is how to effectively calculate the full URIs of resources. For this post, it would be Blog/2008/09/16/Computing+path+of+a+node+in+a+hierarchy, where the post resource is a leaf node with a depth of 5. In general terms, the question is how to calculate path of a node.

Propel's nested sets implementation for some reason calculates the path by recursively traversing from the node to its parent and so on until the root is reached, although this can be done using one SQL query. But even one query per resource is not good enough, since usually one page would use tens or even hundreds of resource objects.

In order to improve performance and not to compute the URI on every load, we left the full URI property for resources as it was, in addition to the new relative (to parent resource) URI property, and properties needed to implement the nested sets. So the question is, how to keep the full URI up-to-date all the time with resource's position in the tree? It certainly needs to change when one of the ascendant (parent, parent of the parent etc.) resources, or the resource in question itself, is moved or updated. But that might also involve a chain reaction since full URIs of descendant resources would also need to change, and they might be in numbers of hundreds or thousands. Definitely a performance problem, especially if done on the PHP level. I was thinking about implementing this with SQL triggers somehow, but it doesn't seem to be so trivial. Anyone has ideas?

Add a comment Comments (5)

8. Google Chrome

2008-09-03 12:33:54 by Martynas Jusevičius

For those who missed it, the new Google Chrome browser is already available for download. Appeared almost out of blue sky, hopefully it will help to further push the market share of Internet Explorer.
The beta version seems pretty stable and solid. There are also reports that its new V8 JavaScript engine (built in Aarhus, Denmark) is multiple times faster than those in other browsers.

Add a comment Comments (7)

9. Resource tree

2008-08-20 14:23:19 by Martynas Jusevičius

We recently internally redesigned our framework framework. Now the resource objects form a parent/child tree, where each resource is a node and contains its URI relative to the parent (instead of full URI relative to the host, as previously). The hierarchy was implemented using nested set support in Propel, which is way more efficient than the ID/parentID method that requires recursion and lots of database queries. The tree of our page would be:

This means that now resources can be treated as files and folder in a filesystem — copied/moved from one URI to another, renamed, given permissions etc. A sitemap of the site can be generated automatically. Of course, as before, they can be related to domain model objects which they stand for.

Add a comment Comments (5)

10. REST authentication?

2008-08-16 16:17:04 by Martynas Jusevičius

While developing a RESTful webservice, I realised I need some kind of authentication to only allow clients to execute certain HTTP methods on certain resources. For example, a client should only be able to able to GET/PUT its own placemarks at /placemarks/client1 but not others at /placemarks/client2.
In a regular Web application this would be solved in a simple way using sessions. But one of the REST principles is statelessness, which eliminates cookies and sessions. So this might be an old question, but what is the common authentication method between a client and a RESTful webservice? Is HTTP Basic/Digest enough here?

Add a comment Comments (35)

Pages: < Previous 1–10 11–20 21–30 31–40 Next >
Ordering: Ascending Descending