【问题标题】:How to use analyze-string in xpath 3.1如何在 xpath 3.1 中使用分析字符串
【发布时间】:2020-04-04 12:26:34
【问题描述】:

假设我想将“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

【问题讨论】:

  • 如果您的问题不清楚,请告诉我
  • 您是否有权访问任何提供 XPath 3.1 API 的软件? Saxon 9(XPath 3.1 的 9.8 和更高版本)有一个。这样就可以回答有关如何在 XSLT 之外使用 XPath 3.1 的一般问题。至于将duration转换为毫秒的问题,为什么要用analyze-string来解决?
  • @Martin Honnen 因为我们(我是一名大学生)必须使用分析字符串函数。此外,我们必须使用 if else 语句来区分小时和分钟)。所有这些都可以使用 XPath-Tester 工具完成。(链接 https://www.freeformatter.com/xpath-tester.html)
  • 那个工具似乎不支持 XPath 3.1,只支持 3.0。但是,如果您有一个工具来测试/评估纯 XPath 3,那么请使用它并且不要在您的问题中询问“没有 xslt”。如果这是你的作业,那么我想你必须自己完成。
  • 我建议您开始阅读 XPath 教程,例如参见 altova.com/training/xpath3/…

标签: xml xslt xpath xsd


【解决方案1】:

假设我想将“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')

【讨论】:

  • 是的,这似乎是一个合理的答案。然而,我的目标是将 fn:analyze-string 与 if-then-else 表达式(以及变量的 let 表达式)结合使用。还有一个问题:如何使用 div 和 mod 以及 string-joint 将毫秒转换回标准形式(HH:MM:SS:Milliseconds)?
  • 那么如何使用 if-else.function 来确定字符串中是否包含小时和分钟?很抱歉这个问题,但我们之前没有讨论过这个话题,讲师/讲师也没有解释过。我想要类似“如果(小时和分钟在字符串中)然后标记化/fn:analyze-string 并返回字符串的整数值。
  • 无论如何都要编辑问题以提供更精确的问题说明。但请不要通过评论答案来改变问题。如果您想更改问题,通常最好从头开始使用新线程。
【解决方案2】:

我将利用 xs:timexs: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'))

【讨论】:

    【解决方案3】:

    我想我明白了:

    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 表达式...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多