/ / Construire une instruction NOT pour analyser XML - jquery

Construire une instruction NOT pour analyser XML - jquery

J'essaie de construire une instruction qui analysera mon code XML en recherchant toutes les lignes qui ne contiennent pas les mots "HIDE" ou "CANCELED" dans le champ "Titre" et ne contiennent pas de champ "Indexé" vide.

Jusqu'ici, j'ai essayé ceci comme point de départ, ce qui, à mon avis, filtrerait tout ce qui contenait "HIDE" et un champ Indexé vide, mais cela ne fonctionnait pas.

$(xml).find("Qry1:not(Title:contains("HIDE"), Indexed:contains(""))").each(function(){

Est-ce que je suis même près d'être ici?

Réponses:

1 pour la réponse № 1

Que diriez-vous d'une approche séquentielle?

$(xml).find("Qry1")
.not(":has(Title:contains("HIDE"))")
.not(":has(Title:contains("CANCELLED"))")
.not(":has(Indexed:empty)")
.each( /* ... */);

Essayez-le:

var xml = "<root>
<Qry1>
<Title>HIDE</Title>
<Indexed>1</Indexed>
</Qry1>
<Qry1>
<Title>CANCELLED</Title>
<Indexed>1</Indexed>
</Qry1>
<Qry1>
<Title>I"m not indexed</Title>
<Indexed></Indexed>
</Qry1>
<Qry1>
<Title>Found Me!</Title>
<Indexed>1</Indexed>
</Qry1>
</root>";

$(xml).find("Qry1")
.not(":has(Title:contains("HIDE"))")
.not(":has(Title:contains("CANCELLED"))")
.not(":has(Indexed:empty)")
.each(function () {
var title = $(this).find("Title").text();
$("<div>", {text: title}).appendTo("#target");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="target"></div>

Vous pouvez aussi utiliser .filter, comme ça:

$(xml).find("Qry1")
.filter(function () {
var $this = $(this),
title = $this.find("Title").text(),
indexed = $this.find("Indexed").text();

return !(title === "HIDE" || title === "CANCELLED" || !indexed);
})
.each( /* ... */);