XSLT

October 25, 2010

XSLT Date and Time Processing Examples

Date and time processing

Posted by pj at 12:40 PM | Comments (0)

June 01, 2009

XSL for transforming RSS 2.0 to RSS 1.0

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" id="rss2to1"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                xmlns:dc="http://purl.org/dc/elements/1.1/"
                xmlns="http://purl.org/rss/1.0/"
                >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/rss"><xsl:apply-templates/></xsl:template>
<xsl:template match="channel">
<rdf:RDF>
    <channel rdf:about="{link}">
    <title><xsl:value-of select="title"/></title>  
    <description><xsl:value-of select="description"/></description>  
    <link><xsl:value-of select="link"/></link>    
    <items>
        <rdf:Seq>
            <xsl:for-each select="item"><rdf:li  rdf:resource="{link}"/></xsl:for-each>
        </rdf:Seq>
    </items>
   </channel>
    <xsl:for-each select="item">
    <item rdf:about="{link}">
        <title><xsl:value-of select="title"/></title>  
        <description><xsl:value-of select="description"/></description>  
        <link><xsl:value-of select="link"/></link>
        <dc:date><xsl:call-template name="FormatDate"><xsl:with-param name="DateTime"  select="pubDate"/></xsl:call-template></dc:date>
    </item>
    </xsl:for-each>
    </rdf:RDF>
</xsl:template>
  <xsl:template name="FormatDate">
    <xsl:param name="DateTime" />
    <!-- new date format 2006-01-14T08:55:22Z -->
    <xsl:variable name="mo">
      <xsl:value-of select="substring($DateTime,9,3)" />
    </xsl:variable>
    <xsl:variable name="day">
      <xsl:value-of select="substring($DateTime,6,2)" />
    </xsl:variable>
    <xsl:variable name="year">
      <xsl:value-of select="substring($DateTime,13,4)" />
    </xsl:variable>
    <xsl:variable name="time">
      <xsl:value-of select="substring($DateTime,18,8)" />
    </xsl:variable>
    <xsl:variable name="hh">
      <xsl:value-of select="substring($time,1,2)" />
    </xsl:variable>
    <xsl:variable name="mm">
      <xsl:value-of select="substring($time,4,2)" />
    </xsl:variable>
    <xsl:variable name="ss">
      <xsl:value-of select="substring($time,7,2)" />
    </xsl:variable>
    <xsl:value-of select="$year"/>
    <xsl:value-of select="'-'"/>
    <xsl:choose>
      <xsl:when test="$mo = 'Jan'">01</xsl:when>
      <xsl:when test="$mo = 'Feb'">02</xsl:when>
      <xsl:when test="$mo = 'Mar'">03</xsl:when>
      <xsl:when test="$mo = 'Apr'">04</xsl:when>
      <xsl:when test="$mo = 'May'">05</xsl:when>
      <xsl:when test="$mo = 'Jun'">06</xsl:when>
      <xsl:when test="$mo = 'Jul'">07</xsl:when>
      <xsl:when test="$mo = 'Aug'">08</xsl:when>
      <xsl:when test="$mo = 'Sep'">09</xsl:when>
      <xsl:when test="$mo = 'Oct'">10</xsl:when>
      <xsl:when test="$mo = 'Nov'">11</xsl:when>
      <xsl:when test="$mo = 'Dec'">12</xsl:when>
    </xsl:choose>

    <xsl:value-of select="'-'"/>
    <xsl:if test="(string-length($day) < 2)">
      <xsl:value-of select="0"/>
    </xsl:if>
    <xsl:value-of select="$day"/>
    <xsl:value-of select="'T'"/>
    <xsl:value-of select="$hh"/>
    <xsl:value-of select="':'"/>
    <xsl:value-of select="$mm"/>
    <xsl:value-of select="':'"/>
    <xsl:value-of select="$ss"/>
    <xsl:value-of select="'Z'"/>
  </xsl:template>

</xsl:stylesheet>

Posted by pj at 09:02 PM | Comments (0)

XSL for formatting a date

XSLT - format date

Posted by pj at 04:02 PM | Comments (0)

July 21, 2008

XSLT for Amazon AWS book search to JSON

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" id="amazon2json"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:aws="http://webservices.amazon.com/AWSECommerceService/2008-06-26"
                >
    <xsl:output method="text" indent="yes"/> 
    <xsl:strip-space elements="*" /> 
    <xsl:template match="/aws:ItemSearchResponse">
        {
        <xsl:apply-templates/>
        }
    </xsl:template>
    <xsl:template match="aws:Items">
        <xsl:for-each select="aws:Item">
            "<xsl:value-of select="aws:ASIN"/>" : {
            "isbn" : "<xsl:value-of select="aws:ASIN"/>",
            <xsl:if test="aws:ItemAttributes/aws:Author">"authors" : [
            <xsl:for-each select="aws:ItemAttributes/aws:Author">
                "<xsl:value-of select="."/>"
                <xsl:if test="position() != last()">
                <xsl:text>,</xsl:text>
                </xsl:if>
            </xsl:for-each>
                ],</xsl:if>
            <xsl:if test="aws:ItemAttributes/aws:Author">
            "author" : "<xsl:for-each select="aws:ItemAttributes/aws:Author">
                <xsl:value-of select="."/>
                <xsl:if test="position() != last()">
                <xsl:text>, </xsl:text>
                </xsl:if>
            </xsl:for-each>",</xsl:if>
            "publisher" : "<xsl:value-of select="aws:ItemAttributes/aws:Manufacturer"/>",
            "title" : "<xsl:value-of select="aws:ItemAttributes/aws:Title"/>",
            "url" : "<xsl:value-of select="aws:DetailPageURL"/>",
            "price" : "<xsl:value-of select="aws:ItemAttributes/aws:ListPrice/aws:FormattedPrice"/>",
            "format" : "<xsl:value-of select="aws:ItemAttributes/aws:Binding"/><xsl:text> </xsl:text><xsl:value-of select="aws:ItemAttributes/aws:ProductGroup"/>",
            "publication_date" : "<xsl:value-of select="aws:ItemAttributes/aws:PublicationDate"/>",
            "edition" : "<xsl:value-of select="aws:ItemAttributes/aws:Edition"/>",
            "thumbnail" : "<xsl:value-of select="aws:MediumImage/aws:URL"/>"
            }
            <xsl:if test="position() != last()">
            <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="aws:OperationRequest">
    </xsl:template>
    <xsl:template match="aws:ItemLinks">
    </xsl:template>
</xsl:stylesheet>

Posted by pj at 03:21 PM | Comments (0)

July 19, 2006

JSP / XSLT combo

The following is an XSLT sheet dynamically generated from JSP and demonstrating use of <xsl:sort/> and batching:

<xsl:sort select="metadata/dc:title"
order="ascending"
data-type="text"/>
<xsl:if test="position() >= ($page * $pagesize) + 1">
<xsl:if test="position() <= $pagesize + ($pagesize * $page)">
<xsl:apply-templates/>
</xsl:if>
</xsl:if>

keys.xsl.jsp

Posted by pj at 10:06 AM | Comments (0)