您的 XSLT 需要进行一些认真的清理工作。记住要牢记DRY 原则。通过避免使用重复的部分,我能够将 XSLT 的长度减少 100 多行。一旦你弄清楚如何正确传递参数值,这应该可以工作:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--Search parameter to store the search string entered-->
<xsl:param name="SearchRecipe" />
<!--Search type parameter to store the type of search selected-->
<xsl:param name="SearchType" select="'Start'" />
<xsl:param name ="IsVegetarian" />
<!--First part of code to capture values from user and then store them
in the variables above-->
<xsl:template match="/">
<html>
<head>
<!--Sets Title of Page (within our Master template)-->
<a>
<title>Recipe Headquarters</title>
</a>
<!--Reference to the stylesheet we are using-->
<link href="Cafesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="banner" name="content" method="get" action="Commercial.aspx">
<h1 >Recipe Book</h1>
<div id="content">
<p>
<label id="h2" for="txtFilter">Search Recipe Book </label>
<input type="text" name="txtFilter"
id="txtFilter" value="{$SearchRecipe}"/>
<!--Creates a simple submit button-->
<input type="submit" name="btnSearch"
id="btnSearch" value="Submit" />
<br />
<br />
<!--Label of text for our set of radio buttons-->
<label for="rdoSearchType">Select a Search Type </label>
<!--Behold, the radio buttons themselves-->
<xsl:call-template name="SearchTypeRadio">
<xsl:with-param name="value" select="'Start'" />
<xsl:with-param name="text" select="'Starts with'" />
</xsl:call-template>
<xsl:call-template name="SearchTypeRadio">
<xsl:with-param name="value" select="'Contains'" />
<xsl:with-param name="text" select="'Contains'" />
</xsl:call-template>
<xsl:call-template name="SearchTypeRadio">
<xsl:with-param name="value" select="'RecipeType'" />
<xsl:with-param name="text" select="'Recipe Type'" />
</xsl:call-template>
<br />
<br />
<br />
<!--Applys the template from the second part-->
<xsl:apply-templates
select="CafeRecipes[$SearchType = 'Start' or
$SearchType = 'Contains' or
$SearchType = 'RecipeType']" />
</p>
<!--End our of Search input text box and the Type of search
we want to do Div-->
</div>
<!--Our self closing footer div, yayyyy. Single line madness-->
<h1>Made by Jagga ^_^</h1>
</form>
</body>
</html>
</xsl:template>
<xsl:template name="SearchTypeRadio">
<xsl:param name="value" />
<xsl:param name="text" />
<input type="radio" name="rdoSearchType" value="{$value}">
<xsl:if test="$SearchType = $value">
<xsl:attribute name="checked">checked</xsl:attribute>
</xsl:if>
<xsl:value-of select="$text"/>
</input>
</xsl:template>
<!--This will be the second part of the xsl code to manipulate the data
based on our chosen values from above-->
<!--Declares new xsl template-->
<xsl:template match="CafeRecipes">
<!--Creates a small table to display the data output produced-->
<table id="content" border="5">
<tr>
<th>Recipe #</th>
<th>Recipe Name</th>
<th>Ingredients Required</th>
<th>Instructions</th>
<th>Recipe Type</th>
<th>Vegetarian?</th>
</tr>
<xsl:variable name="matchedRecipes"
select="ARecipe[
($SearchType = 'Start' and starts-with(ItemName, $SearchRecipe)) or
($SearchType = 'Contains' and contains(ItemName, $SearchRecipe)) or
($SearchType = 'RecipeType' and
contains(RecipeInfo/RecipeType, $SearchRecipe))
]" />
<xsl:if test="$SearchType = 'Start' or $SearchType = 'Contains'">
<xsl:apply-templates select="$matchedRecipes">
<xsl:sort select="RecipeInfo/RecipeType" order="ascending" />
</xsl:apply-templates>
</xsl:if>
<xsl:if test="$SearchType = 'RecipeType'">
<xsl:apply-templates select="$matchedRecipes">
<xsl:sort select="ItemName" order="ascending" />
</xsl:apply-templates>
</xsl:if>
</table>
</xsl:template>
<xsl:template match="ARecipe">
<tr>
<xsl:apply-templates select="*[not(*)] | RecipeInfo/*" />
</tr>
</xsl:template>
<xsl:template match="ARecipe/* | ARecipe/*/*">
<td width="300">
<xsl:apply-templates select="." mode="content" />
</td>
</xsl:template>
<xsl:template match="Vegetarian[. = 'Yes']" mode="content">
YesThisWorks and it's vegetarian
</xsl:template>
<xsl:template match="Vegetarian[. = 'No']" mode="content">
YesThisWorks and it's NOT vegetarian
</xsl:template>
</xsl:stylesheet>
AddParameter()
我在您的 ASPX 代码中看到的唯一调用是MyXMLConduit.AddParameter("TheFilter", AFilter);
. 由于 XSLT 中的参数名称是“SearchRecipe”,因此您需要使用它作为参数名称,而不是“TheFilter”。您还需要获取rdoSearchType
查询参数的值并将其添加为 XSLT 的参数之一。
string searchType = Request["rdoSearchType"];
if(searchType != null)
{
MyXMLConduit.AddParameter("SearchType", searchType);
}