Position Function
"Position" function do returns the number equal to context position and "Last"
function do returns the number equal to context size from a expression evaluation
context. XSL stylesheet 1 demonstrates the use of these two functions in several of the
contexts.
A D V E R T I S E M E N T
XML Source
<source>
<AAA>
<BBB>
<CCC>Carl</CCC>
</BBB>
<BBB/>
<BBB/>
</AAA>
<AAA>
<BBB/>
<BBB>
<CCC>John</CCC>
<CCC>Charles</CCC>
<CCC>Robert</CCC>
<CCC>Anthony</CCC>
</BBB>
</AAA>
</source>
Output
<DIV>BBB(1/5)(2/5)(3/5)(4/5)(5/5)</DIV>
<DIV>CCC(1/5)(2/5)(3/5)(4/5)(5/5)</DIV>
<DIV>CCC(1/4)(2/4)(3/4)(4/4)</DIV>
XSL stylesheet
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<DIV>
<xsl:for-each select="//BBB">
<xsl:call-template name="printout"/>
</xsl:for-each>
</DIV>
<DIV>
<xsl:apply-templates select="//CCC"/>
</DIV>
<DIV>
<xsl:apply-templates select="//AAA[last()]//CCC"/>
</DIV>
</xsl:template>
<xsl:template match="CCC">
<xsl:call-template name="printout"/>
</xsl:template>
<xsl:template name="printout">
<xsl:if test="position()=1">
<xsl:value-of select="name()"/>
</xsl:if>
<xsl:text>(</xsl:text>
<xsl:value-of select="position()"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="last()"/>
<xsl:text>)</xsl:text>
</xsl:template>
</xsl:stylesheet>
Count Function
A count function returns number of nodes present in an argument node-set.
XML Source
<source>
<AAA>
<CCC/>
<BBB>
<CCC>Carl</CCC>
</BBB>
<BBB/>
<BBB/>
</AAA>
<AAA>
<CCC/>
<BBB/>
<BBB>
<CCC>John</CCC>
<CCC>Charles</CCC>
<CCC>Robert</CCC>
<CCC>Anthony</CCC>
</BBB>
</AAA>
</source>
Output
<DIV>
<B>//AAA : </B>2</DIV>
<DIV>
<B>//CCC : </B>7</DIV>
<DIV>
<B>//AAA/CCC : </B>2</DIV>
<DIV>
<B>//CCC[text()]) : </B>5</DIV>
XSL stylesheet
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<DIV>
<B>
<xsl:text>//AAA : </xsl:text>
</B>
<xsl:value-of select="count(//AAA)"/>
</DIV>
<DIV>
<B>
<xsl:text>//CCC : </xsl:text>
</B>
<xsl:value-of select="count(//CCC)"/>
</DIV>
<DIV>
<B>
<xsl:text>//AAA/CCC : </xsl:text>
</B>
<xsl:value-of select="count(//AAA/CCC)"/>
</DIV>
<DIV>
<B>
<xsl:text>//CCC[text()]) : </xsl:text>
</B>
<xsl:value-of select="count(//CCC[text()])"/>
</DIV>
</xsl:template>
</xsl:stylesheet>
ID Function
An "id" function selects the elements by their ID which is unique. The XSL stylesheet 1
shows a simple examples of its use.
XML Source
<!DOCTYPE source [
<!ELEMENT chapter ANY>
<!ATTLIST chapter id ID #REQUIRED>
<!ELEMENT title ANY>
<!ATTLIST title id CDATA #REQUIRED>
<!ELEMENT text ANY>
<!ATTLIST text value ID #REQUIRED>
]>
<source>
<chapter id="intro">Introduction</chapter>
<chapter id="body">
<title id="t1">BODY</title>
<text value="text1">text text text</text>
</chapter>
<chapter id="end">THE END</chapter>
</source>
Output
<P>Introduction</P>
<P>text text text</P>
<P>text text text</P>
XSL stylesheet
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<P>
<xsl:value-of select="id('intro')"/>
</P>
<P>
<xsl:value-of select="id('body')/text"/>
</P>
<P>
<xsl:value-of select="id('text1')"/>
</P>
</xsl:template>
</xsl:stylesheet>
Local-name & Namespace-uri Function
The "local-name" and "namespace-uri()" are been used to get the informations about
the element and attribute names and the namespaces.
XML Source
<source xmlns:zvon="http://zvon.org/" xmlns:ict="XXX" >
<zvon:id>ZvonAAA</zvon:id>
<zvon:size>258</zvon:size>
<ict:id>ICT1258</ict:id>
</source>
Output
<TABLE border="1">
<TR>
<TH>name</TH>
<TH>local</TH>
<TH>URI</TH>
</TR>
<TR>
<TD>source</TD>
<TD>source</TD>
<TD/>
</TR>
<TR>
<TD>zvon:id</TD>
<TD>id</TD>
<TD>http://zvon.org/</TD>
</TR>
<TR>
<TD>zvon:size</TD>
<TD>size</TD>
<TD>http://zvon.org/</TD>
</TR>
<TR>
<TD>ict:id</TD>
<TD>id</TD>
<TD>XXX</TD>
</TR>
</TABLE>
XSL stylesheet
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<TABLE border="1">
<TR>
<TH>name</TH>
<TH>local</TH>
<TH>URI</TH>
</TR>
<xsl:apply-templates select="//*"/>
</TABLE>
</xsl:template>
<xsl:template match="*">
<TR>
<TD>
<xsl:value-of select="name()"/>
</TD>
<TD>
<xsl:value-of select="local-name()"/>
</TD>
<TD>
<xsl:value-of select="namespace-uri()"/>
</TD>
</TR>
</xsl:template>
</xsl:stylesheet>
Be the first one to comment on this page.
Share And Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords xsl sort, xsl element, xsl xpath, xsl examples, xsl example, xsl param, xsl select,
xsl template, xsl syntax, xsl text, xpath node, xsl parameter, xsl javascript,
xsl document, xsl id, xsl date, select function, javascript node, string xsl,
element node, xsl stylesheet, value xsl, javascript function, node attributes,
name node, value function, node child, node examples, expression function, xsl tag,
function variables, document node, variable function, text node, node attribute,
string function, select node, node tag, text function, xsl using