Monday, December 22, 2008

XSLT note 6 - fast search node

We can make search much faster if we generate index. We generate index by using xsl:key.

<?xml version='1.0'?> <?xml-stylesheet type="text/xsl" href="key_sample.xsl" ?> <titles> <book title="XML Today" author="David Perry"/> <book title="XML and Microsoft" author="David Perry"/> <book title="XML Productivity" author="Jim Kim"/> </titles> <?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <!-- create index for book based on @author --> <xsl:key name="title-search" match="book" use="@author"/> <xsl:template match="/"> <HTML> <BODY> <!-- use index title-search to search book where index value = 'David Perry' --> <xsl:for-each select="key('title-search', 'David Perry')"> <div> <xsl:value-of select="@title"/> </div> </xsl:for-each> </BODY> </HTML> </xsl:template> </xsl:stylesheet>

No comments:

Post a Comment