0

I am trying to find multiple nodes in an XML file which have a node attribute ex. like position[validFromDate=2015-01-05] and where the date is within the range of two dates (30 day windows). In addition I need to have a logic operand OR as in "select nodes IF position isPrimaryPosition attribute = true OR position validFromDate (date) is between date1 and date2.

To be very clear: isPrimaryPosition=true OR (date1 > validFromDate < date2)

The relevant part of the XML file looks like this:

<positions>
<position isPrimaryPosition="true" validFromDate="2015-06-20">
<position isPrimaryPosition="false" validFromDate="2015-01-19">
<position isPrimaryPosition="false" validFromDate="2015-06-20">
<position isPrimaryPosition="true" validFromDate="2015-01-19">
<position isPrimaryPosition="false" validFromDate="2015-01-19">
<position isPrimaryPosition="false" validFromDate="2015-06-20">
</positions>

The code I'm working on right now is like this but I can't figure out how to do the selection between the dates an logically OR this to the isPrimaryPosition:

$ansettelse = $(this).find('position[isPrimaryPosition=true],position[validFromDate=2015-01-05]')   
  $ansettelse.each(function() {             
    $stillingskategori  = $(this).parent().siblings('category').attr('id');
    $ansattnummer       = $(this).parent().siblings('employeeId').text();
    $ansettelsesprosent = $(this).parent().siblings('employmentPercentage').text();
});

I hope to get some help pointing me in the right direction.

petrusjak
  • 45
  • 11
  • How are u getting the nodes from the xml file? Are you ajaxing them in? Are you using json objects? Is the xml data a string on the js side? – Demodave Jan 07 '15 at 14:46
  • I'm using an Ajax call to get the XML – petrusjak Jan 07 '15 at 14:50
  • var $person = $(xml).find('person:has(ssn:contains('+ $('#fld_socialsecurity').val().replace(/\./g, "") +'))'); and then $person.each(function() { – petrusjak Jan 07 '15 at 14:51
  • I'm struggling on the second part between dates. Also, I wasn't sure are you seeking the position such as [3] or a value? – Demodave Jan 07 '15 at 15:43
  • Thank you Demodave for your effort, I just want to make shure that only the nodes with the date within the range are included. Values is not important neither is position. – petrusjak Jan 07 '15 at 18:15
  • add a filter to the position, updated code. – Demodave Jan 07 '15 at 19:02
  • what do you mean by "only the nodes with the date " – Demodave Jan 07 '15 at 19:08
  • And I think your date test is wrong it should be (date1 < validFromDate < date2) for range if not you can correct my code. – Demodave Jan 07 '15 at 19:19

1 Answers1

0

I will answer this question in two parts

To be very clear: Part 1 [isPrimaryPosition=true] Part 2 [OR (date1 > validFromDate < date2)]

Part 1 - Test for: isPrimaryPosition=true

var value = $(xml).find("position[isPrimaryPosition=true]"); // <-- Get the value here

Part 2 - Test for: OR (date1 > validFromDate < date2)

var date1 = '2015-01-19';
var date2 = '2015-01-20';

// Since we are doing OR, If the 1st isPrimaryPosition=true doesn't exist reset value
if(value.length == 0) // <-- Test for the value here
{
   // Add a filter to the position then return test
   value = $(xml).find('position').filter(function() {

       var validFromDate = $(this).text();

       // I think your test is wrong date1 > validFromDate < date2
       // I think it should be date1 < validFromDate < date2 because you are doing range
       // value between to date fields I believe

       return date1 < validFromDate && validFromDate < date2;
   });
}

value.each(function () {

// This is just to spit out a value
$(".main").append($(this).find("date").text() + "<br />"); 

}); 

<div class="main">

</div>

I added a date tag just to see the values for the xml

<?xml version="1.0" encoding="UTF-8"?>
<positions>
    <position isPrimaryPosition="true" validFromDate="2015-06-20"><date>2015-06-20</date>
    </position>
<position isPrimaryPosition="false" validFromDate="2015-01-19"><date>2015-01-19</date>
    </position>
<position isPrimaryPosition="false" validFromDate="2015-06-20"><date>2015-06-20</date>
    </position>
<position isPrimaryPosition="true" validFromDate="2015-01-19"><date>2015-01-19</date>
    </position>
<position isPrimaryPosition="false" validFromDate="2015-01-19"><date>2015-01-19</date>
    </position>
<position isPrimaryPosition="false" validFromDate="2015-06-20"><date>2015-06-20</date>
    </position>
</positions>
Demodave
  • 6,242
  • 6
  • 43
  • 58