我们如何使用 1.0 版加入 XPATH? [英] how do we join in XPATH using version 1.0?
问题描述
我正在尝试获取 lN 和一个人的姓名以及 > 300 的汽车租赁价格
I'm trying to get the lN and the names of a person and the price of a car rental which is > 300
下面是我的 XML 示例:
<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>
</vehicle>
<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>
我想要做的是获得一个租车人的驾照和年龄,>= 300
有效的单独查询:
//price[Rate >= 300]
//person/name/licenseNumber
//person/name
//car/carType
推荐答案
这些表达式应该可以工作(结果是一个包含所需信息的格式化字符串):
These expressions should work (result is a formatted string containing the required information) :
Xpath 1.0:
concat(normalize-space(//Rate[.>=300]/preceding::licenseNumber[1]),"|",//Rate[.>=300]/preceding::name[1],"|",2020-substring(//Rate[.>=300]/preceding::dob[1],1,4))
Xpath 2.0:
concat(replace(string-join(//Rate[.>=300]/preceding::person[1]/*[position()<3],"|"),"\W(\d+)\W(|.+)","$1$2"),"|",year-from-date(current-date())-year-from-date(//Rate[.>=300]/preceding::dob[1]))
输出(执照号、姓名、年龄):
Output (licence number, name, age) :
'033329372|mike lornco|60'
测试:https://www.freeformatter.com/xpath-tester.html
我想你有多个项目.您可以使用 :
I suppose you have multiple items. You can iterate with :
concat(normalize-space((//Rate[.>=300]/preceding::licenseNumber[1])[x]),"|",(//Rate[.>=300]/preceding::name[1])[x],"|",2020-number(substring((//Rate[.>=300]/preceding::dob[x])[1],1,4)))
其中 [x] 从 1 到 XXX.
Where [x] starts from 1 to XXX.
您的 XML 数据包含错误.这是我用过的:
EDIT : Your XML data contains errors. This is what I've used :
<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>
编辑 2:XPath 1.0 表达式的解释:
EDIT 2 : Explanation of the XPath 1.0 expression :
我们使用concat
来连接多个操作的结果(每个操作用,"分隔):
We use concat
to join the result of multiple operations (each one seperated with a ",") :
normalize-space(//Rate[.>=300]/preceding::licenseNumber[1])
:我们得到一个拥有汽车租赁的人的驾照号码是 >= 300(XPath 语法).我们使用 normalize-space() 来删除前导和关闭空格.
normalize-space(//Rate[.>=300]/preceding::licenseNumber[1])
: we get the licence number of a person who has a car rental which is >= 300 (XPath syntax).
We use normalize-space() to remove leading and closing whitespaces.
我们放了一个|"将结果与下一个结果分开.
We put a "|" to separate the result from the next one.
//Rate[.>=300]/preceding::name[1]
:我们得到一个人的姓名,该人的租车金额 >= 300(XPath 语法).
//Rate[.>=300]/preceding::name[1]
: we get the name of a person who has a car rental which is >= 300 (XPath syntax).
2020-substring(//Rate[.>=300]/preceding::dob[1],1,4)
:我们得到一个租车人的生日即 >= 300.有了这个生日,我们用 susbtring() 提取出生年份(1,4"意味着我们想要从字符 1 到字符 4 的值).最后我们用 2020 减去这个值来得到这个人的年龄.
2020-substring(//Rate[.>=300]/preceding::dob[1],1,4)
: we get the birthdate of a person who has a car rental which is >= 300. With this birthdate, we extract with susbtring() the year of birth ("1,4" means we want values from character 1 to character 4). Finally we substract this value from 2020 to get the age of the person.
编辑 3:XPath 1.0 以获得最长的预订,请参阅 我们如何在 XPATH 1.0 中获得最长预订?
EDIT 3 : XPath 1.0 to get the longest booking, see How do we get the Longest Booking in XPATH 1.0?
这篇关于我们如何使用 1.0 版加入 XPATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!