开发者

JQuery contains exact search

开发者 https://www.devze.com 2023-01-24 02:40 出处:网络
I\'m using JQuery to parse some xml and having trouble with the line below: $(xml).find(\"ACT:contains(\" + selectedAct + \")>SCENE\").each(function()

I'm using JQuery to parse some xml and having trouble with the line below:

$(xml).find("ACT:contains(" + selectedAct + ")>SCENE").each(function()

For example, if the selected act is 'ACT I', then the above line finds not only 'ACT I', but also 'ACT II', 'ACT IV', and so on, i.e. it is returning all acts that contain the variable selectedAct.

Any help is much appreciated. I've seen examples where 'filter' is used with an anonymous function, but I don't know how to structure it in relation to my code above.

Any ideas?

Many thanks

EDIT:

XML sample:

<ACT><TITLE>ACT I</TITLE>

<SCENE><TITLE>SCENE I.  Alexandria. A room in CLEOPATRA's palace.</TITLE>
<STAGEDIR>Enter DEMETRIUS and PHILO</STAGEDIR>

<SPEECH>
<SPEAKER>PHILO</SPEAKER>
&l开发者_JS百科t;LINE>Nay, but this dotage of our general's</LINE>

DTD:

<!-- DTD for Shakespeare    J. Bosak    1994.03.01, 1997.01.02 -->
<!-- Revised for case sensitivity 1997.09.10 -->
<!-- Revised for XML 1.0 conformity 1998.01.27 (thanks to Eve Maler) -->

<!ENTITY amp "&#38;#38;">
<!ELEMENT PLAY     (TITLE, FM, PERSONAE, SCNDESCR, PLAYSUBT, INDUCT?,
                         PROLOGUE?, ACT+, EPILOGUE?)>
<!ELEMENT TITLE    (#PCDATA)>
<!ELEMENT FM       (P+)>
<!ELEMENT P        (#PCDATA)>
<!ELEMENT PERSONAE (TITLE, (PERSONA | PGROUP)+)>
<!ELEMENT PGROUP   (PERSONA+, GRPDESCR)>
<!ELEMENT PERSONA  (#PCDATA)>
<!ELEMENT GRPDESCR (#PCDATA)>
<!ELEMENT SCNDESCR (#PCDATA)>
<!ELEMENT PLAYSUBT (#PCDATA)>
<!ELEMENT INDUCT   (TITLE, SUBTITLE*, (SCENE+|(SPEECH|STAGEDIR|SUBHEAD)+))>
<!ELEMENT ACT      (TITLE, SUBTITLE*, PROLOGUE?, SCENE+, EPILOGUE?)>
<!ELEMENT SCENE    (TITLE, SUBTITLE*, (SPEECH | STAGEDIR | SUBHEAD)+)>
<!ELEMENT PROLOGUE (TITLE, SUBTITLE*, (STAGEDIR | SPEECH)+)>
<!ELEMENT EPILOGUE (TITLE, SUBTITLE*, (STAGEDIR | SPEECH)+)>
<!ELEMENT SPEECH   (SPEAKER+, (LINE | STAGEDIR | SUBHEAD)+)>
<!ELEMENT SPEAKER  (#PCDATA)>
<!ELEMENT LINE     (#PCDATA | STAGEDIR)*>
<!ELEMENT STAGEDIR (#PCDATA)>
<!ELEMENT SUBTITLE (#PCDATA)>
<!ELEMENT SUBHEAD  (#PCDATA)>


Try this:

$(xml).find("ACT > TITLE").filter(function() {
    return this.firstChild.nodeValue === selectedAct;
}).siblings('SCENE').each(function() {...


You could try extending the selectors... I made a demo in my blog a while back.

$.extend($.expr[':'],{
containsExact: function(a,i,m){
return $.trim(a.innerHTML.toLowerCase()) === m[3].toLowerCase();
},
containsExactCase: function(a,i,m){
return $.trim(a.innerHTML) === m[3];
},
containsRegex: function(a,i,m){
 var regreg =  /^\/((?:\\\/|[^\/])+)\/([mig]{0,3})$/,
  reg = regreg.exec(m[3]);
 return RegExp(reg[1], reg[2]).test($.trim(a.innerHTML));
}
});
0

精彩评论

暂无评论...
验证码 换一张
取 消