XML | 質問トピック | 22 回答例

あ、key 使ったほうが高速かも。[あとで試す]

むー、この場合 key 使うのは難しそうだな……。経験則だから BK かもだけど、XSLT プロセッサによっては key 付けとくだけでも速くなったりする。この例だと とかやる。

あれ、variable で select したら、もちろんキャッシュされるよね? ね? 自信満々でできますって回答しちゃったけど……。

グローバル変数の場合

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method='html'
		encoding='utf-8'
		doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
		indent='yes'/>
	<xsl:param name='query_keyword'/>
	<xsl:param name="query_state"/>
	<xsl:param name="query_category"/>
	<xsl:param name="query_page"/>

	<xsl:variable name="result" select="/catalog/pr[
		($query_keyword='' or contains(title, $query_keyword))
		and ($query_state='all' or state=$query_state)
		and ($query_category='all' or cat=$query_category)]"/>

	<xsl:template match="/">
		<title>_</title>
		<xsl:value-of select="count($result)"/>
		<table frame="border" rules="all">
			<xsl:apply-templates select="$result"/>
		</table>
	</xsl:template>

	<xsl:template match="catalog/pr">
		<xsl:if test="$query_page='all' or position()&lt;=$query_page">
			<tr>
				<td align="center"><xsl:value-of select="position()"/></td>
				<td><xsl:value-of select="id"/></td>
				<td><xsl:value-of select="name"/></td>
				<td><xsl:value-of select="state"/></td>
				<td><xsl:value-of select="cat"/></td>
			</tr>
		</xsl:if>
	</xsl:template>

</xsl:stylesheet>

with-param で渡す場合

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method='html'
		encoding='utf-8'
		doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
		indent='yes'/>
	<xsl:param name='query_keyword'/>
	<xsl:param name="query_state"/>
	<xsl:param name="query_category"/>
	<xsl:param name="query_page"/>

	<xsl:template match="/">
		<xsl:variable name="result" select="catalog/pr[
			($query_keyword='' or contains(title, $query_keyword))
			and ($query_state='all' or $query_state=state)
			and ($query_category='all' or cat=$query_category)]"/>
		<title>_</title>
		<xsl:value-of select="count($result)"/>
		<table frame="border" rules="all">
			<xsl:call-template name="rows">
				<xsl:with-param name="result" select="$result"/>
			</xsl:call-template>
		</table>
	</xsl:template>

	<xsl:template name="rows">
		<xsl:param name="result"/>
		<xsl:for-each select="$result">
			<xsl:if test="$query_page='all' or position()&lt;=$query_page">
				<tr>
					<td align="center"><xsl:value-of select="position()"/></td>
					<td><xsl:value-of select="id"/></td>
					<td><xsl:value-of select="name"/></td>
					<td><xsl:value-of select="state"/></td>
					<td><xsl:value-of select="cat"/></td>
				</tr>
			</xsl:if>
		</xsl:for-each>
	</xsl:template>

</xsl:stylesheet>

と言うか、この場合 call-template 要らない……でもまあ with-param の例ということで……。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method='html'
		encoding='utf-8'
		doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
		indent='yes'/>
	<xsl:param name='query_keyword'/>
	<xsl:param name="query_state"/>
	<xsl:param name="query_category"/>
	<xsl:param name="query_page"/>

	<xsl:template match="/">
		<xsl:variable name="result" select="catalog/pr[
			($query_keyword='' or contains(title, $query_keyword))
			and ($query_state='all' or $query_state=state)
			and ($query_category='all' or cat=$query_category)]"/>
		<title>_</title>
		<xsl:value-of select="count($result)"/>
		<table frame="border" rules="all">
			<xsl:for-each select="$result">
				<xsl:if test="$query_page='all' or position()&lt;=$query_page">
					<tr>
						<td align="center"><xsl:value-of select="position()"/></td>
						<td><xsl:value-of select="id"/></td>
						<td><xsl:value-of select="name"/></td>
						<td><xsl:value-of select="state"/></td>
						<td><xsl:value-of select="cat"/></td>
					</tr>
				</xsl:if>
			</xsl:for-each>
		</table>
	</xsl:template>

</xsl:stylesheet>