Reputation: 1059
I'm fairly new on jQuery and manipulating xml doms. So if my question is not releavnt forgive me.
Sample Of My XML:
<Urunler>
<Urun>
<ID>21955</ID>
<Mensei>Coke</Mensei> /* Contains Same Value */
</Urun>
<Urun>
<ID>21956</ID>
<Mensei>Coke</Mensei> /* Contains Same Value */
</Urun>
<Urunler>
My Ajax Call and Jquery code to show them into html dom:
$.ajax({
url: 'webservice/Resim/Stokkart.xml',
dataType: 'xml',
cache:true,
success: parseXml
});
function parseXml(xml) {
$(xml).find("Urun").filter(function () {
return $(this).find("ASTipNo").text() == categoryCode;
}).each(function () {
$('#product-list').append(
'<div class="product-name col-lg-12 col-md-12 col-sm-12 col-xs-12"></div>'+
'<h5 style="color:red;">' + $(this).find("Mensei").text() + '</h5>'+
'</div>'
);
});
}
What i'm trying to achieve; i want to remove repeating <Urun>
nodes if they has the same <Mensei>
values in my xml document or can i merge them into one node (Like GROUP BY
in SQL)?
PS: I can not do it in server side because i'm developing an offline mobile app on cordova.
Any help will greatly appricated.
Upvotes: 0
Views: 129
Reputation: 4652
var xmlString = "
<Urunler>
<Urun>
<ID>21955</ID>
<Mensei>Coke</Mensei>
</Urun>
<Urun>
<ID>21956</ID>
<Mensei>Coke</Mensei>
</Urun>
<Urun>
<ID>21957</ID>
<Mensei>Test</Mensei>
</Urun>
<Urunler>";
var menseiA = [];
var $xml = $(xmlString);
var $uruns = $xml.find("Urun");
//output xml
$xml.find("Urun").each(function () {
var id = $(this).find('ID').text();
var mensei = $(this).find('Mensei').text();
console.log(id + ':' + mensei);
});
console.log('=====================');
//finds and removes duplicates
$uruns.each(function () {
var mensei = $(this).find('Mensei').text();
if (jQuery.inArray(mensei, menseiA) !== -1) $(this).remove();
menseiA.push(mensei);
});
//output new xml
$xml.find("Urun").each(function () {
var id = $(this).find('ID').text();
var mensei = $(this).find('Mensei').text();
console.log(id + ':' + mensei);
});
Upvotes: 1