根据XSLT中的属性名称值进行匹配来修改日期

问题描述 投票:0回答:1

我有一个元素数组,其中:value元素可以有不同的值。如果元素包含祖鲁格式的日期,即:2019-04-17T10:42:48.0135859,我需要将其更改为YYYY-MM-DD格式。我已经提出了一个解决方案。但是,我对匹配i:type =“b:dateTime”更感兴趣。这意味着如果i:type等于或包含b:dateTime,那么XSLT将获取日期并执行所需的转换。

输入XML是:

<Properties
xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
>
<a:KeyValueOfstringanyType>
<a:Key>dtDynamicModifyDate</a:Key>
<a:Value i:type="b:dateTime"
xmlns:b="http://www.w3.org/2001/XMLSchema"
>2019-04-17T10:42:48.0135859</a:Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
<a:Key>tiEnrollmentStatus</a:Key>
<a:Value i:type="b:string"
xmlns:b="http://www.w3.org/2001/XMLSchema"
>Enrolled</a:Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
<a:Key>tiNumberOfEnrollments</a:Key>
<a:Value i:type="b:int"
xmlns:b="http://www.w3.org/2001/XMLSchema"
>1</a:Value>
</a:KeyValueOfstringanyType>
<a:KeyValueOfstringanyType>
<a:Key>dtModifyDate</a:Key>
<a:Value i:type="b:dateTime"
xmlns:b="http://www.w3.org/2001/XMLSchema"
>2019-04-16T15:57:39.331-04:00</a:Value>
</a:KeyValueOfstringanyType>
</Properties>

转型可在这里找到:https://xsltfiddle.liberty-development.net/ncdD7mC/1

而不是这个条件,我想要检查上述条件(i:类型等于或包含b:dateTime)

<xsl:when test="contains($payload/*[local-name()='Value'], '-') and contains($payload/*[local-name()='Value'], 'T') and contains($payload/*[local-name()='Value'], ':')"> 

任何指向XPATH的指针都将受到赞赏。

干杯,谢拉

xslt xslt-1.0
1个回答
0
投票

我认为你正在寻找的表达是......

<xsl:when test="$payload/*[local-name()='Value']/@*[name()='i:type'] ='b:dateTime'">

但是,如果名称空间前缀发生更改,则会失败,因此您可能应该这样做:

<xsl:when test="$payload/*[local-name()='Value']/@*[local-name()='type'] ='b:dateTime'">

但是,如果在不同的命名空间中有两个名为type的属性,则可能无法为您提供正确的结果。唯一真正的解决方案是在XSLT中声明xmlns:i命名空间,然后你会这样做:

<xsl:when test="$payload/*[local-name()='Value']/@i:type ='b:dateTime'">
© www.soinside.com 2019 - 2024. All rights reserved.