How do we get the Longest Booking in XPATH 1.0?

夙愿已清 提交于 2020-04-17 20:20:25

问题


How can I get the name of the person who has the longest Booking using the attribute StartDate and EndDate belwo i menioned the xml dummy data i have like 12 diffrent datas and more people , the version i am using for xpath is 1.0

<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>

what i did so far:

/rent/StartDate[not(text() <= preceding-sibling::StartDate/text()) and not(text() <=following-sibling::StartDate/text())]

but it's not working maybe I'm missing something any help?

How do we get the name of the person who has the Longest Booking?


回答1:


Duplicate topic ? See : how do we join in XPATH using version 1.0?

I've worked with this sample of data:

<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>

For XML with 2 rent elements, use this to get the longest booking :

//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./following::endDate,"-","")-translate(./following::startDate,"-","")]|//rent[translate(.//endDate,"-","")-translate(.//startDate,"-","")>=translate(./preceding::endDate,"-","")-translate(./preceding::startDate,"-","")]

For XML with 3 or more rent elements, use this to get the longest booking :

//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,"-","")]]

Test with http://www.xpathtester.com/xpath

EDIT : These expressions will get the longest booking. To get the person name, it needs "//name" in some parts of the expression. If necessary I'll add it later.

EDIT 2 : XPath expressions to get the name of the person with the longest booking :

For 2 rents elements :

//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()

For 3 or more rent elements :

//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()

Output for your sample data : mike lornco



来源:https://stackoverflow.com/questions/61255658/how-do-we-get-the-longest-booking-in-xpath-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!