首页 > 解决方案 > 如何在 xpath 3.1 中使用分析字符串

问题描述

假设我想将“03:02:22:11”之类的东西转换为毫秒。如何使用 Xpath 3.1 执行此操作?

我试过:analyze-string(/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 属性使用分析字符串将它们转换为毫秒吗?

简短的更新:我已经实现了这样的东西:

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-Expression 如何处理此问题的一些示例:

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

标签: xmlxsltxpathxsd

解决方案


假设我想将“03:02:22:11”之类的东西转换为毫秒。如何使用 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, '(..):(..):(..):(..)', 'PT$1H$2M$3.$4S'))
    div xs:dayTimeDuration('PT0.001S')

推荐阅读