Miscellaneous Additional Functions
The Current Function
A "current function" do returns the node-set that has current node as the only member.
For the outermost expression, expression which is not occurring within another
expression, Current node is always same as a context node. However, within the square
brackets current node is usually different from context node.
A D V E R T I S E M E N T
XML source
<?xml version="1.0"?>
<xslTutorial >
<AAA name="first">
<BBB name="first">11111</BBB>
<BBB name="second">22222</BBB>
</AAA>
<AAA name="second">
<BBB name="first">33333</BBB> ;
<BBB name="second">44444</BBB>
</AAA>
</xslTutorial>
HTML output 1
<TABLE border="1">
<TR>
<TH> . </TH>
<TH>current()</TH></TR>
<TR>
<TD>first</TD>
<TD>first</TD></TR>
<TR>
<TD>11111</TD>
<TD>1111122222</TD></TR>
<TR>
<TD>second</TD>
<TD>second</TD></TR>
<TR>
<TD>33333</TD>
<TD/></TR></TABLE>
XSL stylesheet 1
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<TABLE border="1">
<TR><TH> . </TH><TH>current()</TH></TR>
<xsl:apply-templates select="//AAA"/>
</TABLE>
</xsl:template>
<xsl:template match="AAA">
<TR>
<TD>
<xsl:value-of select="./@name"/>
</TD><TD>
<xsl:value-of select="current()/@name"/>
</TD></TR>
<TR><TD>
<xsl:apply-templates select="BBB[./@name='first']"/>
</TD><TD>
<xsl:apply-templates select="BBB[current()/@name='first']"/>
</TD></TR>
</xsl:template>
</xsl:stylesheet>
Generate Id
The function generate-id generates the id conforming to the XML spec. The Stylesheet
2 does uses the generate-id function to add the id to all the elements in the source XML.
XML source
<?xml version="1.0"?>
<xslTutorial >
<AAA name='top'>
<BBB pos='1' val='bbb'>11111</BBB>
<BBB>22222</BBB>
</AAA>
<AAA name='bottom'>
<BBB>33333</BBB>
<BBB>44444</BBB>
</AAA>
</xslTutorial>
HTML output 1
<DIV>
<B>generate-id(//AAA) : </B>N3</DIV>
<DIV>
<B>generate-id(//BBB) : </B>N6</DIV>
<DIV>
<B>generate-id(//AAA[1]) : </B>N3</DIV>
<DIV>
<B>generate-id(//*[1]) : </B>N1</DIV>
<DIV>
<B>generate-id(//xslTutorial/*[1]) : </B>N3</DIV>
XSL stylesheet 1
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >
<xsl:template match="/">
<DIV><B><xsl:text> generate-id(//AAA) : </xsl:text> </B>
<xsl:value-of select="generate-id(//AAA) "/></DIV>
<DIV><B><xsl:text> generate-id(//BBB) : </xsl:text> </B>
<xsl:value-of select="generate-id(//BBB) "/></DIV>
<DIV><B><xsl:text> generate-id(//AAA[1]) : </xsl:text> </B>
<xsl:value-of select="generate-id(//AAA[1]) "/></DIV>
<DIV><B><xsl:text> generate-id(//*[1]) : </xsl:text> </B>
<xsl:value-of select="generate-id(//*[1]) "/></DIV>
<DIV><B><xsl:text> generate-id(//xslTutorial/*[1]) : </xsl:text> </B>
<xsl:value-of select="generate-id(//xslTutorial/*[1]) "/></DIV>
</xsl:template>
</xsl:stylesheet>
XML source
<?xml version="1.0"?>
<xslTutorial >
<AAA name='top'>
<BBB pos='1' val='bbb'>11111</BBB>
<BBB>22222</BBB>
</AAA>
<AAA name='bottom'>
<BBB>33333</BBB>
<BBB>44444</BBB>
</AAA>
</xslTutorial>
HTML output 2
<xslTutorial id="N1">
<AAA id="N3" name="top">
<BBB id="N6" pos="1" val="bbb">11111</BBB>
<BBB id="N11">22222</BBB> </AAA>
<AAA id="N15" name="bottom">
<BBB id="N18">33333</BBB>
<BBB id="N21">44444</BBB> </AAA> </xslTutorial>
XSL stylesheet 2
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="*">
<xsl:copy select=".">
<xsl:attribute name="id">
<xsl:value-of select="generate-id()"/>
</xsl:attribute>
<xsl:for-each select="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/ >
</xsl:copy>
</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 Miscellaneous Additional Functions, XSL Functions, xsl string functions, xsl date functions,
xpath functions, xslt function, xsl function, xslt functions, xpath function, xslt string functions,
xpath string functions, xslt document function, xpath 1.0 functions, xsl document function,
xsl string function, functions in xsl, xpath 2.0 functions, xsl replace function,
xpath contains function, xpath count function, xslt date functions, xslt date function,
xslt replace function, xslt string function, functions in xslt, xsl date function, xsl count function