Created
December 30, 2011 01:46
-
-
Save greystate/1537207 to your computer and use it in GitHub Desktop.
XSLT for transforming the XML from twitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8" ?> | |
| <xsl:stylesheet | |
| version="1.0" | |
| xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
| xmlns:umb="urn:umbraco.library" | |
| xmlns:make="urn:schemas-microsoft-com:xslt" | |
| exclude-result-prefixes="umb make" | |
| > | |
| <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> | |
| <xsl:variable name="tweets" select="/statuses/status" /><!-- grab using document() function - e.g.: document('TWEETS_URL')/statuses/status --> | |
| <xsl:variable name="twitter_root_url" select="'http://twitter.com'" /> | |
| <xsl:template match="/"> | |
| <xsl:apply-templates select="$tweets" /> | |
| </xsl:template> | |
| <!-- Main work-horse --> | |
| <xsl:template match="status"> | |
| <!-- Grab text to use in multiple places --> | |
| <xsl:variable name="text" select="text" /> | |
| <!-- Sort entities by @start in text --> | |
| <xsl:variable name="sortedEntitiesProxy"> | |
| <xsl:for-each select="entities//*[@start]"> | |
| <xsl:sort select="@start" data-type="number" order="ascending" /> | |
| <xsl:copy-of select="." /> | |
| </xsl:for-each> | |
| </xsl:variable> | |
| <xsl:variable name="sortedEntities" select="make:node-set($sortedEntitiesProxy)/*" /> | |
| <!-- Uncomment this next line to double check against plain (orig.) version --> | |
| <!-- <p class="original"><xsl:value-of select="$text" /></p> --> | |
| <p class="tweet"> | |
| <!-- Any text before the first entity --> | |
| <xsl:value-of select="substring($text, 1, $sortedEntities[1]/@start)" /> | |
| <!-- Handle entities (and intermingled text) --> | |
| <xsl:for-each select="$sortedEntities"> | |
| <!-- Grab the position (in sorted set) --> | |
| <xsl:variable name="index" select="position()" /> | |
| <!-- Grab the previous (preceding-sibling:: won't work in this set) --> | |
| <xsl:variable name="previous" select="$sortedEntities[$index - 1]" /> | |
| <!-- Text between this and the previous entity --> | |
| <xsl:value-of select="substring($text, $previous/@end + 1, current()/@start - $previous/@end)" /> | |
| <!-- Handle the specific entity --> | |
| <xsl:apply-templates select="." /> | |
| </xsl:for-each> | |
| <!-- Any text after the last entity --> | |
| <xsl:value-of select="substring($text, $sortedEntities[last()]/@end + 1, 300)" /> | |
| </p> | |
| </xsl:template> | |
| <!-- Special case for a plain tweet with no specials... --> | |
| <xsl:template match="status[not(normalize-space(entities))]"> | |
| <p class="tweet plain"> | |
| <xsl:value-of select="text" /> <!-- (should maybe use disable-output-escaping...) --> | |
| </p> | |
| </xsl:template> | |
| <!-- :: Plain templates for the misc. entities :: --> | |
| <!-- User mention (e.g.: @warrenbuckley)--> | |
| <xsl:template match="user_mention"> | |
| <a href="{$twitter_root_url}/{screen_name}" title="{name}"><xsl:value-of select="concat('@', screen_name)" /></a> | |
| </xsl:template> | |
| <!-- Hash tag (e.g. #xslt) --> | |
| <xsl:template match="hashtag"> | |
| <a href="{$twitter_root_url}/search?q=%23{text}" title="#{text}"><xsl:value-of select="concat('#', text)" /></a> | |
| </xsl:template> | |
| <!-- URL --> | |
| <xsl:template match="url"> | |
| <a href="{expanded_url}" title="{expanded_url}"><xsl:value-of select="display_url" /></a> | |
| </xsl:template> | |
| <!-- Image --> | |
| <xsl:template match="creative[type = 'photo']"> | |
| <a href="{media_url}" title="{display_url}"><img src="{media_url}:thumb" width="{sizes/thumb/w}" height="{sizes/thumb/h}" /></a> | |
| </xsl:template> | |
| <!-- Flag any entity we don't have a template for yet --> | |
| <xsl:template match="*" priority="-1"> | |
| <span style="background:#ff8000;color:#000;"><xsl:value-of select="concat('<', name(), ' />')" /></span> | |
| </xsl:template> | |
| </xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment