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>

No comments: