http://www.sephiroth.it/file_detail.php?pageNum_comments=20&id=130
Es un buscador de episodios que usa Xpath para su funcionamiento.
El buscador busca por temporada y episodo siguiendo la siguiente jerarquía
<id>1-1</id> Donde el primer 1 es la temporada y el segundo el número del epsidio.
El código completo es el siguiente:
Código :
// imports xfactor xpath functions and creates new XMl doc
import com.xfactorstudio.xml.xpath.*;
myDoc = new XML();
myDoc.load("data/buscar.xml");
// search and replace prototype from actionscript.org 
// this is used later on to search for term and replace with same term 
// wrapped in html code to highlight
String.prototype.searchReplace = function(find, replace) {
   return this.split(find).join(replace);
};
// Part 1 of search (runs when search button is pressed, or the enter key)
function startSearch() {
   
   // clears result text areas
   this.taGuestStar.text = "";
   this.txDescription.text = "";
   this.txTitle.text = "";
   this.txDate.text = "";
   
   // gets search word from input text
   term = this.laoinput.text;
   
   if (term == "") {  // if search is blank
      this.errorBox.gotoAndStop(2);  
   }
   
   else {  // else continue on with search
      
   
      // gets data from radio button on where to search.
      //The 'data' for each button corresponds to the xml tag name it searches
      tag = searchGroup.selection.data;
   
      // builds query string.  this searches every 'ep' tag for
      // the term from the input text box (term) in the chosen tag (tag) [Description, Guest Star or Title]
      var query:String = "//ep[contains("+tag+", "+"'"+term+"'"+")]";
   
      // selects the nodes using contains function of XPath
      myResults = XPath.selectNodes(myDoc, query);
   
      // Converts myResults array of xml nodes to regular array 
      // this part was made with the help of Neeld 
      myDataSet = new Array();
      for (var i = 0; i<myResults.length; i++) {
         var name = XPath.selectNodes(myResults[i], "./name")[0].firstChild.nodeValue;
         var id = XPath.selectNodes(myResults[i], "./id")[0].firstChild.nodeValue;
         myDataSet.push({Name:name, ID:id});
      }
      //sends resulting array to result datagrid
      this.dgSearchResults.dataProvider = myDataSet;
   
   
      // my crappy little hit counter /////////////////
      var hits = myResults.length;
      if (hits == 1) {
         hits = "Un resultado";
      } else if (hits>1) {
         hits = hits+" Resultados encontrados";
      } else {
         hits = "Ningún resultado";
      }
      //sets hit counter label
      this.ResultsLabel.text = hits;
      ////////////////////////////////
   }
}
// Part 2 of search (runs when a result is choosen from result datagrid)
function highlightTerms() {
   
   /* the reason for all this nonsense is that the episode descriptions
     can be lengthy, so instead of including them in the result array
     which would slow down the search I 
     am using the 'id' from the result array to build
     the xml path to the episode selected
     If you don't have large blocks of text to return and you don't
     need to highlight the search term, you don't neccesarrily need this 
     step
   */
   var gridindex = this.dgSearchResults.getSelectedItem().ID;
   var sindex = gridindex.charAt(0);   // this is the season number
   var epindex1 = gridindex.charAt(2);
   var epindex2 = gridindex.charAt(3);
   if (epindex1 == 0) {
      epindex = epindex2;            // this is episode number if <10, so that it ignores the leading '0'
   } else {                          
      epindex = epindex1+epindex2;   // this is episode number if >10
   }
   
   // Uses selected episode id to select the nodes for that episode
   // which can then be used to get all the information from it
   var indexQuery:String = "/episodeguide/season["+sindex+"]/ep["+epindex+"]";
   var choosenEpisodeData = XPath.selectNodes(myDoc, indexQuery);
   
   // gets the search term from input text box tp be used later
   var term = this.laoinput.text;
   
   // highlights search word in the description
   var choosenEpisodeDescription = XPath.selectNodes(choosenEpisodeData, "./d")[0].firstChild.nodeValue;
   choosenEpisodeDescription = choosenEpisodeDescription.toString();
   var newString1 = choosenEpisodeDescription.searchReplace(term, "<font color='#0000CC'><b>"+term+"</b></font>");
   txDescription.text = newString1;
   
   // highlights search word in the guest star box
   var choosenGuestStarText = XPath.selectNodes(choosenEpisodeData, "./gs")[0].firstChild.nodeValue;
   choosenGuestStarText = choosenGuestStarText.toString();
   var newString2 = choosenGuestStarText.searchReplace(term, "<font color='#0000CC'><b>"+term+"</b></font>");
   taGuestStar.text = newString2;
   
   // set name in the gray bar
   txTitle.text = this.dgSearchResults.getSelectedItem().Name;
   
   // set date in the gray bar
   var choosenDatetext = XPath.selectNodes(choosenEpisodeData, "./dt")[0].firstChild.nodeValue;
   choosenDatetext1 = choosenDatetext.toString();
   txDate.text = choosenDatetext1;
}
//////////// OTHER STUF ///////////////////////////////////////
///// for enter key press  perform search//////
function keyDownFunction() {
   if (Key.isDown(Key.ENTER)) {
      startSearch();
   }
}
var EnterKeyListener = new Object();
EnterKeyListener.onKeyDown = keyDownFunction;
Key.addListener(EnterKeyListener);
stop();
///////////////////////////////////////////////
// styles for components
txDescription.setStyle("marginLeft", "4");
txDescription.setStyle("marginRight", "1");
taGuestStar.setStyle("fontSize", "10");
cbExampleSearches.setStyle("fontSize", "9");
cbExampleSearches.setStyle("backgroundColor", '0x979797');
cbExampleSearches.addEventListener("change", examplechange); 
SubCb.addEventListener("change", examplechange); 
El problema ha surgido cuando he sobrepasado el número de sesión con 1 cifra (del 1 al 9) a 2 cifras (10, 11, 12, etc.)
¿Alguién me puede echar una mano para lograr que el sistema funcione con temporadas de 2 cifras (10, 11, etc.)?
Creo que tendría que cambiar algo del siguiente código:
Código :
   var gridindex = this.dgSearchResults.getSelectedItem().ID;
   var sindex = gridindex.charAt(0);   // this is the season number
   var epindex1 = gridindex.charAt(2);
   var epindex2 = gridindex.charAt(3);
   if (epindex1 == 0) {
      epindex = epindex2;            // this is episode number if <10, so that it ignores the leading '0'
   } else {                          
      epindex = epindex1+epindex2;   // this is episode number if >10
   }
Pero por desgracia no controlo mucho y no tengo ni idea de qué es exáctamente lo que tendría que cambiar
¿Alguien me puede echar una mano, por favor?
Bueno, en espera de sus prontas noticias,
Un saludo
Barry Collins
