gpt4 book ai didi

xpath - Xpath,在其他两个节点之间查找节点

转载 作者:行者123 更新时间:2023-12-03 17:32:56 33 4
gpt4 key购买 nike

我需要构造一个通用XPath来找到正确的节点,其中的标准是日期和时间。例如查找“ 5 May”,“ 12:17:44”的节点

XML具有日期和时间标签。
不方便地,日期标签仅在当天的第一次出现时填充。 (始终填充时间标签)

我已经试过了:

/itemisationTable/row[@Date="03 May"]/following-sibling::row[@Time="12:17:44"]


它工作正常,但不正确,因为这

/itemisationTable/row[@Date="03 May"]/following-sibling::row[@Time="21:12:06"]


还可以找到结果,但不可以。



我的另一个问题是

/itemisationTable/row[@Date="03 May"]/following-sibling::row[@Time="09:34:13"]


应该找到一个节点,但是找不到。



如果有人可以通过XPath帮助我,我将不胜感激,因为它超出了我的Xpath技能。

这是XML的片段

<itemisationTable index="1" name="Belgium - SMS/Data" total="1.522">
<row Date="03 May" Time="09:34:13" Number="xphone.com" Description="Roaming Data" Origin="Belgium" Destination="Other Provider" InBundle="" Taxable="T" Duration="12.57 MB" Cost_exc_VAT="1.258" />
<row Date="" Time="10:43:41" Number="4428" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="" Time="10:43:44" Number="4428" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="" Time="12:17:44" Number="4408" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="" Time="21:10:50" Number="4412" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="" Time="21:11:55" Number="4412" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="04 May" Time="21:12:06" Number="4412" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="" Time="21:22:34" Number="4412" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="" Time="21:23:23" Number="4412" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="" Time="21:23:31" Number="4412" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="05 May" Time="21:23:56" Number="4412" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="" Time="21:30:45" Number="4412" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="" Time="22:24:35" Number="4431" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="" Time="22:24:38" Number="4431" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
<row Date="06 May" Time="" Number="xphone.com" Description="Roaming Data" Origin="Belgium" Destination="Other Provider" InBundle="" Taxable="T" Duration="2.59 MB" Cost_exc_VAT="0.264" />
<row Date="" Time="07:09:15" Number="4483" Description="xphone SMS" Origin="Belgium" Destination="UK" InBundle="B" Taxable="" Duration="1" Cost_exc_VAT="0.000" />
</itemisationTable>

最佳答案

整rick这并不是特别有效,但是它是一个可行的解决方案(使用XPath 2.0):

/itemisationTable
/row[@Date=$date]
/(self::row | following-sibling::row[
not(./preceding-sibling::row[@Date != ""][1]/@Date != $date)
][not(./@Date != "" and ./@Date != $date)])[@Time=$time]


使用XPath 1.0,这变得更加麻烦:

/itemisationTable
/row[@Date=$date]
/following-sibling::row[
not(./preceding-sibling::row[@Date != ""][1]/@Date != $date)
][not(./@Date != "" and ./@Date != $date)][@Time=$time]
| /itemisationTable/row[@Date=$date][not(./@Date != "" and ./@Date !=$date][@Time=$time]


通过XPath引擎设置 $date$time-例如,在XMLStarlet中,这将是 --var date='"03 May"';用 declare variable $date="03 May"在XQuery引擎中评估XPath;等等

重要的是使用 preceding-sibling回溯并查看您是否越过边界,排除所有确实存在的节点。

对于具有足够表达能力以构建有效解决方案的语言,我想切换到XQuery。



为了允许复制/粘贴测试,以下内容已在 http://www.freeformatter.com/xpath-tester.html上成功使用:

/itemisationTable   /row[@Date="03 May"]   /following-sibling::row[     not(./preceding-sibling::row[@Date != ""][1]/@Date != "03 May")   ][not(./@Date != "" and ./@Date != "03 May")][@Time="21:11:55"] | /itemisationTable/row[@Date="03 May"][not(./@Date != "" and ./@Date != "03 May")][@Time="21:11:55"]

关于xpath - Xpath,在其他两个节点之间查找节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31895603/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com