如何忽略XSLT中完整日期功能中的月份

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何忽略XSLT中完整日期功能中的月份相关的知识,希望对你有一定的参考价值。

完整的Jan中如何忽略月份(例如FebDec,.. date function)。因为要求仅是yearday降序,所以我输入的最后两行s / b首先是(Sept. 24, 2015,),其后是(Oct. 1, 2015,)。输入XML

<root>
<p content-type="emCase"><named-content content-type="emEntry">A.H. Emery Co. v Marcan Prods. Corp. (SD NY 1967) 268 F Supp 289, aff&#x2019;d (2d Cir 1968) 389 F2d 11:</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">AFL-CIO v Unemployment Ins. Appeals Bd. (1996) 13 C4th 1017:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Mendoza v Nordstrom, Inc. (2017) 2 C5th 1074:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Mendoza v Nordstrom, Inc. (2018) 2 C5th 1074:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Mendoza v Nordstrom, Inc. (9th Cir 2017) 865 F3d 1261:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Frlekin v Apple, Inc. (9th Cir 2017) 870 F3d 867:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Frlekin v Apple, Inc. (review granted Sept. 20, 2017, S243805) 2017 Cal Lexis 7496:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Bradford Technols., Inc. v NCV Software.com (ND Cal, Aug. 6, 2013, No. C 11&#x2013;04621 EDL) 2013 US Dist Lexis 111502:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Bradford Technols., Inc. v NCV Software.com (ND Cal, Jan. 4, 2013, No. C 11&#x2013;04621 EDL) 2013 US Dist Lexis 1592:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Appler v Mead Johnson &amp; Co., LLC (SD Ind, Oct. 1, 2015, No. 3:14&#x2013;cv&#x2013;166&#x2013;RLY-WGH) 2015 US Dist Lexis 133769:</named-content></p>
<p content-type="emCase"><named-content content-type="emEntry">Appler v Mead Johnson &amp; Co., LLC (SD Ind, Sept. 24, 2015, No. 3:14&#x2013;cv&#x2013;166&#x2013;RLY-WGH) 2015 US Dist Lexis 128182:</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: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">A.H. Emery Co. v Marcan Prods. Corp. (SD NY 1967) 268 F Supp 289, aff’d (2d Cir 1968) 389 F2d 11:</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">AFL-CIO v Unemployment Ins. Appeals Bd. (1994) 23 CA4th 51:</named-content>
   </p>
   <p content-type="emCase">
      <named-content content-type="emEntry">Mendoza v Nordstrom, Inc. (2018) 2 C5th 1074:</named-content>
   </p>
   <p content-type="emCase">
      <named-content content-type="emEntry">Mendoza v Nordstrom, Inc. (9th Cir 2017) 865 F3d 1261:</named-content>
   </p>
   <p content-type="emCase">
      <named-content content-type="emEntry">Mendoza v Nordstrom, Inc. (2017) 2 C5th 1074:</named-content>
   </p>
   <p content-type="emCase">
      <named-content content-type="emEntry">Frlekin v Apple, Inc. (review granted Sept. 20, 2017, S243805) 2017 Cal Lexis 7496:</named-content>
   </p>
   <p content-type="emCase">
      <named-content content-type="emEntry">Frlekin v Apple, Inc. (9th Cir 2017) 870 F3d 867:</named-content>
   </p>
   <p content-type="emCase">
      <named-content content-type="emEntry">Bradford Technols., Inc. v NCV Software.com (ND Cal, Aug. 6, 2013, No. C 11–04621 EDL) 2013 US Dist Lexis 111502:</named-content>
   </p>
   <p content-type="emCase">
      <named-content content-type="emEntry">Bradford Technols., Inc. v NCV Software.com (ND Cal, Jan. 4, 2013, No. C 11–04621 EDL) 2013 US Dist Lexis 1592:</named-content>
   </p>
   <p content-type="emCase">
      <named-content content-type="emEntry">Appler v Mead Johnson &amp; Co., LLC (SD Ind, Sept. 24, 2015, No. 3:14–cv–166–RLY-WGH) 2015 US Dist Lexis 128182:</named-content>
   </p>
   <p content-type="emCase">
      <named-content content-type="emEntry">Appler v Mead Johnson &amp; Co., LLC (SD Ind, Oct. 1, 2015, No. 3:14–cv–166–RLY-WGH) 2015 US Dist Lexis 133769:</named-content>
   </p>
</root>

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

答案

代替在]中使用的日期排序键>

<xsl:sort select="let $date := mf:extract-date(.)
                  return if (exists($date)) then $date else 1" order="descending"/>

简单地比较日期

<xsl:sort select="let $date := mf:extract-date(.)
                  return if (exists($date)) then day-from-date($date) else 1" order="descending"/>

第一键中已经使用了年份。

以上是关于如何忽略XSLT中完整日期功能中的月份的主要内容,如果未能解决你的问题,请参考以下文章

如果没有给出参数,如何忽略添加到 XSLT 文件中的参数?

SQL/ms-access 中的条件仅考虑月份中的某一天,而不是完整日期

SQL里怎么把日期截取为月份

分割逗号时,如何忽略括号中的逗号?

如何从 Presto 中的日期获取月份名称

从jooq中的日期获取月份名称