2017-09-17 123 views
1

你好下面的代碼使我的網頁加載速度非常緩慢。你能幫忙嗎?XSL模板正在加載速度非常緩慢

<xsl:template match="Category" mode="CategorySelectorScript"> 
      <xsl:variable name="ThisCategoryID" select="CategoryID"/> 
      <xsl:variable name="HasChildren"><xsl:choose> 
       <xsl:when test="../Category[ParentCategoryID=$ThisCategoryID]"> 
        true 
       </xsl:when> 
       <xsl:otherwise> 
        false 
       </xsl:otherwise></xsl:choose> 
      </xsl:variable> 
      arrCategory=new Array(<xsl:value-of select="CategoryID"/>, <xsl:value-of select="ParentCategoryID"/>, '<xsl:value-of select="Name"/>', <xsl:value-of select="$HasChildren"/>); 
      arrCategories.push(arrCategory); 
     </xsl:template> 

問題是這一行<xsl:when test="../Category[ParentCategoryID=$ThisCategoryID]">當我從它加載快的XPath刪除../但當時我沒有得到的工作特性。那麼../有什麼替代方案嗎?

全碼

<?xml version="1.0" encoding="utf-8"?> 

<xsl:stylesheet version="1.0"  
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    exclude-result-prefixes="xsl"> 

<xsl:output method="xml" 
      omit-xml-declaration="yes" 
      indent="yes" 
      encoding="utf-8"/> 

    <xsl:template match="Location"> 
     <xsl:call-template name="CategorySelectorScript"/> 

     <form id="frmLocationCategoryAdd" action="locationcategoriesadd.php" method="post"> 
      <div> 
       <input type="hidden" id="inpAction" name="inpAction" value="SAVE"/> 
       <input type="hidden" id="inpLocationID" name="inpLocationID" value="{LocationID}"/> 
       <input type="hidden" name="inpCategoryIDs" id="inpCategoryIDs" value=""/> 
      </div> 

      <div id="categoryselects"><xsl:text> </xsl:text></div> 

      <div><a id="categoryselected" href="#" class="actionlink addcategory">Add this category</a></div> 
      <div id="categoryalreadyselected">This category has already been selected</div> 

      <h3 id="categoriestoadd">Categories to add</h3> 
      <div id="categoriesselected"><p id="nocategoriesmessage">No categories</p></div> 

      <div class="formcontrols"> 
       <a class="savelink" href="#" id="savelink"> 
        <span>Add Categories</span> 
       </a> 
       <a class="cancellink" href="#" id="cancellink"> 
        <span>Cancel</span> 
       </a> 
      </div> 
     </form> 
    </xsl:template> 


    <xsl:template name="CategorySelectorScript"> 
     <script type="text/javascript"> 
     <xsl:value-of select="$start_html_comment" disable-output-escaping="yes"/> 
      $(document).ready(
      function() 
      { 
       $('#cancellink').click(function() { 
        $('#inpAction').val('CANCEL'); 
        $('#frmLocationCategoryAdd').submit(); 
        } 
       ); 

       $('#savelink').click(function() { 
        ProcessCategoryIDs(); 

        <!-- alert($('#inpCategoryIDs').val()); --> 

        $('#frmLocationCategoryAdd').submit(); 
       } 
       ); 

       $('#categoryselects').append(CreateCategorySelectBox(<xsl:value-of select="/XML/RootCategoryID"/>)); 

       $('#categoryselected').hide(); 
       $('#categoryalreadyselected').hide(); 

       $('#categoryselected').click(function() { 
        $('#categoryselected').hide(); 
        $('#nocategoriesmessage').hide(); 

        intThisCategoryID=0; 
        strCategoryHierarchy=''; 
        $('#categoryselects').children().each(function(i) { 
         if (!isNaN(parseInt(this.value, 10))) 
         { 
          intThisCategoryID=parseInt(this.value, 10); 
          strCategoryHierarchy+=GetCategoryName(intThisCategoryID)+' <xsl:value-of select="'&gt;'" disable-output-escaping="yes"/> '; 
         } 
        } 
        ); 

        <!-- alert(intThisCategoryID); --> 

        <!-- remove last > --> 
        strCategoryHierarchy=strCategoryHierarchy.replace(/ <xsl:value-of select="'&gt;'" disable-output-escaping="yes"/> $/i, ''); 

        strCategoryToAdd='<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>div id="AddedCategory_'+intThisCategoryID+'" class="addedcategory"<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 

        strCategoryToAdd+='<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>span class="addedcategoryhierarchy"<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'+strCategoryHierarchy+'<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>/span<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 

        strCategoryToAdd+='<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>a id="DeleteCategory_'+intThisCategoryID+'" class="actionlink deletecategory" onclick="DeleteCategory(this)"<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>Delete this category<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>/a<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 

        strCategoryToAdd+='<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>/div<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 

        $('#categoriesselected').append(strCategoryToAdd); 

        return false; 
       } 
       ); 

      } 
      ) 

      function DeleteCategory(oDeleteClicked) 
      { 
       $('#AddedCategory_'+getID(oDeleteClicked.id)).remove(); 

       if (0==$('.addedcategory').length) 
        $('#nocategoriesmessage').show(); 
      } 

      function GetCategoryName(intCategoryID) 
      { 
       for (i=0; i<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>arrCategories.length; i++) 
       { 
        if (arrCategories[i][0]==intCategoryID) 
         return arrCategories[i][2]; 
       } 

       return ''; 
      } 

      function SelectClicked(oSelect) 
      {   
       intCategoryID=$('#'+oSelect.id).val(); 
       intParentCategoryID=getID(oSelect.id); 
       strCategoryText=$('#CategoryID_'+intParentCategoryID+'_'+intCategoryID).html(); 
       CategoryClicked(intCategoryID, intParentCategoryID, strCategoryText); 
      } 

      function CategoryClicked(intCategoryID, intParentCategoryID, strCategoryText) 
      { 
        <!-- 
        alert('intCategoryID: '+intCategoryID); 
        alert('intParentCategoryID: '+intParentCategoryID); 
        --> 

        $('#categoryselected').hide(); 
        $('#categoryalreadyselected').hide(); 
        <!-- some pretty gnarly stuff to get all sibling select boxes after the current one --> 
        var allSelects = $('#categoryselects select'); 
        var currentIndex = allSelects.index($('#CategorySelect_'+intParentCategoryID)[0]); 
        var nextSiblings = allSelects.gt(currentIndex); 
        nextSiblings.remove(); 

        <!-- if this isn't a leaf category, populate the next select box with its children --> 
        if (-1!=strCategoryText.search('&gt;') || -1!=strCategoryText.search('<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>')) <!-- for some reason safari sees the non-escaped > in the string --> 
        { 
         $('#categoryselects').append(CreateCategorySelectBox(intCategoryID)); 
         $('#CategorySelect_'+intCategoryID).focus(); 

         /* force scrolling div over to the right */ 
         oCategorySelects=document.getElementById('categoryselects'); 
         oCategorySelects.scrollLeft=oCategorySelects.scrollWidth-680; 
        } 
        else 
        { 
         /* check that this category isn't already in the selected categories */ 
         var bFound=false; 

         $('#categoriesselected').children().each(function() { 
          if (getID(this.id)==intCategoryID) 
           bFound=true; 
         } 
         ); 

         if (bFound) 
          $('#categoryalreadyselected').show(); 
         else 
          $('#categoryselected').show(); 
        } 
      } 

      function ProcessCategoryIDs() 
      { 
       arrCategoryIDs=new Array(); 

       $('#categoriesselected').children().each(function() { 
        if (!isNaN(parseInt(getID(this.id), 10))) 
         arrCategoryIDs.push(getID(this.id)); 
       } 
       ); 

       $('#inpCategoryIDs').val(arrCategoryIDs.join()); 
      } 

      <!-- generate javascript array from the categories XML: --> 
      var arrCategories=new Array(); 

      <xsl:apply-templates select="/XML/Categories/Category" mode="CategorySelectorScript"/> 

      <!-- alert(dump(arrCategories)); --> 

      <!-- category select box writing stuff --> 
      function CreateCategorySelectBox(intCategoryID) 
      { 
       strSelectBox='<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>select id="CategorySelect_'+intCategoryID+'" size="10" onclick="SelectClicked(this)"<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 

       arrCategoriesInThisCategory=GetCategoriesWithParentID(intCategoryID); 

       for (i=0; i<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>arrCategoriesInThisCategory.length; i++) 
       { 
        if (true==arrCategoriesInThisCategory[i][3]) 
         strChildMarker=' <xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 
        else 
         strChildMarker=''; 

        strSelectBox+='<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>option id="CategoryID_'+intCategoryID+'_'+arrCategoriesInThisCategory[i][0]+'" value="'+arrCategoriesInThisCategory[i][0]+'"<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'+arrCategoriesInThisCategory[i][2]+strChildMarker+'<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>/option<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 
       } 

       strSelectBox+='<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>/select<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 

       return strSelectBox; 
      } 

      function GetCategoriesWithParentID(intParentCategoryID) 
      { 
       arrResult=new Array(); 
       for (i=0; i<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>arrCategories.length; i++) 
       { 
        if (arrCategories[i][1]==intParentCategoryID) 
         arrResult.push(arrCategories[i]); 
       } 

       return arrResult; 
      } 
     // <xsl:value-of select="$end_html_comment" disable-output-escaping="yes"/> 
     </script> 
    </xsl:template> 

    <xsl:template match="Category" mode="CategorySelectorScript"> 
     <xsl:variable name="ThisCategoryID" select="CategoryID"/> 
     <xsl:variable name="HasChildren"><xsl:choose> 
      <xsl:when test="../Category[ParentCategoryID=$ThisCategoryID]"> 
       true 
      </xsl:when> 
      <xsl:otherwise> 
       false 
      </xsl:otherwise></xsl:choose> 
     </xsl:variable> 
     arrCategory=new Array(<xsl:value-of select="CategoryID"/>, <xsl:value-of select="ParentCategoryID"/>, '<xsl:value-of select="Name"/>', <xsl:value-of select="$HasChildren"/>); 
     arrCategories.push(arrCategory); 
    </xsl:template> 

    <!-- category filter for location searches (front and back end) --> 
    <xsl:template name="CategoryFilter"> 
     <xsl:param name="FiltersOpen">0</xsl:param> 

     <input type="hidden" name="inpCategoryIDs" id="inpCategoryIDs" value="0"/> 

     <xsl:choose> 
      <xsl:when test="0 = $FiltersOpen"> 
       <a href="#" id="filterlink">Filter by location categories</a> 
       <a href="#" id="cancelfilterlink" style="display:none">Remove location category filters</a> 
      </xsl:when> 
      <xsl:otherwise> 
       <a href="#" id="filterlink" style="display:none">Filter by location categories</a> 
       <a href="#" id="cancelfilterlink">Remove location category filters</a> 
      </xsl:otherwise> 
     </xsl:choose> 

     <div id="categoryfilters"> 
      <xsl:if test="0 = $FiltersOpen"> 
       <xsl:attribute name="style">display:none;</xsl:attribute> 
      </xsl:if> 
      <div id="categoryselects"><xsl:text> </xsl:text></div> 

      <a id="categoryselected" href="#" class="actionlink addcategory">Add this category</a> 
      <div id="categoryalreadyselected">This category has already been selected</div> 

      <h3 id="categoriestoadd">Categories to filter by</h3> 
      <div id="categoriesselected"><p id="nocategoriesmessage">No categories</p></div> 
     </div> <!-- #categoryfilters --> 

     <script type="text/javascript"> 
     <xsl:value-of select="$start_html_comment" disable-output-escaping="yes"/> 
      $(document).ready(
       function() 
       { 
        $('#filterlink').click(function() { 
         $('#categoryfilters').slideDown(); 
         $(this).hide(); 
         $('#cancelfilterlink').show(); 
         return false; 
        } 
        ); 

        $('#cancelfilterlink').click(function() { 
         $('#categoryfilters').slideUp(); 

         $('#categoryfilters select:gt(0)').remove(); 
         $('#categoryfilters select:first option[@selected]').removeAttr('selected'); 

         $('#categoriesselected').children().remove(); 
         $('#categoriesselected').append('<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>p id="nocategoriesmessage"<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>No categories<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>/p<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>') 

         $(this).hide(); 
         $('#filterlink').show(); 

         return false; 
        } 
        ); 

        if (0!=arrSelectedCategoryIDs.length) 
        { 
         $('#nocategoriesmessage').hide(); 
         $('#categoryfilters').show(); 
         $('#filterlink').hide(); 
         $('#cancelfilterlink').show(); 

         for (i=0; i<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>arrSelectedCategoryIDs.length; i++) 
         { 
          SelectCategory(arrSelectedCategoryIDs[i]); 
         } 
        } 
       } 
      ); 

      function SelectCategory(intCategoryID) 
      { 
       strCategoryToAdd='<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>div id="AddedCategory_'+intCategoryID+'" class="addedcategory"<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 

       strCategoryToAdd+='<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>span class="addedcategoryhierarchy"<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'+CategoryHierarchyString(intCategoryID)+'<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>/span<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 

       strCategoryToAdd+='<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>a id="DeleteCategory_'+intCategoryID+'" class="actionlink deletecategory" onclick="DeleteCategory(this)"<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>Delete this category<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>/a<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 

       strCategoryToAdd+='<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>/div<xsl:value-of select="'&gt;'" disable-output-escaping="yes"/>'; 

       $('#categoriesselected').append(strCategoryToAdd); 
      } 

      function CategoryHierarchyString(intCategoryID) 
      { 
       var strTemp=''; 
       var i=0; 

       for (i=0; i<xsl:value-of select="'&lt;'" disable-output-escaping="yes"/>arrCategories.length; i++) 
       { 
        if (arrCategories[i][0]==intCategoryID) 
        { 
         if (<xsl:value-of select="/XML/RootCategoryID"/>==arrCategories[i][1]) 
         { 
          return arrCategories[i][2]; 
         } 
         else 
         { 
          strTemp=arrCategories[i][2]; 
          return CategoryHierarchyString(arrCategories[i][1])+' <xsl:value-of select="'&gt;'" disable-output-escaping="yes"/> '+strTemp; 
         } 
        } 
       } 
      } 
     // <xsl:value-of select="$end_html_comment" disable-output-escaping="yes"/> 
     </script> 
    </xsl:template> 

</xsl:stylesheet> 

PS:假如真有別的需要請評論,我將與你分享的要求。

謝謝

回答

0

您沒有顯示源XML。

如果類別元素的數量很大,如果他們都在文檔中同一水平上,那麼你應該通過定義按鍵得到改善:

<xsl:key name="CategoryByParentCategory" match="Category" 
    use="ParentCategoryID"/> 

,然後從test="../Category[ParentCategoryID=$ThisCategoryID]"改變你的測試到test="key('CategoryByParentCategory', $ThisCategoryID)"

(如果Category元素嵌套,或者如果你沒有搜索所有的人都這種變化是不是安全的:這就是爲什麼我們需要看到XML)

的點回答你的邏輯:你將變量$HasChildren的值設置爲一個包含字符串「true」或「false」的文本節點,其中包含一系列空白,然後插入一些生成的Javascript中。通過將變量聲明更改爲

<xsl:variable name="HasChildren" 
select="boolean(key('CategoryByParentCategory', $ThisCategoryID))"/> 
+0

您可以更方便地實現該效果請檢查編輯的完整代碼。謝謝 –

+0

謝謝你,先生。它只是通過在模板上添加xsl:鍵而神奇地工作。 –