Showing posts with label xslt. Show all posts
Showing posts with label xslt. Show all posts

Saturday, November 24, 2012

Extracting CDATA section using XSLT

In a XML message, we pass some data which we might not want to be parsed by xml parsers.
Characters like "<>" are illegal in XML elements.  To save such characters we use CDATA section in our xml message.
For instance in the following xml message, we use CDATA block to pass <metadata> to other end without parsing "<,>" signs.
eg:


    <ser:getArtifactContentResponse    xmlns:ser="http://services.generic.governance.carbon.wso2.org">
    <ser:return>
         <![CDATA[
   <metadata xmlns="http://www.wso2.org/governance/metadata">
    <overview>       
        <name>scannapp&lt;/name>
        <developerId>developer702&lt;/developerId>
        <stateId>2&lt;/stateId>
        <serverURL>http://abc.com&lt;/serverURL>
        <id>cspapp1103&lt;/id>
        <description>scann doc&lt;/description>
        <hostingTypeId>1&lt;/hostingTypeId>       
    </overview>
</metadata>
  ]]>
  </ser:return>
</ser:getArtifactContentResponse>

 
To extract the CDATA section,( here it contains xml message) we could use simple xslt script.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" xmlns:ns="http://services.generic.governance.carbon.wso2.org">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" />
    <xsl:template match="/">
        <xsl:value-of select="//ns:getArtifactContentResponse/ns:return/text()" disable-output-escaping="yes"/>
    </xsl:template>
</xsl:stylesheet>
The output would be ;

   <metadata xmlns="http://www.wso2.org/governance/metadata">
    <overview>       
        <name>scannapp</name>
        <developerId>developer702</developerId>
        <stateId>2</stateId>
        <serverURL>http://abc.com</serverURL>
        <id>cspapp1103</id>
        <description>scann doc</description>
        <hostingTypeId>1</hostingTypeId>       
    </overview>
</metadata>

Friday, August 17, 2012

Excluding namespaces in XSLT

When we write a complex xslt script, we need to introduce functions, templates etc.. XSLT function needs namespace definition. But we may not need those namespaces in the output.
There is an extra attribute we need to define in our xslt script to avoid those additional namespaces,which are not to be present in our output.

eg:
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:func="http://www.function.com" exclude-result-prefixes="func">
Here if you note that, i have added an attribute(ie: exclude-result-prefixes) to the style-sheet to avoid my additional namespace ,which is  http://www.function.com .

Tuesday, August 7, 2012

Debugging XSLT script

To know what is the output of a XSLT function or variable we can simply use <xsl:message> to do a print statement in the xslt transfromation script.

Eg:  In the following template when we do the substring operation we might need to know, what is the input we are getting in our template.

<xsl:template name="FindPhoneNum">
    <xsl:param name="DigitsLength" />
    <xsl:param name="PhoneNum" />
    <xsl:message>
        PhoneNum---------:
        <xsl:value-of select="$PhoneNum" />
        DigitsLength---------:
        <xsl:value-of select="$DigitsLength" />
    </xsl:message>   
    <xsl:variable name="number">
        <xsl:value-of select="substring($PhoneNum, $DigitsLength -6, $DigitsLength)" />
    </xsl:variable>
    <xsl:value-of select="$number" />
</xsl:template>
Output will be like;

PhoneNum---------: 18168918984
DigitsLength---------: 11

Friday, February 10, 2012

Implementing incremental logic in XSLT

I faced difficult time to implement the incremental logic for xslt 1.0 today..My requirement was to printout consequent numbers upto a certain value.
Here is my sample segment xslt code for this..

  <xsl:element name="Numbers">
                              <xsl:for-each select="//flight_info">
                              <xsl:if test="position()= last()">
                                  <xsl:call-template name="incrementValue">
                                      <xsl:with-param name="i"  select="1"/>                                                                    
                                     <xsl:with-param name="pos" select="position()"/>
                                </xsl:call-template>
                                </xsl:if>                                     
                             </xsl:for-each>
     </xsl:element>


//This template does the trick, which contains the recursive call to the same template..

    <xsl:template name="incrementValue">
        <xsl:param name="i" />
        <xsl:param name="pos" />

        <xsl:if test="$i <= $pos">
            <xsl:value-of select="concat($i,' ')" />
            <xsl:call-template name="incrementValue">
                <xsl:with-param name="i" select="$i + 1" />
                <xsl:with-param name="pos" select="$pos" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

Monday, February 6, 2012

Date conversion function in XSLT

Recently, i found a need to convert one format to different date formats using XSLT script..This is a sample script i wrote to convert      Thu Jun 07 18:00:00 EDT 2012 --to--> 2012-06-07T18:00:00  which may be useful to others also...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   
    <!-- Thu Jun 07 18:00:00 EDT 2012 to 2012-02-12T118:00:00 -->
    <xsl:template match="/">
   
        <xsl:element name="PickUpDateTime">
            <xsl:call-template name="FormatDate">
                <xsl:with-param name="DateTime" select="//date" />
            </xsl:call-template>
        </xsl:element>
   
    </xsl:template>
   
    <xsl:template name="FormatDate">
        <xsl:param name="DateTime" />   
        <!-- Thu Jun 07 18:00:00 EDT 2012 to 2012-02-12T118:00:00 -->
        <xsl:variable name="day">
            <xsl:value-of select="substring($DateTime,1,3)" />
        </xsl:variable>
       
        <xsl:variable name="month-temp">
            <xsl:value-of select="substring-after($DateTime,' ')" />
        </xsl:variable>       
        <xsl:variable name="month">
            <xsl:value-of select="substring($month-temp,1,3)" />
        </xsl:variable>       
       
        <xsl:variable name="date-temp">
            <xsl:value-of select="substring-after($month-temp,' ')" />
        </xsl:variable>
        <xsl:variable name="date">
            <xsl:value-of select="substring($date-temp,1,2)" />
        </xsl:variable>       
       
        <xsl:variable name="year">
            <xsl:value-of select="substring-after($DateTime,'EDT ')" />
        </xsl:variable>           
       
        <xsl:variable name="time">
            <xsl:value-of select="substring-after($date-temp,' ')" />
        </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="$month = 'Jan'">01</xsl:when>
            <xsl:when test="$month= 'Feb'">02</xsl:when>
            <xsl:when test="$month = 'Mar'">03</xsl:when>
            <xsl:when test="$month = 'Apr'">04</xsl:when>
            <xsl:when test="$month = 'May'">05</xsl:when>
            <xsl:when test="$month = 'Jun'">06</xsl:when>
            <xsl:when test="$month = 'Jul'">07</xsl:when>
            <xsl:when test="$month = 'Aug'">08</xsl:when>
            <xsl:when test="$month = 'Sep'">09</xsl:when>
            <xsl:when test="$month = 'Oct'">10</xsl:when>
            <xsl:when test="$month = 'Nov'">11</xsl:when>
            <xsl:when test="$month = 'Dec'">12</xsl:when>
        </xsl:choose>

        <xsl:value-of select="'-'" />
   
        <xsl:value-of select="$date" />
        <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:template>
</xsl:stylesheet>