BusinessWire.com

Sample XSL Template/NewsML/XHTML to HTML

<?xml version="1.0" encoding="UTF-8" ?>

- <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xhtml" version="1.0">

- <!--

 ****************************************************************     

Real simple XLS Template to convert a Story in NEWSML / XHTML into an HTML Document             

This source code is provided free of charge and placed in the public domain.

This program is meant to demonstrate one possible way to  implement the conversion of Business Wire NewsML with XHTML content into an HTML document. It is not supported by Business Wire.   Use this software at your own risk.             

$RCSfile: NEWSML_W_HTML_TO_HTML_EXAMPLE.xsl,v $                         

$ Last modified by $Author: jaysonl $                                                 

$Date: 2011/04/18 21:53:18 $                            

$Revision: 1.4 $                                               

$copyright 2007(c) Business Wire       

$author Jayson Lorenzen   $

 ******************************************************************

  -->

  <xsl:output omit-xml-declaration="yes" method="xml" indent="no" encoding="UTF-8" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" />

- <!--

 ********************************** VARIABLES  *****************************************

  -->

- <!--

 ADD A RETURN

  -->

  <xsl:variable name="NEWLINE" select="' '" />

- <xsl:variable name="PRODUCT">

  <xsl:apply-templates select="//NewsEnvelope/NewsProduct/@FormalName" />

  </xsl:variable>

- <xsl:variable name="PROVIDERID">

  <xsl:apply-templates select="//NewsItem/Identification/NewsIdentifier/ProviderId" />

  </xsl:variable>

- <xsl:variable name="DATELINE_CITY">

  <xsl:apply-templates select="//NewsItem/NewsComponent/NewsComponent/NewsLines/DateLine" />

  </xsl:variable>

- <xsl:variable name="SOURCE">

  <xsl:apply-templates select="//NewsItem/NewsComponent/AdministrativeMetadata/Source/Party/@FormalName" />

  </xsl:variable>

- <xsl:variable name="HEADLINE">

  <xsl:apply-templates select="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='HeadLine']/ContentItem[Format/@FormalName='XHTML']/DataContent/xhtml:html/xhtml:body" />

  </xsl:variable>

- <xsl:variable name="CSS">

  <xsl:apply-templates select="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='StyleSheet']/ContentItem[Format/@FormalName='CSS']/DataContent" />

  </xsl:variable>

- <!--

 WEB STYLES

  -->

- <xsl:variable name="CSS_EXTRA">

  <xsl:text>body {font:normal small Arial,Helvetica,sans-serif;color:#000;background-color:#fff;padding:24px;margin:0;} a img {border:0;} h3 {font-size:medium;color:#000;margin:0 0 1em 0; text-align:center;}</xsl:text>

  </xsl:variable>

- <!--

 END VARIABLES

  -->

- <!--

 CATCH ALL

  -->

- <xsl:template match="/">

  <xsl:apply-templates select="//NewsItem" />

  </xsl:template>

- <!--

 MAIN TEMPLATE

  -->

- <!--

 NEWSITEM 

  -->

- <xsl:template match="//NewsItem">

- <xsl:element name="html">

- <xsl:element name="head">

- <!--

 ADD A RETURN

  -->

  <xsl:value-of select="$NEWLINE" />

- <!--

 ADD CONTENT-TYPE META FOR IE

  -->

  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

- <!--

 ADD A RETURN

  -->

  <xsl:value-of select="$NEWLINE" />

- <!--

 META_KEYWORDS

  -->

- <xsl:element name="meta">

  <xsl:attribute name="name">keywords</xsl:attribute>

- <xsl:attribute name="content">

  <xsl:apply-templates select="//NewsComponent/Metadata[MetadataType/@FormalName='BWKeywords']/Property" />

  </xsl:attribute>

  </xsl:element>

- <!--

 ADD A RETURN

  -->

  <xsl:value-of select="$NEWLINE" />

- <!--

 META_CREATED

  -->

- <xsl:element name="meta">

  <xsl:attribute name="name">ThisRevisionCreated</xsl:attribute>

- <xsl:attribute name="content">

  <xsl:value-of select="//NewsItem/NewsManagement/ThisRevisionCreated" />

  </xsl:attribute>

  </xsl:element>

- <!--

 ADD A RETURN

  -->

  <xsl:value-of select="$NEWLINE" />

- <!--

 META_ID

  -->

- <xsl:element name="meta">

  <xsl:attribute name="name">NewsItemID</xsl:attribute>

- <xsl:attribute name="content">

  <xsl:value-of select="//NewsItem/Identification/NewsIdentifier/NewsItemId" />

  </xsl:attribute>

  </xsl:element>

- <!--

 ADD A RETURN

  -->

  <xsl:value-of select="$NEWLINE" />

- <!--

 META_REVISION

  -->

- <xsl:element name="meta">

  <xsl:attribute name="name">RevisionId</xsl:attribute>

- <xsl:attribute name="content">

  <xsl:value-of select="//NewsItem/Identification/NewsIdentifier/RevisionId" />

  </xsl:attribute>

  </xsl:element>

- <!--

 ADD A RETURN

  -->

  <xsl:value-of select="$NEWLINE" />

- <xsl:element name="title">

  <xsl:value-of select="//NewsItem/NewsComponent/NewsComponent/NewsLines/HeadLine" />

  </xsl:element>

- <!--

 ADD A RETURN

  -->

  <xsl:value-of select="$NEWLINE" />

- <xsl:if test="$CSS">

- <!--

 ADD A RETURN

  -->

  <xsl:value-of select="$NEWLINE" />

- <xsl:element name="style">

  <xsl:attribute name="type">text/css</xsl:attribute>

- <xsl:comment>

- <!--

 RELEASE STYLES

  -->

  <xsl:value-of select="$CSS" />

- <!--

 EXTRA STYLES

  -->

  <xsl:value-of select="$CSS_EXTRA" />

  </xsl:comment>

  </xsl:element>

- <!--

 ADD A RETURN

  -->

  <xsl:value-of select="$NEWLINE" />

  </xsl:if>

  </xsl:element>

- <!--

 ADD A RETURN

  -->

  <xsl:value-of select="$NEWLINE" />

- <xsl:element name="body">

- <!--

 HEADLINE

  -->

  <xsl:apply-templates select="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='HeadLine']/ContentItem[Format/@FormalName='XHTML']/DataContent/xhtml:html/xhtml:body" />

- <!--

 SUB HEADLINE

  -->

  <xsl:apply-templates select="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='SubHeadLine']/ContentItem[Format/@FormalName='XHTML']/DataContent/xhtml:html/xhtml:body" />

- <!--

 BODY

  -->

  <xsl:apply-templates select="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='Body']/ContentItem[Format/@FormalName='XHTML']/DataContent/xhtml:html/xhtml:body | NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='Summary']/ContentItem[Format/@FormalName='XHTML']/DataContent/xhtml:html/xhtml:body" />

- <!--

 CONTACT

  -->

  <xsl:element name="br" />

  <xsl:element name="br" />

  <xsl:apply-templates select="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='Contact']/ContentItem[Format/@FormalName='XHTML']/DataContent/xhtml:html/xhtml:body" />

- <!--

 KEYWORDS

  -->

- <xsl:element name="p">

- <xsl:element name="b">

  <xsl:text>KEYWORDS:</xsl:text>

  </xsl:element>

  <xsl:apply-templates select="//NewsItem/NewsComponent/Metadata[MetadataType/@FormalName='BWKeywords']/Property[@FormalName='BWCountryKeywords'] | //NewsItem/NewsComponent/Metadata[MetadataType/@FormalName='BWKeywords']/Property[@FormalName='BWRegionKeywords'] | //NewsItem/NewsComponent/Metadata[MetadataType/@FormalName='BWKeywords']/Property[@FormalName='BWStateKeywords']" />

  </xsl:element>

- <xsl:element name="p">

- <xsl:element name="b">

  <xsl:text>INDUSTRY KEYWORDS:</xsl:text>

  </xsl:element>

  <xsl:apply-templates select="NewsComponent/Metadata[MetadataType/@FormalName='BWKeywords']/Property[@FormalName='BWIndustryKeywords']" />

  </xsl:element>

- <xsl:element name="p">

- <xsl:element name="b">

  <xsl:text>MEDIA:</xsl:text>

  </xsl:element>

  </xsl:element>

- <table cellpadding="3" cellspacing="3">

  <xsl:apply-templates select="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='Logo'] | //NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='Photo']" />

  </table>

  </xsl:element>

  </xsl:element>

  </xsl:template>

- <!--

 HEADLINE

  -->

- <xsl:template match="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='HeadLine']/ContentItem[Format/@FormalName='XHTML']/DataContent/xhtml:html/xhtml:body">

- <xsl:for-each select="child::*">

- <xsl:element name="{local-name(.)}">

  <xsl:copy-of select="@*" />

  <xsl:apply-templates mode="recurCopyNoNamespace" />

  </xsl:element>

  </xsl:for-each>

  </xsl:template>

- <!--

 SUB HEADLINE

  -->

- <xsl:template match="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='SubHeadLine']/ContentItem[Format/@FormalName='XHTML']/DataContent/xhtml:html/xhtml:body">

- <xsl:for-each select="child::*">

- <xsl:element name="{local-name(.)}">

  <xsl:copy-of select="@*" />

  <xsl:apply-templates mode="recurCopyNoNamespace" />

  </xsl:element>

  </xsl:for-each>

  </xsl:template>

- <!--

 BODY 

  -->

- <xsl:template match="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='Body']/ContentItem[Format/@FormalName='XHTML']/DataContent/xhtml:html/xhtml:body | NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='Summary']/ContentItem[Format/@FormalName='XHTML']/DataContent/xhtml:html/xhtml:body">

- <xsl:for-each select="child::*">

- <xsl:element name="{local-name(.)}">

  <xsl:copy-of select="@*" />

- <xsl:choose>

- <xsl:when test="name()='p' and position()=1">

  <xsl:value-of select="$DATELINE_CITY" />

  <xsl:text>--(</xsl:text>

- <xsl:element name="a">

- <xsl:attribute name="href">

  <xsl:value-of select="concat('http://www.',$PROVIDERID)" />

  </xsl:attribute>

  <xsl:value-of select="$PRODUCT" />

  </xsl:element>

  <xsl:text>)--</xsl:text>

  <xsl:apply-templates mode="recurCopyNoNamespace" />

  </xsl:when>

- <xsl:otherwise>

  <xsl:apply-templates mode="recurCopyNoNamespace" />

  </xsl:otherwise>

  </xsl:choose>

  </xsl:element>

  </xsl:for-each>

  </xsl:template>

- <!--

 CONTACT

  -->

- <xsl:template match="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='Contact']/ContentItem[Format/@FormalName='XHTML']/DataContent/xhtml:html/xhtml:body">

- <xsl:for-each select="child::*">

- <xsl:element name="{local-name(.)}">

  <xsl:copy-of select="@*" />

  <xsl:apply-templates mode="recurCopyNoNamespace" />

  </xsl:element>

  </xsl:for-each>

  </xsl:template>

- <!--

 KEYWORDS

  -->

- <xsl:template match="NewsComponent/Metadata[MetadataType/@FormalName='BWKeywords']/Property[@FormalName='BWCountryKeywords'] | //NewsItem/NewsComponent/Metadata[MetadataType/@FormalName='BWKeywords']/Property[@FormalName='BWRegionKeywords'] | //NewsItem/NewsComponent/Metadata[MetadataType/@FormalName='BWKeywords']/Property[@FormalName='BWStateKeywords']">

  <xsl:text> </xsl:text>

  <xsl:value-of select="@Value" />

  </xsl:template>

- <!--

 INDUSTRY KEYWORDS

  -->

- <xsl:template match="NewsComponent/Metadata[MetadataType/@FormalName='BWKeywords']/Property[@FormalName='BWIndustryKeywords']">

  <xsl:text> </xsl:text>

  <xsl:value-of select="@Value" />

  </xsl:template>

- <!--

 PHOTO AND LOGO

  -->

- <xsl:template match="//NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='Logo'] | //NewsItem/NewsComponent/NewsComponent/NewsComponent[Role/@FormalName='Photo']">

- <tr>

- <td>

- <font face="Arial" size="2">

- <b>

  <xsl:if test="Role/@FormalName = 'Photo'">Photo</xsl:if>

  <xsl:if test="Role/@FormalName = 'Logo'">Logo</xsl:if>

  </b>

  </font>

  </td>

  </tr>

- <tr>

- <td>

- <xsl:element name="img">

- <xsl:attribute name="src">

- <xsl:choose>

- <xsl:when test="ContentItem[Characteristics/Property[@FormalName='ImageUse']/@Value = 'Viewimage']/@Href">

  <xsl:value-of select="ContentItem[Characteristics/Property[@FormalName='ImageUse']/@Value = 'Viewimage']/@Href" />

  </xsl:when>

- <xsl:otherwise>

  <xsl:value-of select="ContentItem[Characteristics/Property[@FormalName='ImageUse']/@Value = 'Thumbnail']/@Href" />

  </xsl:otherwise>

  </xsl:choose>

  </xsl:attribute>

- <xsl:attribute name="alt">

  <xsl:if test="Role/@FormalName = 'Photo'">Photo</xsl:if>

  <xsl:if test="Role/@FormalName = 'Logo'">Logo</xsl:if>

  </xsl:attribute>

  </xsl:element>

  </td>

  </tr>

- <tr>

- <td>

- <font face="Arial" size="2">

  <xsl:value-of select="NewsLines/HeadLine" />

  </font>

  </td>

  </tr>

  </xsl:template>

- <!--

 SPECIAL TEMPLATE TO REMOVE UNWANTED NAMESPACE

  -->

- <xsl:template match="*" mode="recurCopyNoNamespace">

- <xsl:element name="{name()}">

  <xsl:copy-of select="@*" />

  <xsl:apply-templates select="*|text()|comment()" mode="recurCopyNoNamespace" />

  </xsl:element>

  </xsl:template>

- <!--

 SPECIAL COPY TEMPLATE TO REMOVE UNWANTED NAMESPACE (USED WITH ABOVE)

  -->

- <xsl:template match="text()|comment()" mode="recurCopyNoNamespace">

- <xsl:copy>

  <xsl:copy-of select="@*" />

  <xsl:apply-templates select="*|text()|comment()" mode="recurCopyNoNamespace" />

  </xsl:copy>

  </xsl:template>

  </xsl:stylesheet>