如何解决排序时发现的不比较类型:无法将 xs:date 与 xs:integer语言 - XSLT 进行比较



如何解决排序时发现的不可比较的类型: 无法将 xs:date 与 xs:integer 进行比较 此行显示错误O’Connor v Uber Technols., Inc. (ND Cal, Dec. 05, 2013, No. C–13–3826 EMC) 2013 US Dist Lexis 171813, rev’d on other grounds (9th Cir 2018) 904 F3d 1087:此行带有两个括号(ND Cal,2013 年 12 月 5 日,第 C–13–3826 EMC 号(和(2018 年第 9 期(。我们能不能先用括号。最后两个条目不根据最新日期移动后代,因为正则表达式用于输入 XML((.*?)((' || string-join($months, '|') || '). ([0-9]{1,2}), ([0-9]{4})).*?)
日期格式

<root>
<p content-type="emCase"><named-content content-type="emEntry">AFL-CIO v Unemployment Ins. Appeals Bd. (1994) 23 CA4th 51:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">AFL-CIO v Unemployment Ins. Appeals Bd. (1996) 13 C4th 1017:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Ambat v City &#x0026; County of San Francisco (ND Cal 2010) 693 F Supp 2d 1130, rev&#x2019;d on other grounds (9th Cir 2014) 757 F3d 1017:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Ambat v City &#x0026; County of San Francisco (9th Cir 2014) 757 F3d 1017:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Anaheim Union High Sch. Dist. (2016) PERB Dec. No. 2504E, 41 PERC 80:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Anaheim Union High Sch. Dist. (1981) PERB Dec. No. 0177E, 5 PERC 12148:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Artec Group v Klimov (ND Cal, Nov. 22, 2015) 2015 US Dist Lexis 171698:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Artec Group v Klimov (ND Cal, Dec. 22, 2015) 2016 US Dist Lexis 170194:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">O&#x2019;Connor v Uber Technols., Inc. (ND Cal, Dec. 05, 2013, No. C&#x2013;13&#x2013;3826 EMC) 2013 US Dist Lexis 171813, rev&#x2019;d on other grounds 904 F3d 1087:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">O&#x2019;Connor v Uber Technols., Inc. (2018) 904 F3d 1087:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">O&#x2019;Connor v Uber Technols., Inc. (ND Cal 2015) 82 F Supp 3d 1133:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Swarmfly, Inc. v Cloudflare, Inc. (ND Cal, May 31, 2018, No. C 17-06957 WHA) 2018 US Dist Lexis 91333:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Swarmify, Inc. v Cloudflare, Inc. (ND Cal, Sept. 28, 2018, No. C 17-06957 WHA) 2018 US Dist Lexis 168317:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Swarmify, Inc. v Cloudflare, Inc. (ND Cal, Mar. 3, 2018, No. C 17-06597 WHA) 2018 WL 112204:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Zody v Microsoft Corp. (ND Cal, June 7, 2013, No. C&#x2013;12&#x2013;00942&#x2013;YGR) 2013 US Dist Lexis 80623:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Zody v Microsoft Corp. (ND Cal, July 24, 2013, No. C&#x2013;12&#x2013;00942&#x2013;YGR) 2013 US Dist Lexis 103868:</named-content></p>
</root>

XSLT

<xsl:param name="months" as="xs:string*"
select="'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'"/>
<xsl:param name="date-pattern" as="xs:string"
select="'((.*?)((' || string-join($months, '|') || '). ([0-9]{1,2}), ([0-9]{4})).*?)'"/>
<xsl:output indent="yes"/>
<xsl:template match="@specific-use|named-content[@content-type='emSecs']"/>    
<xsl:function name="mf:extract-date" as="xs:date?">
<xsl:param name="input" as="xs:string"/>
<xsl:sequence
select="let $match := analyze-string($input, $date-pattern)/*:match[1]
return 
if ($match) 
then xs:date(
$match//*:group[@nr = 5] 
|| '-' || format-integer(index-of($months, $match//*:group[@nr = 3]), '00') 
|| '-' || format-integer($match//*:group[@nr = 4], '00'))
else ()"/>
</xsl:function>
<xsl:function name="mf:extract-year" as="xs:integer?">
<xsl:param name="input" as="xs:string"/>
<xsl:sequence
select="analyze-string($input, '((.*?)([0-9]{4}))')/*:match[1]/*:group[@nr = 2]"/>
</xsl:function>
<xsl:function name="mf:extract-sort" as="xs:string?">
<xsl:param name="input" as="xs:string"/>
<xsl:sequence
select="analyze-string($input, '((.*?))')/*:match[1]/*:group[@nr = 1]"/>
</xsl:function>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:for-each-group select="p" group-adjacent="substring-before(named-content[@content-type = 'emEntry'], '(')">
<xsl:apply-templates select="current-group()">
<xsl:sort select="let $year := mf:extract-year(.)
return if ($year) then -$year else 1"/>
<xsl:sort select="let $date := mf:extract-date(.)
return if (exists($date)) then $date else 1" order="descending"/>
<xsl:sort select="let $sort := mf:extract-sort(.)
return if ($sort) then $sort else 1" order="descending"/>
</xsl:apply-templates>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

预期输出

<root>
<p content-type="emCase">
<named-content content-type="emEntry">AFL-CIO v Unemployment Ins. Appeals Bd. (1996) 13 C4th 1017:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">AFL-CIO v Unemployment Ins. Appeals Bd. (1994) 23 CA4th 51:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">Ambat v City &amp; County of San Francisco (9th Cir 2014) 757 F3d 1017:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">Ambat v City &amp; County of San Francisco (ND Cal 2010) 693 F Supp 2d 1130, rev’d on other grounds (9th Cir 2014) 757 F3d 1017:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">Anaheim Union High Sch. Dist. (2016) PERB Dec. No. 2504E, 41 PERC 80:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">Anaheim Union High Sch. Dist. (1981) PERB Dec. No. 0177E, 5 PERC 12148:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">Artec Group v Klimov (ND Cal, Dec. 22, 2015) 2016 US Dist Lexis 170194:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">Artec Group v Klimov (ND Cal, Nov. 22, 2015) 2015 US Dist Lexis 171698:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">O’Connor v Uber Technols., Inc. (2018) 904 F3d 1087:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">O’Connor v Uber Technols., Inc. (ND Cal 2015) 82 F Supp 3d 1133:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">O’Connor v Uber Technols., Inc. (ND Cal, Dec. 05, 2013, No. C–13–3826 EMC) 2013 US Dist Lexis 171813, rev’d on other grounds (9th Cir 2018) 904 F3d 1087:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">Swarmify, Inc. v Cloudflare, Inc. (ND Cal, Sept. 28, 2018, No. C 17-06957 WHA) 2018 US Dist Lexis 168317:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">Swarmfly, Inc. v Cloudflare, Inc. (ND Cal, May 31, 2018, No. C 17-06957 WHA) 2018 US Dist Lexis 91333:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">Swarmify, Inc. v Cloudflare, Inc. (ND Cal, Mar. 3, 2018, No. C 17-06597 WHA) 2018 WL 112204:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">Zody v Microsoft Corp. (ND Cal, July 24, 2013, No. C–12–00942–YGR) 2013 US Dist Lexis 103868:</named-content>
</p>
<p content-type="emCase">
<named-content content-type="emEntry">Zody v Microsoft Corp. (ND Cal, June 7, 2013, No. C–12–00942–YGR) 2013 US Dist Lexis 80623:</named-content>
</p>
</root>

代码:https://xsltfiddle.liberty-development.net/pNmC4Jf/19

let $date := mf:extract-date(.) return if (exists($date)) then $date else 1

可能是罪魁祸首,您将需要使用一些默认xs:date,不确定您需要哪个基准日期,但您可以使用例如else xs:date('1900-01-01').

至于月份名称匹配,如果我们对月份名称使用正则表达式模式,我不确定以前的方法是否有效,但是如果我们使用附加函数

<xsl:param name="months" as="xs:string*"
select="'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June?', 'July?', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'"/>
<xsl:param name="date-pattern" as="xs:string"
select="'((.*?)((' || string-join($months, '|') || '). ([0-9]{1,2}), ([0-9]{4})).*?)'"/>
<xsl:function name="mf:index-of-match" as="xs:integer*">
<xsl:param name="patterns" as="xs:string*"/>
<xsl:param name="month-match" as="xs:string"/>
<xsl:sequence
select="for $pos in (1 to count($patterns))
return $pos[matches($month-match, $patterns[$pos])]"/>
</xsl:function>
<xsl:function name="mf:extract-date" as="xs:date?">
<xsl:param name="input" as="xs:string"/>
<xsl:sequence
select="let $match := analyze-string($input, $date-pattern)/*:match[1]
return 
if ($match) 
then xs:date(
$match//*:group[@nr = 5] 
|| '-' || format-integer(mf:index-of-match($months, $match//*:group[@nr = 3]), '00') 
|| '-' || format-integer($match//*:group[@nr = 4], '00'))
else ()"/>
</xsl:function>

https://xsltfiddle.liberty-development.net/pNmC4Jf/20 它可能会提供更多的灵活性。

最新更新