{"id":139,"date":"2007-02-17T13:15:41","date_gmt":"2007-02-17T18:15:41","guid":{"rendered":"\/cgi\/wp\/?p=139"},"modified":"2007-02-18T19:57:48","modified_gmt":"2007-02-19T00:57:48","slug":"xml-xsl-livevideo-api-2","status":"publish","type":"post","link":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=139","title":{"rendered":"XML &#038; XSL &#8211; LiveVideo API"},"content":{"rendered":"<p>Yesterday I created a web part to show video comments using the <strong>LiveVideo<\/strong> 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.<\/p>\n<p>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&#8217;t in a very readable format. Third, I wanted to alternate the table row colors.<\/p>\n<p>For the first problem involving the image, I found the solution at <a title=\"TopXML\" href=\"http:\/\/www.topxml.com\/xsl\/tutorials\/intro\/xsl8a.asp\">TopXML<\/a>. The exact syntax gave me a lot of problems because you should not use the <strong>@src<\/strong> they show.<\/p>\n<p><code><font color=\"#0000ff\"><<\/font><font color=\"#800000\">img<\/font><font color=\"#0000ff\">><\/font><br \/>\n<font color=\"#0000ff\"><<\/font><font color=\"#800000\">xsl:attribute<\/font> <font color=\"#0000ff\">name=\"src\"<\/font><font color=\"#0000ff\">><\/font><br \/>\n<font color=\"#0000ff\"><<\/font><font color=\"#800000\">xsl:value-of<\/font> <font color=\"#0000ff\">select=\"profileThumbnail\"<\/font> <font color=\"#0000ff\">\/><\/font><br \/>\n<font color=\"#0000ff\"><<\/font><\/code><font color=\"#0000ff\"><code>\/<\/code><code><font color=\"#800000\">xsl:attribute<\/font><font color=\"#0000ff\">><\/font><br \/>\n<<\/code><code>\/<\/code><code><font color=\"#800000\">img<\/font><font color=\"#0000ff\">><\/font><\/code><\/font><\/p>\n<p>For the second problem involving the date format, I found the solution at <font color=\"#0000ff\"><a title=\"John Workman's weblog\" href=\"http:\/\/geekswithblogs.net\/workdog\/archive\/2007\/02\/08\/105858.aspx\">John Workman's weblog<\/a><\/font>. 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.<\/p>\n<p><code><font color=\"#0000ff\"><<\/font><font color=\"#800000\">xsl:call-template<\/font> name=\"FormatDate\"<font color=\"#0000ff\">><\/font><br \/>\n<font color=\"#0000ff\"><<\/font><font color=\"#800000\">xsl:with-param<\/font> name=\"DateTime\" select=\"entryDate\"<font color=\"#0000ff\">\/><br \/>\n...................<\/font><font color=\"#0000ff\"><br \/>\n<\/font><<code>\/<\/code><font color=\"#800000\">xsl:call-template<\/font><font color=\"#0000ff\">><\/font><\/code><\/p>\n<p>For the third problem involving alternating table row background colors, I found the solution at <font color=\"#0000ff\"><a title=\"William Pohlhaus' Web Site\" href=\"http:\/\/www17.homepage.villanova.edu\/william.pohlhaus\/xsl\/mod\/\">William Pohlhaus&#8217; Web Site<\/a><\/font>. It is a neat trick using the XML element position and the math function mod.<\/p>\n<p><code><font color=\"#0000ff\"><<font color=\"#800000\">xsl:if<\/font> test=\"position() mod 2 = 1\"<font color=\"#0000ff\">><br \/>\n<\/font><font color=\"#0000ff\"><<\/font><font color=\"#800000\">tr<\/font> bgcolor=\"#EFF3FB\"<font color=\"#0000ff\">><\/font><br \/>\n<<code>\/<\/code><font color=\"#800000\">xsl:if<\/font><font color=\"#0000ff\">><\/font><br \/>\n<font color=\"#0000ff\"><<\/font><\/font><font color=\"#800000\">xsl:if<\/font> test=&#8221;position() mod 2 = 0&#8243;><br \/>\n<font color=\"#0000ff\"><<\/font><font color=\"#800000\">tr<\/font> bgcolor=&#8221;#FFFFFF&#8221;<font color=\"#0000ff\">><\/font><br \/>\n<font color=\"#800000\"><font color=\"#0000ff\"><<\/font><code>\/<\/code><font color=\"#800000\">xsl:if<\/font><font color=\"#0000ff\">><\/font><\/font><\/code><\/p>\n<p>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:<\/p>\n<table id=\"table1\" bgcolor=\"#eeeeee\">\n<tr>\n<td valign=\"top\"><code><span style=\"color: #000000\"><span style=\"color: #0000bb\"><!--p<--><\/span><span style=\"color: #ff8000\">\/\/ Load the XML source<br \/>\n<\/span><span style=\"color: #0000bb\">$xml <\/span><span style=\"color: #007700\">= new <\/span><span style=\"color: #0000bb\">DOMDocument<\/span><span style=\"color: #007700\">;<br \/>\n<\/span><span style=\"color: #0000bb\">$xml<\/span><span style=\"color: #007700\">-><\/span><span style=\"color: #0000bb\">load<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #dd0000\">'LiveVideo.xml'<\/span><span style=\"color: #007700\">);<\/span><\/span><span style=\"color: #007700\"><span style=\"color: #0000bb\">$xsl <\/span><span style=\"color: #007700\">= new <\/span><span style=\"color: #0000bb\">DOMDocument<\/span><span style=\"color: #007700\">;<br \/>\n<\/span><span style=\"color: #0000bb\">$xsl<\/span><span style=\"color: #007700\">-><\/span><span style=\"color: #0000bb\">load<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #dd0000\">'LiveVideo.xsl'<\/span><span style=\"color: #007700\">);<\/span> <\/span><\/code><br \/>\n<span style=\"color: #007700\"><code style=\"color: #007700\"><code><span style=\"color: #ff8000\">\/\/ Configure the transformer<br \/>\n<\/span><span style=\"color: #0000bb\">$proc <\/span><span style=\"color: #007700\">= new <\/span><span style=\"color: #0000bb\">XSLTProcessor<\/span><span style=\"color: #007700\">;<br \/>\n<\/span><span style=\"color: #0000bb\">$proc<\/span><span style=\"color: #007700\">-><\/span><span style=\"color: #0000bb\">importStyleSheet<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000bb\">$xsl<\/span><span style=\"color: #007700\">); <\/span><\/code><br \/>\n<code><span style=\"color: #ff8000\">\/\/ attach the xsl rules<\/span><span style=\"color: #ff8000\"><br \/>\n<\/span><span style=\"color: #007700\">echo <\/span><span style=\"color: #0000bb\">$proc<\/span><span style=\"color: #007700\">-><\/span><span style=\"color: #0000bb\">transformToXML<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000bb\">$xml<\/span><span style=\"color: #007700\">);<\/span><span style=\"color: #007700\"><span style=\"color: #0000bb\">?><\/span> <\/span><span style=\"color: #007700\"><span style=\"color: #007700\"><span style=\"color: #007700\"><code style=\"color: #007700\" \/><\/span><\/span><\/span><\/code><\/code><\/span><\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/?p=139\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/139"}],"collection":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=139"}],"version-history":[{"count":0,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=\/wp\/v2\/posts\/139\/revisions"}],"wp:attachment":[{"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/williamsportwebdeveloper.com\/cgi\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}