дубликаты топи c? См .: Как мы можем присоединиться к XPATH, используя версию 1.0?
Я работал с этим образцом данных:
<data>
<rent number="101111">
<car>
<startDate>2018-02-08</startDate>
<endDate>2018-03-05</endDate>
<Location>Toranto</Location>
<carType>BMW</carType>
<transmissionType>Automatic</transmissionType>
</car>
<person>
<licenseNumber> 02389749372 </licenseNumber>
<name>Alexa Steve</name>
<dob>1999-03-01</dob>
<phone>
<type>Home</type>
<number>44 010 1111 4567</number>
</phone>
<email> Alexa@steve.ca</email>
</person>
<price>
<Rate>100.50</Rate>
</price>
</rent>
<rent number="103311">
<car>
<startDate>2018-07-01</startDate>
<endDate>2018-09-05</endDate>
<Location>ottawa</Location>
<carType>audi 8</carType>
<transmissionType>Automatic</transmissionType>
</car>
<person>
<licenseNumber> 033329372 </licenseNumber>
<name>mike lornco</name>
<dob>1960-03-03</dob>
<phone>
<type>Home</type>
<number>44 010 1111 3333</number>
</phone>
<email> mikelornokorenco@gmail.com</email>
</person>
<price>
<Rate>300.50</Rate>
</price>
</rent>
</data>
Для XML с 2 арендными платами элементы, используйте это, чтобы получить самое длинное бронирование:
//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./following::endDate,"-","")-translate(./following::startDate,"-","")]|//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./preceding::endDate,"-","")-translate(./preceding::startDate,"-","")]
Для XML с 3 или более элементами аренды, используйте это, чтобы получить самое длинное бронирование:
//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./following::endDate,"-","")-translate(./following::startDate,"-","") and translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./preceding::endDate,"-","")-translate(./preceding::startDate,"-","")]|//rent[1][translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./following::endDate,"-","")-translate(./following::startDate,"-","") and translate(.//endDate,"-","")-translate(.//startDate,"-","")>=//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./following::endDate,"-","")-translate(./following::startDate,"-","") and translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./preceding::endDate,"-","")-translate(./preceding::startDate,"-","")]]|//rent[last()][translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./preceding::endDate,"-","")-translate(./preceding::startDate,"-","") and translate(.//endDate,"-","")-translate(.//startDate,"-","")>=//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./following::endDate,"-","")-translate(./following::startDate,"-","") and translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./preceding::endDate,"-","")-translate(./preceding::startDate,"-","")]]
Тест с http://www.xpathtester.com/xpath
РЕДАКТИРОВАТЬ: Эти выражения получат самое длинное бронирование. Чтобы получить имя человека, ему нужно "// имя" в некоторых частях выражения. При необходимости я добавлю его позже.
РЕДАКТИРОВАТЬ 2: выражения XPath, чтобы получить имя человека с самым длинным бронированием:
Для двух элементов аренды:
//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./following::endDate,"-","")-translate(./following::startDate,"-","")]//name/text()|//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./preceding::endDate,"-","")-translate(./preceding::startDate,"-","")]//name/text()
Для 3-х или более арендных элементов:
//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./following::endDate,"-","")-translate(./following::startDate,"-","") and translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./preceding::endDate,"-","")-translate(./preceding::startDate,"-","")]//name/text()|//rent[1][translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./following::endDate,"-","")-translate(./following::startDate,"-","") and translate(.//endDate,"-","")-translate(.//startDate,"-","")>=//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./following::endDate,"-","")-translate(./following::startDate,"-","") and translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./preceding::endDate,"-","")-translate(./preceding::startDate,"-","")]]//name/text()|//rent[last()][translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./preceding::endDate,"-","")-translate(./preceding::startDate,"-","") and translate(.//endDate,"-","")-translate(.//startDate,"-","")>=//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./following::endDate,"-","")-translate(./following::startDate,"-","") and translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./preceding::endDate,"-","")-translate(./preceding::startDate,"-","")]]//name/text()
Вывод данных для примера: Майк Лорнко