Filter XPath requests by date

I have a sample XML where I request nodes based on a date.

Example XML document :

<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<NewDataSet>
    <Table>
        <EmployeeBankGUID>dc396ebe-c8a4-4a7f-85b5-b43c1890d6bc</EmployeeBankGUID>
        <ValidFromDate>2012-02-01T00:00:00-05:00</ValidFromDate>
    </Table>
    <Table>
        <EmployeeBankGUID>2406a5aa-0246-4cd7-bba5-bb17a993042b</EmployeeBankGUID>
        <ValidFromDate>2013-02-01T00:00:00-05:00</ValidFromDate>
    </Table>
    <Table>
        <EmployeeBankGUID>2af49699-579e-4beb-9ab0-a58b4bee3158</EmployeeBankGUID>
        <ValidFromDate>2014-02-01T00:00:00-05:00</ValidFromDate>
    </Table>
</NewDataSet>

So basically three dates:

  • 2/1/2012
  • 1/2/2013
  • 1/2/2014

Using MSXML, I can query and filter these dates using an XPath query:

/NewDataSet/Table[ValidFromDate>"2013-02-12"]

And this works and returns IXMLDOMNodeListcontaining one element:

<Table>
    <EmployeeBankGUID>2af49699-579e-4beb-9ab0-a58b4bee3158</EmployeeBankGUID>
    <ValidFromDate>2014-02-01T00:00:00-05:00</ValidFromDate>
</Table>

Except it no longer works

This XPath query using MSXML; an xml variant created by Microsoft in the late 1990s before W3C was standardized on a completely different form of XPath.

DOMDocument doc = new DOMDocument();
//...load the xml...
IXMLDOMNodeList nodes = doc.selectNodes('/NewDataSet/Table[ValidFromDate>"2013-02-12"]');

MSXML ( ). 2005 , , , , , , MSXML 6.

, DOMDocument60, DOMDocument:

DOMDocument doc = new DOMDocument60();
//...load the xml...
IXMLDOMNodeList nodes = doc.selectNodes('/NewDataSet/Table[ValidFromDate>"2013-02-12"]');

XPath .

" " ?

, ,

, , , XML 2013-02-01T00:00:00-05:00 , . , , , .

, , . :

  • /NewDataSet/Table[ValidFromDate<"a"]
  • /NewDataSet/Table[ValidFromDate>"a"]
  • /NewDataSet/Table[ValidFromDate!="a"]
  • /NewDataSet/Table[ValidFromDate>"2014-02-12T00:00:00-05:00"]
  • /NewDataSet/Table[ValidFromDate<"2014-02-12T00:00:00-05:00"]
  • /NewDataSet/Table[ValidFromDate!="2014-02-12T00:00:00-05:00"]

,

, , , ?

"" XPath ?

, , XPath ?

, , , , ? , , . , , "" ?

MSXML6

, , :

DOMDocument60 GetXml(String url)
{
   XmlHttpRequest xml = CoServerXMLHTTP60.Create();
   xml.Open('GET', url, False, '', '');
   xml.Send(EmptyParam);

   DOMDocument60 doc = xml.responseXML AS DOMDocument60;

   //MSXML6 removed all kinds of features originally present (thanks W3C)
   //Need to use Microsoft proprietary extensions to get some of it back (thanks W3C)
   doc.setProperty('SelectionNamespaces', 'xmlns:ms="urn:schemas-microsoft-com:xslt"');

   return doc;
}


DOMDocument doc = GetXml('http://example.com/GetBanks.ashx?employeeID=12345');

//Finds future banks. 

//Only works in MSXML3; intentionally broken in MSXML6 (thanks W3C):
//String qry = '/NewDataSet/Table[ValidFromDate > "2014-02-12"]';

//MSXML6 compatible version of doing the above (send complaints to W3C);
String qry = '/NewDataSet/Table[ms:string-compare(ValidFromDate, "2014-02-12") >= 0]';

IXMLDOMNodeList nodes = doc.selectNodes(qry);
+4
1

XPath

"" XPath ?

XPath 1.0 , . , . , .

, , XPath ?

XPath 1.0 , / .

ms:string-compare, MSXML 4.0.

/NewDataSet/Table[
  ms:string-compare(ValidFromDate, "2014-02-12T00:00:00-05:00") > 0
]

(XML)

, , , ?

, XPath ( xmllint, libxml), translate , :

/NewDataSet/Table[
  translate(ValidFromDate, "-:T", "") < translate("2014-02-12T00:00:00-05:00", "-:T", "")
]
+4

Source: https://habr.com/ru/post/1526046/


All Articles