XML & XSL – LiveVideo API

Yesterday I created a web part to show video comments using the LiveVideo API. The API returns XML so I had to format it using XSL (Extensible Stylesheet Language) which is a stylesheet for XML. I used Visual Studio 2005 Standard Edition. Unfortunately, the XSLT editor/debugger features are only included in the Professional and higher levels of Visual Studio 2005. Since many web services and APIs (Application Programming Interface) return XML, I thought it was worth the time to explore transforming XML through XSL.

I ran into three problems requiring solutions. First, I had to display an image using the web address for the image found in an XML tag. Second, I had to format a date that wasn’t in a very readable format. Third, I wanted to alternate the table row colors.

For the first problem involving the image, I found the solution at TopXML. The exact syntax gave me a lot of problems because you should not use the @src they show.

<img>
<xsl:attribute name="src">
<xsl:value-of select="profileThumbnail" />
<
/xsl:attribute>
<
/img>

For the second problem involving the date format, I found the solution at John Workman's weblog. You need to use another XSL template to format a date. I had to edit his FormatDate template because it did not format the date as I wanted it.

<xsl:call-template name="FormatDate">
<xsl:with-param name="DateTime" select="entryDate"/>
...................

</xsl:call-template>

For the third problem involving alternating table row background colors, I found the solution at William Pohlhaus’ Web Site. It is a neat trick using the XML element position and the math function mod.

<xsl:if test="position() mod 2 = 1">
<tr bgcolor="#EFF3FB">
</xsl:if>
<
xsl:if test=”position() mod 2 = 0″>
<tr bgcolor=”#FFFFFF”>
</xsl:if>

After solving those problems for my web part I explored XSLT using PHP. Unfortunately, my web hosting company does not appear to have any of the required extensions installed to support XSLT in PHP. I have PHP 5.0 installed on my local web server and I found the following code works:

// Load the XML source
$xml = new DOMDocument;
$xml->load('LiveVideo.xml');
$xsl = new DOMDocument;
$xsl->load('LiveVideo.xsl');

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);

// attach the xsl rules
echo $proc->transformToXML($xml);?>
This entry was posted in General. Bookmark the permalink.

Leave a Reply

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

Time limit exceeded. Please complete the captcha once again.