如何在 xpath 3.1 中使用分析字符串
How to use analyze-string in xpath 3.1
假设我想将“03:02:22:11”之类的内容转换为毫秒。我如何使用 Xpath 3.1 执行此操作?
我试过:分析字符串(/pc:podcast/pc:episode/pc:chapter, [(([0-9]?[0-9]:)?([0 -5]?[0-9]:))?([0-5]?[0-9])(.[0-9][0-9]?[0-9]?)?] 没有任何结果。
下面是我的xml-文件
<?xml version="1.0" encoding="UTF-8"?>
<pc:podcast
xmlns:pc="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast/0.3"
xmlns:pt="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast-transcript/0.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast/0.3
podcast.xsd
https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast-transcript/0.1
podcast-transcript.xsd"
url="https://forschergeist.de/"
version="0.3">
<pc:title>Forschergeist</pc:title>
<pc:persons>
<pc:person pid="timpritlove" gender="male">Tim Pritlove</pc:person>
<pc:person pid="ritaadrian" gender="female">Rita Adrian</pc:person>
<pc:person pid="peterpurgathofer" gender="male">Peter Purgathofer</pc:person>
<pc:person pid="justushaucap" gender="male">Justus Haucap</pc:person>
<pc:person pid="oliverparodi" gender="male">Oliver Parodi</pc:person>
<pc:person pid="alinakokoschka" gender="female">Alina Kokoschka</pc:person>
</pc:persons>
<pc:episode episode="73" title="Klimawandel in Seen" date="2019-11-19T19:35:00" url="https://forschergeist.de/podcast/fg073-klimawandel-in-seen/" duration="01:23:14">
<pc:contributor pid="timpritlove" role="host"/>
<pc:contributor pid="ritaadrian" role="guest" />
<pc:chapter number="1" duration="00:41" title="Intro" />
<pc:chapter number="2" duration="01:14" title="Begrüßung" />
<pc:chapter number="3" duration="04:41" title="Leibniz-Institut für Gewässerökologie und Binnenfischerei " />
<pc:chapter number="4" duration="02:35" title="Persönlicher Werdegang" />
<pc:chapter number="5" duration="04:44" title="Wissenschaftskultur in den USA" />
<pc:chapter number="6" duration="06:34" title="Das Ökosystem der Binnenseen" />
<pc:chapter number="7" duration="15:04" title="Langzeitbeobachtung der Seen" />
<pc:chapter number="8" duration="12:46" title="Erkenntnisse aus den Messungen" />
<pc:chapter number="9" duration="08:27" title="Veränderungen durch den Klimawandel" />
<pc:chapter number="10" duration="03:10" title="Erforderliche Maßnahmen" />
<pc:chapter number="11" duration="11:52" title="Wissenschaftliche Arbeit im IPCC" />
<pc:chapter number="12" duration="10:02" title="Datenmanagement" />
<pc:chapter number="13" duration="01:06" title="Ausklang" />
<pt:transcript
xmlns:pt="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast-transcript/0.1"
version="0.1">
</pt:transcript>
</pc:episode>
<pc:episode episode="72" title="Verantwortung in der Informatik" date="2019-10-01T11:00:00" url="https://forschergeist.de/podcast/fg072-verantwortung-in-der-informatik/" duration="01:53:23">
<pc:contributor pid="timpritlove" role="host"/>
<pc:contributor pid="peterpurgathofer" role="guest" />
<pc:chapter number="1" duration="00:42" title="Intro" />
<pc:chapter number="2" duration="00:46" title="Begrüßung" />
<pc:chapter number="3" duration="08:26" title="Peter Purgathofer" />
<pc:chapter number="4" duration="09:26" title="Ethik und Moral in der Informatik" />
<pc:chapter number="5" duration="06:41" title="Entscheidung und Verantwortung" />
<pc:chapter number="6" duration="11:22" title="Denkweisen in der Informatik" />
<pc:chapter number="7" duration="00:55" title="Strukturierung des Kurses" />
<pc:chapter number="8" duration="05:31" title="Pre-Scientific Thinking" />
<pc:chapter number="9" duration="04:15" title="Scientific Thinking" />
<pc:chapter number="10" duration="06:01" title="Mathematical Thinking" />
<pc:chapter number="11" duration="07:35" title="Computational Thinking" />
<pc:chapter number="12" duration="09:57" title="Design Thinking" />
<pc:chapter number="13" duration="10:20" title="Critical Thinking und Diversity" />
<pc:chapter number="14" duration="05:15" title="Responsible Thinking" />
<pc:chapter number="15" duration="04:00" title="Creative Thinking" />
<pc:chapter number="16" duration="02:18" title="Economical Thinking" />
<pc:chapter number="17" duration="04:07" title="Criminal Thinking" />
<pc:chapter number="18" duration="02:07" title="Denkweisen für die Wissenschaft" />
<pc:chapter number="19" duration="11:45" title="Der Reifegrad der Informatik" />
<pc:chapter number="20" duration="01:43" title="Ausklang" />
</pc:episode>
</pc:podcast>
我可以对 pc:chapter 中的所有 @duration 属性使用 analyze-string 将它们转换为毫秒吗?
简短更新:我已经实现了这样的东西:
let $string :=analyze-string(string(//pc:episode[1]/@duration), "(([0-9]?[0-9]:)?([0-5]?[0-9]:))?([0-5]?[0-9])(\.[0-9][0-9]?[0-9]?)?" ) return xs:integer($string[1])*3600000 + xs:integer($string[2])*60000 + xs:decimal($string[3] || '.' || $string[4])*1000
但现在我收到无法执行 XPath 操作的错误。无法将字符串“01:23:14”转换为整数,所以我一定做错了什么,但我不知道是什么。
感谢 Xpath 指南,但这并不能解决我的问题(我需要弄清楚每当出现 13:23 之类的内容时,他应该将其转换为毫秒,否则当字符串类似于 '12 :32:32.221' 然后他应该将其转换为毫秒: if(string(/pc:podcast/pc:episode[1]/@duration != '00:00:00') then...
以下是 Xpath 表达式应如何处理此问题的一些示例:
0.444 → 444 (0 * 1000 + 0.444 * 1000)
12.23 → 12230 (12 * 1000 + 0.23 * 1000 + 0.0 * 1000)
12:46 → 766.000 (12 * 60 * 1000 + 46 * 1000 + 0.0 * 1000)
01:53:23 → 6803000 (1 * 60 * 60 * 1000 + 53 * 60 * 1000 + 23 * 1000 + 0.0 * 1000)
01:53:23.123 → 6803123 (1 * 60 * 60 * 1000 + 53 * 60 * 1000 + 23 * 1000 + 0.123 * 1000)
我也试过这个,它似乎有效:
let $string :=tokenize(string(//pc:episode[1]/@duration), ':') return xs:integer($string[1])*3600000 + xs:integer($string[2])*60000 + xs:decimal($string[3] || '.' || $string[4])*1000
我会利用 xs:time
和 xs:duration
数据类型及其支持的算术运算:
let $time := xs:time('01:53:23.123'), $midnight := xs:time('00:00:00'), $duration := $time - $midnight return ($duration, $duration div xs:dayTimeDuration('PT0.001S'))
Supposing I want to convert something like "03:02:22:11" into milliseconds. How do I do this with Xpath 3.1?
首先你没有解释语义。大概这意味着 3 小时 2 分 22.11 秒?
我愿意
let $t := tokenize($input, ':')
return xs:integer($t[1])*3600000 +
xs:integer($t[2])*60000 +
xs:decimal($t[3] || '.' || $t[4])*1000
我不明白你为什么要为此使用 fn:analyze-string
。
另一种选择是
xs:dayTimeDuration(replace($input, '(..):(..):(..):(..)', 'PTHM.S'))
div xs:dayTimeDuration('PT0.001S')
我想我明白了:
let $string :="01:53:23.123", $regexpress :="(([0-9]?[0-9]):?([0-5]?[0-9])):?([0-5]?[0-9])(\.[0-9][0-9]?[0-9]?)?" return xs:integer(analyze-string($string, $regexpress)//fn:group[@nr="2"]/string())*3600000+xs:integer(analyze-string($string, $regexpress)//fn:group[@nr="3"]/string())*60000 + xs:integer(analyze-string($string, $regexpress)//fn:group[@nr="4"]/string())*1000 + xs:decimal(analyze-string($string, $regexpress)//fn:group[@nr="5"]/string())*1000
现在我只需要 if then else 表达式...
假设我想将“03:02:22:11”之类的内容转换为毫秒。我如何使用 Xpath 3.1 执行此操作?
我试过:分析字符串(/pc:podcast/pc:episode/pc:chapter, [(([0-9]?[0-9]:)?([0 -5]?[0-9]:))?([0-5]?[0-9])(.[0-9][0-9]?[0-9]?)?] 没有任何结果。
下面是我的xml-文件
<?xml version="1.0" encoding="UTF-8"?>
<pc:podcast
xmlns:pc="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast/0.3"
xmlns:pt="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast-transcript/0.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast/0.3
podcast.xsd
https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast-transcript/0.1
podcast-transcript.xsd"
url="https://forschergeist.de/"
version="0.3">
<pc:title>Forschergeist</pc:title>
<pc:persons>
<pc:person pid="timpritlove" gender="male">Tim Pritlove</pc:person>
<pc:person pid="ritaadrian" gender="female">Rita Adrian</pc:person>
<pc:person pid="peterpurgathofer" gender="male">Peter Purgathofer</pc:person>
<pc:person pid="justushaucap" gender="male">Justus Haucap</pc:person>
<pc:person pid="oliverparodi" gender="male">Oliver Parodi</pc:person>
<pc:person pid="alinakokoschka" gender="female">Alina Kokoschka</pc:person>
</pc:persons>
<pc:episode episode="73" title="Klimawandel in Seen" date="2019-11-19T19:35:00" url="https://forschergeist.de/podcast/fg073-klimawandel-in-seen/" duration="01:23:14">
<pc:contributor pid="timpritlove" role="host"/>
<pc:contributor pid="ritaadrian" role="guest" />
<pc:chapter number="1" duration="00:41" title="Intro" />
<pc:chapter number="2" duration="01:14" title="Begrüßung" />
<pc:chapter number="3" duration="04:41" title="Leibniz-Institut für Gewässerökologie und Binnenfischerei " />
<pc:chapter number="4" duration="02:35" title="Persönlicher Werdegang" />
<pc:chapter number="5" duration="04:44" title="Wissenschaftskultur in den USA" />
<pc:chapter number="6" duration="06:34" title="Das Ökosystem der Binnenseen" />
<pc:chapter number="7" duration="15:04" title="Langzeitbeobachtung der Seen" />
<pc:chapter number="8" duration="12:46" title="Erkenntnisse aus den Messungen" />
<pc:chapter number="9" duration="08:27" title="Veränderungen durch den Klimawandel" />
<pc:chapter number="10" duration="03:10" title="Erforderliche Maßnahmen" />
<pc:chapter number="11" duration="11:52" title="Wissenschaftliche Arbeit im IPCC" />
<pc:chapter number="12" duration="10:02" title="Datenmanagement" />
<pc:chapter number="13" duration="01:06" title="Ausklang" />
<pt:transcript
xmlns:pt="https://purl.org/net/hbuschme/teaching/2019ws-infostruk/podcast-transcript/0.1"
version="0.1">
</pt:transcript>
</pc:episode>
<pc:episode episode="72" title="Verantwortung in der Informatik" date="2019-10-01T11:00:00" url="https://forschergeist.de/podcast/fg072-verantwortung-in-der-informatik/" duration="01:53:23">
<pc:contributor pid="timpritlove" role="host"/>
<pc:contributor pid="peterpurgathofer" role="guest" />
<pc:chapter number="1" duration="00:42" title="Intro" />
<pc:chapter number="2" duration="00:46" title="Begrüßung" />
<pc:chapter number="3" duration="08:26" title="Peter Purgathofer" />
<pc:chapter number="4" duration="09:26" title="Ethik und Moral in der Informatik" />
<pc:chapter number="5" duration="06:41" title="Entscheidung und Verantwortung" />
<pc:chapter number="6" duration="11:22" title="Denkweisen in der Informatik" />
<pc:chapter number="7" duration="00:55" title="Strukturierung des Kurses" />
<pc:chapter number="8" duration="05:31" title="Pre-Scientific Thinking" />
<pc:chapter number="9" duration="04:15" title="Scientific Thinking" />
<pc:chapter number="10" duration="06:01" title="Mathematical Thinking" />
<pc:chapter number="11" duration="07:35" title="Computational Thinking" />
<pc:chapter number="12" duration="09:57" title="Design Thinking" />
<pc:chapter number="13" duration="10:20" title="Critical Thinking und Diversity" />
<pc:chapter number="14" duration="05:15" title="Responsible Thinking" />
<pc:chapter number="15" duration="04:00" title="Creative Thinking" />
<pc:chapter number="16" duration="02:18" title="Economical Thinking" />
<pc:chapter number="17" duration="04:07" title="Criminal Thinking" />
<pc:chapter number="18" duration="02:07" title="Denkweisen für die Wissenschaft" />
<pc:chapter number="19" duration="11:45" title="Der Reifegrad der Informatik" />
<pc:chapter number="20" duration="01:43" title="Ausklang" />
</pc:episode>
</pc:podcast>
我可以对 pc:chapter 中的所有 @duration 属性使用 analyze-string 将它们转换为毫秒吗?
简短更新:我已经实现了这样的东西:
let $string :=analyze-string(string(//pc:episode[1]/@duration), "(([0-9]?[0-9]:)?([0-5]?[0-9]:))?([0-5]?[0-9])(\.[0-9][0-9]?[0-9]?)?" ) return xs:integer($string[1])*3600000 + xs:integer($string[2])*60000 + xs:decimal($string[3] || '.' || $string[4])*1000
但现在我收到无法执行 XPath 操作的错误。无法将字符串“01:23:14”转换为整数,所以我一定做错了什么,但我不知道是什么。
感谢 Xpath 指南,但这并不能解决我的问题(我需要弄清楚每当出现 13:23 之类的内容时,他应该将其转换为毫秒,否则当字符串类似于 '12 :32:32.221' 然后他应该将其转换为毫秒: if(string(/pc:podcast/pc:episode[1]/@duration != '00:00:00') then...
以下是 Xpath 表达式应如何处理此问题的一些示例:
0.444 → 444 (0 * 1000 + 0.444 * 1000)
12.23 → 12230 (12 * 1000 + 0.23 * 1000 + 0.0 * 1000)
12:46 → 766.000 (12 * 60 * 1000 + 46 * 1000 + 0.0 * 1000)
01:53:23 → 6803000 (1 * 60 * 60 * 1000 + 53 * 60 * 1000 + 23 * 1000 + 0.0 * 1000)
01:53:23.123 → 6803123 (1 * 60 * 60 * 1000 + 53 * 60 * 1000 + 23 * 1000 + 0.123 * 1000)
我也试过这个,它似乎有效:
let $string :=tokenize(string(//pc:episode[1]/@duration), ':') return xs:integer($string[1])*3600000 + xs:integer($string[2])*60000 + xs:decimal($string[3] || '.' || $string[4])*1000
我会利用 xs:time
和 xs:duration
数据类型及其支持的算术运算:
let $time := xs:time('01:53:23.123'), $midnight := xs:time('00:00:00'), $duration := $time - $midnight return ($duration, $duration div xs:dayTimeDuration('PT0.001S'))
Supposing I want to convert something like "03:02:22:11" into milliseconds. How do I do this with Xpath 3.1?
首先你没有解释语义。大概这意味着 3 小时 2 分 22.11 秒?
我愿意
let $t := tokenize($input, ':')
return xs:integer($t[1])*3600000 +
xs:integer($t[2])*60000 +
xs:decimal($t[3] || '.' || $t[4])*1000
我不明白你为什么要为此使用 fn:analyze-string
。
另一种选择是
xs:dayTimeDuration(replace($input, '(..):(..):(..):(..)', 'PTHM.S'))
div xs:dayTimeDuration('PT0.001S')
我想我明白了:
let $string :="01:53:23.123", $regexpress :="(([0-9]?[0-9]):?([0-5]?[0-9])):?([0-5]?[0-9])(\.[0-9][0-9]?[0-9]?)?" return xs:integer(analyze-string($string, $regexpress)//fn:group[@nr="2"]/string())*3600000+xs:integer(analyze-string($string, $regexpress)//fn:group[@nr="3"]/string())*60000 + xs:integer(analyze-string($string, $regexpress)//fn:group[@nr="4"]/string())*1000 + xs:decimal(analyze-string($string, $regexpress)//fn:group[@nr="5"]/string())*1000
现在我只需要 if then else 表达式...