XSLT文件不显示任何XML数据

时间:2013-02-02 22:52:26

标签: xml xslt

我正在使用C#来处理使用XML数据的XSLT表。

在我的XSLT表上,我正在应用一个遍历XML数据的模板,并假设根据传递给它的参数丢弃数字。

当我运行XSLT表并将参数传递给它时,没有输出。 XSLT的html端似乎运行得很好。

这是我的XSLT代码:

http://pastebin.com/eL9wnRgK

这是我的XML文件:

http://pastebin.com/B3eqbd6K

我正在使用Visual Studio 2012处理上述内容,如果我们排除了用于处理xslt的C#类,即使在VS的内置XSLT调试器中它似乎也没有做任何事情(没有数据输出) 。

我花了好几个小时经历但却无法找到我出错的地方,并且没有任何突出显示为红色的语法或显示的任何错误。这种语言比c#...

更难

1 个答案:

答案 0 :(得分:1)

您的XSLT需要进行一些严肃的清理工作。请记住DRY principle。通过避免使用重复的部分,我能够将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>

我在ASPX代码中看到AddParameter()的唯一号码是MyXMLConduit.AddParameter("TheFilter", AFilter);。由于XSLT中参数的名称是“SearchRecipe”,因此您需要将其用作参数名称,而不是“TheFilter”。您还需要获取rdoSearchType查询参数的值,并将其添加为XSLT的参数之一。

string searchType = Request["rdoSearchType"];
if(searchType != null)
{
    MyXMLConduit.AddParameter("SearchType", searchType);
}