Xml 将字符串转换为类似“quot;01:53:23.123“;仅使用analyze string,if-then-else和let表达式使用xpath将属性转换为毫秒

Xml 将字符串转换为类似“quot;01:53:23.123“;仅使用analyze string,if-then-else和let表达式使用xpath将属性转换为毫秒,xml,xpath,Xml,Xpath,我的问题是,我需要使用if-then-else表达式将属性)中的字符串转换为毫秒(如果字符串中没有使用小时或分钟,那么就转换为毫秒…),我希望我的问题是清楚的 下面是一个示例XML代码(我以前在另一个问题中发布过): 例如,如果我有一个像12:46这样的字符串,我想使用if-then-else表达式转换它,就像上面提到的那样。如果字符串为12:46,则为12:46→ 766.000(12*60*1000+46*1000+0.0*1000)如果为01:53:23,则为01:53:23.123→ 6

我的问题是,我需要使用if-then-else表达式将属性
中的字符串转换为毫秒(如果字符串中没有使用小时或分钟,那么就转换为毫秒…),我希望我的问题是清楚的

下面是一个示例XML代码(我以前在另一个问题中发布过):

例如,如果我有一个像12:46这样的字符串,我想使用if-then-else表达式转换它,就像上面提到的那样。如果字符串为12:46,则为12:46→ 766.000(12*60*1000+46*1000+0.0*1000)如果为01:53:23,则为01:53:23.123→ 6803123 (1 * 60 * 60 * 1000 + 53 * 60 * 1000 + 23 * 1000 + 0.123 * 1000). 我必须使用分析字符串并让它

我的做法如下:

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
然而,这似乎不起作用。此外,我需要if-then-else表达式:

比如:

if(string(//pc:episode[1]/@duration) != '00:00:00') then only convert the seconds into milliseconds else convert like above.  
我想我找到了一个可能的解决方案,尽管它不是我想要的:

if(string(//pc:episode[1]/@duration) = "([0-9]?[0-9]:)?([0-5]?[0-9]:))?([0-5]?[0-9])(\.[0-9][0-9]?[0-9]?)?") then 'error' else let $string1:=string(//pc:episode[1]/@duration), $string :=tokenize($string1, ':') return xs:integer($string[1])*3600000 + xs:integer($string[2])*60000 + xs:decimal($string[3] || '.' || $string[4])*1000 

请解释为什么您必须使用XPath语言的特定子集。更具体地说,我可以想出很多解决这个问题的好办法,不涉及let、analyze string、,或者如果不是这样,为什么这样的解决方案是不可接受的?@MichaeI Kay我也可以使用tokenize函数,但根据我的练习,建议使用analyze string函数。从简单开始,如果您想将
分析字符串
与正则表达式一起使用,但不知道如何处理其结果,请首先查看其结果。不清楚为什么您认为将
analyze string
调用的结果绑定到一个变量,然后使用位置谓词是有意义的,结果是单个元素,例如,请参见。我可以将匹配组存储到变量中吗?如何存储?
if(string(//pc:episode[1]/@duration) != '00:00:00') then only convert the seconds into milliseconds else convert like above.  
if(string(//pc:episode[1]/@duration) = "([0-9]?[0-9]:)?([0-5]?[0-9]:))?([0-5]?[0-9])(\.[0-9][0-9]?[0-9]?)?") then 'error' else let $string1:=string(//pc:episode[1]/@duration), $string :=tokenize($string1, ':') return xs:integer($string[1])*3600000 + xs:integer($string[2])*60000 + xs:decimal($string[3] || '.' || $string[4])*1000