eneylon

Binding back

In general on March 27, 2011 at 7:54 am

The last article discussed how to convert XML content to bind rendered content to its source – by carrying references back to the originating document using an XSLT transformation. To make use of that data in the client (generally being a web browser), we need an interaction mechanism. HTML is already self-aware of its structure through the DOM, and so a user interaction can be easily captured in relation to some rendered content.

The following example demonstrates how simple it is to get JavaScript to make HTML aware of its own existence (existential HTML):

<html>
<head>
<script type="text/javascript">
function locate(e){
alert('You pressed button '+e.button+' in the element with id of value '+e.srcElement.getAttribute('id'));
}
</script>
</head>
<body onMouseDown="locate(event)">
<div id="first">Click on this text to see that a <span id="second">different context</span> can be recognised <span id="third">even when nested <sup id="fourth">inside</sup> another context</span>.</div>
</body>

Clicking in the different areas of the text in the document results in different messages being displayed based on the context of the mouse in relation to the ids used in the document. By extension if every html element contained an XPath back to source (or a hash key for an XPath stored on the server for efficiency’s sake), then every presentational structure can be tied back to the semantic structure of the originating document. It’s up to the transformation writer to ensure that those structures that need relating back to the original are passed through.

<div source="/doc/annex/section[5]/paragraph[3]">Allows us to point to  particular structure in the source document</div>

Since XPaths can be arbitarily long, it might be more efficient to use a hash on the XPath and do a lookup on the server side if content is sent back (for example for adding annotations to a particular node in the XML document structure). This approach has been used to allow users to comment on data conversion quality by having jQuery popup a menu when the user right clicks some content.

Annotation Correlation

In general on June 18, 2010 at 9:06 pm

When structured content is converted for presentation, the relationship between source and rendition is often lost.

Increasingly documents are being made available not just for reading, but also for writing. Wikis allow editing of content from a raw state, but the bulk of annotation (for example in consultation exercises) still needs moderation or processing before affecting the source document. So the publication of transformed documents for annotation is a legitimate model for soliciting input to those documents.

The problem comes when the comments need to be tied back to the source. Lossy transformations are common when documents are converted into HTML. Reversing a transformation is not often a design consideration and the semantics of source tags are often lost when rendering content.

What is needed is a means of commenting on a presentation form that allows annotation at the precision of the source document. The approach described here assumes the source is an XML document that is transformed for display to the reader.

In order to be able to tie items in the HTML to their corresponding elements in the source document, each element in the source must be uniquely represented in the rendition. The approach advocated is to insert the XPath for each node in the source in a attribute of the rendered content. It is proposed that this attribute be named noid.

Of course there are other ways of achieving the same result: such as creating a lookup table of XPaths and giving each node a corresponding guid. However the direct approach of inserting the XPath in the attribute has the advantage of simplicity, not needing another data structure, and transparency. One disadvantage is the increase in file size becomes a function of content structure and element naming rather than just the number of nodes in the source.

The solution uses XSLT to transform the source. This allows for easy extensibility and placement in a transformation pipeline. The code below enhances the identity transform by adding a new attribute to every element output. That attribute contains the XPath of the element that it corresponds to:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:attribute name="noid">
<xsl:for-each select="ancestor-or-self::*">
<xsl:variable name="my-key-name"><xsl:value-of select="local-name(.)"/></xsl:variable>
<xsl:text>/</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>[</xsl:text>
<xsl:value-of select="1+count(preceding-sibling::*[local-name(.)=$my-key-name])"/>
<xsl:text>]</xsl:text>
</xsl:for-each>
</xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

The additional code performs a path trace which takes the current node and calculates the route that is needed to access that particular node in the source.

The result of documents transformed with this is an identical document which can be used to render HTML and provides a means back to the source for every element. Any subsequent processing can choose to make use of those links. This would typically by using the noid attribute to populate the id on a div or span element in HTML.

This post has shown that an XML document can be transformed to provide a route back to the source document in a subsequent rendition. In the next post in this series I will cover how to make use of that path from the rendered document using JavaScript events and the HTML document object model.

Event Horizon

In meetings on April 4, 2010 at 3:59 pm

Going to, and looking forward to, events has always been important to me. On those occasions that I glimpsed the possibilities of groups drawn together by a common interest, something ignited inside.

The first regular events, that I elected to participate in, were sports-related. Living close to Highbury Stadium, before the spate of incidents that forced football stadia to control access, provided ample opportunity to indulge in the fix of fixtures. Back then, we [friends] could get in free to the stadium at half time. Jostling towards any confrontations that were brewing hinted at other possibilities from these gatherings, and provided relief from the boring football on offer.

Days going to Twickenham were memorable. The adventure was in travelling to strange parts of London. We could slip the gate a few pounds to get into the (equally boring) rugby internationals of the early-eighties. Even venturing to White Hart Lane (to watch more interesting football) was just a precursor for the passion that was to follow.

The epiphany happened one summer in Ireland. Our family went every year, and the summer of 1983 looked set to be no different to any other. Until the day an older cousin asked if I wanted to go along to a festival in Dublin. I was 15 and bored at being stuck in the countryside: and so jumped at the opportunity. Off I went to my first gig – Anvil, Motorhead, Twister Sister and Black Sabbath at Dalymount Stadium.

That gig changed my life. Back in London, I dragged my best friend to a virtuoso performance by Ritchie Blackmore’s Rainbow. We lost half a stone in sweat each that night – and he wound up working for a record label. It wasn’t just me that was caught up in the possibilities of the event experience.

The next ten years were focussed on getting to gigs. Being much more focussed on urban gigs than the festival scene, I developed many strategies around getting access to live music. It wasn’t necessarily expensive: getting to know the bouncers at the university venue was rewarding in many dimensions.

With age, it became less urgent to be at the right gigs. And then it started feeling akward – being at he front of the crowd was no longer important, and the sense that something important would be missed was plainly mistaken. The wilderness years occurred when my daughter was born. Going out was less important. Being there for her is what is important. But time passes, and we all move on ….

The passion was rekindled with the organization of a few tech events – things I’d like to see happen and couldn’t wait for someone else to organise (such as XMLOpen in 2004). Then I took my daughter to her first gig (Yassou N’Dour in Lucca, Italy). Suddenly I was reminded of what it was that had enthralled me for so long.

Then unconferences became commonplace. Suddenly I could indulge my interests without having to feel indebited to an employer. Perhaps not as focussed as some of the paid conferences I’ve been fortunate to attend; they nonetheless provide opportunity to discover what is really important to you. I’ve learned more about myself at these events than at weeks focussing on obscure technologies in distant countries.

So now I spend much more time thinking about how I’m spending time. Events are as important to me as ever, but I’m no longer looking to recreate the last buzz. What’s important now are the opportunities afforded: to learn new approaches, meet new people, and to think new thoughts. The type of event is just a constraint – and one that can be deconstructed by those attending.

I’ve just booked tickets for Womad in Charlton Park this July and would love to hear from anyone interested in doing something a bit different while we are there.

Follow

Get every new post delivered to your Inbox.