Monday, 14 May 2012

xsl:number

Input:-

<?xml version="1.0" encoding="UTF-8"?>
<number-format>
    <number>1</number>
    <number>2</number>
    <number>3</number>
    <number>4</number>
    <chapter-no>1</chapter-no>
    <chapter-no>23</chapter-no>
    <chapter-no>3</chapter-no>
    <chap-no>1</chap-no>
    <chap-no>23</chap-no>
    <chap-no>3</chap-no>
</number-format>


Output:-



<number-format>
    <number>1st</number>
    <number>2nd</number>
    <number>3rd</number>
    <number>4th</number>
    <chapter-no>First</chapter-no>
    <chapter-no>Twenty-Third</chapter-no>
    <chapter-no>Third</chapter-no>
    <chap-no>One</chap-no>
    <chap-no>Twenty Three</chap-no>
    <chap-no>Three</chap-no>
</number-format>

XSLT CODE:-


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
   
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="number-format">
        <xsl:element name="number-format">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="number">
        <xsl:copy>
            <!--TO RETURN AS <number>1st</number>-->
            <xsl:number value="." format="1" ordinal="yes"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="chapter-no">
        <xsl:element name="chapter-no">
            <!--TO RETURN AS <chapter-no>First</chapter-no>-->
            <xsl:number value="." format="Ww" ordinal="yes"/>          
        </xsl:element>
    </xsl:template>

    <xsl:template match="chap-no">
        <xsl:element name="chap-no">
            <!--TO RETURN AS<chap-no>One</chap-no>-->
            <xsl:number value="." format="Ww"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>



Monday, 7 May 2012

XSLT Function


XSLT Function
I want to implement a custom XSLT function to filter a list of people on the basis of age.


INPUT.XML----------

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE person_list SYSTEM "validator.dtd">

<person_list>
<person id="1" name="John Smith">
<age>35</age>
<height units="cm">173</height&amp;gt;
<weight units="kg">75</weight&amp;gt;
</person>
<person id="2" name="Tom Stone">
<age>26</age>
<height units="cm">177</height&amp;gt;
<weight units="kg">65</weight&amp;gt;
</person>
</person_list>


FUNCTION.XSL
----------
Try this function which would take two parameter as in input.

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform&quot;
xmlns:my="http://www.mysite.com/my&amp;quot;&gt;

<xsl:function name="my:age">
<xslaram name="ageRequired"/>
<xslaram name="ageCurrent"/>
<xsl:if test="$ageCurrent > $ageRequired">
The current age is <xsl:value-of select="$ageCurrent"/>
</xsl:if>
</xsl:function>

<xsl:template match="/">
<xsl:for-each select="person_list/person"&amp;gt;
<xsl:value-of select="my:age(18, age)"/>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>