kush
kush

Reputation: 79

How I can get nodes based on condition given in xpath on the basis of current date?

I have the XML tree as given below and I want to extract the one attribute from the active node, Here I am calculating the active node on the basis of start_date and end_date of the node.

                  <Parent>
                   <child>
                    <benefits_rate>0.0</benefits_rate>
                    <created_on_timestamp>2021-01-15T07:06:48.000Z</created_on_timestamp>
                    <custom_double6>3400.0</custom_double6>
                    <custom_string20>Yes</custom_string20>
                    <end_date>9999-12-31</end_date>
                    <event>12</event>
                    <event_reason>PAYOTH</event_reason>
                    <is_insider>false</is_insider>
                    <last_modified_by>sfadmin</last_modified_by>
                    <last_modified_on>2021-01-15T07:06:48.000Z</last_modified_on>
                    <pay_group>FZ</pay_group>
                    <seq_number>1</seq_number>
                    <start_date>2021-01-15</start_date>
                   </child>
                   <child>
                    <benefits_rate>0.0</benefits_rate>
                    <created_on_timestamp>2019-12-26T11:30:36.000Z</created_on_timestamp>
                    <custom_string20>No</custom_string20>
                    <end_date>2021-01-14</end_date>
                    <event>H</event>
                    <event_reason>HIRNEW</event_reason>                        
                    <is_insider>false</is_insider>
                    <last_modified_by>sfadmin</last_modified_by>
                    <last_modified_on>2019-12-26T11:30:36.000Z</last_modified_on>
                    <pay_group>FZ</pay_group>
                    <seq_number>1</seq_number>
                    <start_date>2009-12-01</start_date>
                   </child>
                  </parent> 

Xpath I am providing like

//parent/child[xs:date(@start_date) le format-date(current-date(),  "[[[Y]-[M]-[D]]]") and  xs:date(@end_date) ge format-date(current-date(), "[[[Y]-[M]-[D]]]")]/event_reason

Can someone guide me here, what I am doing wrong?

Upvotes: 1

Views: 78

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167696

To compare xs:dates you don't need and shouldn't use format-date, just use the comparison operators on the xs:date values e.g. //child[xs:date(start_date) le current-date() and xs:date(end_date) ge current-date()]/event_reason.

Upvotes: 1

JaSON
JaSON

Reputation: 4869

Your XPath needs couple fixes:

  • XPath is case-sensitive, so use Parent instead of parent
  • start_date/end_date are children, but not attributes, so no need to add @
  • There are extra square brackets in date formatting: "[[[Y]-[M]-[D]]]" --> "[Y]-[M]-[D]"

The working expression is

//Parent/child[date(start_date) le format-date(current-date(),  "[Y]-[M]-[D]") and date(end_date) ge format-date(current-date(), "[Y]-[M]-[D]")]/event_reason

Upvotes: 0

Related Questions