在XSLT中将日期从名称转换为数字

时间:2016-05-25 19:39:20

标签: xml date xslt

我正在尝试转换格式为xml的日期 11-APR-16,我想解析日期并在2016-04-11或yyyy-mm-dd转换它

我目前正在使用Xslt代码转换日期:

    catch(PDOException $e) {
    require_once 'includes/log.php';
}

但是我仍然返回值APR,我应该如何将日期从字母转换为数字值

3 个答案:

答案 0 :(得分:3)

因为您想将变量与字符串进行比较 从例如:

更改您的<xsl:when test="$month = 'JAN'">01</xsl:when>
test="$month = JAN"

到:

$month

JAN将变量<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="UTF-8"/> <xsl:variable name="Date" select="'11-APR-16'"/> <xsl:variable name="Day" select="substring($Date,1,2)" /> <xsl:variable name="month" select="substring($Date,4,3)" /> <xsl:variable name="Year" select="substring($Date,8,2)" /> <xsl:template name="get-month-abbreviation"> <xsl:choose> <xsl:when test="$month = 'APR'">04</xsl:when> <xsl:otherwise>error: <xsl:value-of select="$month"/></xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="/"> <xsl:call-template name="get-month-abbreviation" /> </xsl:template> </xsl:styles 与当前节点中的元素<activity android:name="com.example.android.LinkHandlerActivity" android:label="@string/title_gizmos" > <intent-filter android:label="@string/filter_title_viewgizmos"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- Accepts URIs that begin with "http://www.example.com/gizmos” --> <data android:scheme="http" android:host="www.example.com" android:pathPrefix="/gizmos" /> <!-- note that the leading "/" is required for pathPrefix--> <!-- Accepts URIs that begin with "example://gizmos” --> <data android:scheme="example" android:host="gizmos" /> </intent-filter> 进行比较。

更新为ARP添加工作示例(仅限)

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();

答案 1 :(得分:2)

每个人似乎都在假设您正在使用XSLT 1.0,尽管您没有将其作为约束,但值得了解的是,XSLT 2.0中有很多东西可以使这个问题更容易。

首先也是最明显的,你可以使用正则表达式:

<xsl:analyze-string select="$date" regex="([0-9]{2})\-([A-Z]{3})\-([0-9]{4})">
  <xsl:matching-substring>
    <xsl:value-of select="regex-group(3), f:month-num(regex-group(2)), regex-group(1)" separator="-"/>
  </xsl:matching-substring>
</xsl:analyze-string>

其次,您可以使用函数而不是模板:

<xsl:variable name="months" as="xs:string*" select="'JAN', 'FEB', 'MAR', ..."/>
<xsl:function name="f:month-num" as="xs:string">
  <xsl:param name="month-name" as="xs:string"/>
  <xsl:sequence select="format-number(index-of($months, $month-name), '00')"/>
</xsl:function>

使用函数的一个好处是上下文不会泄漏到函数中。所以编写APR而不是'APR'会给你一个错误信息,而不是试图找到一个名为APR的子元素。另一个好处是那些“as”属性 - 数据可以在进出函数的过程中进行类型检查,这样可以在你陷入困境时为你提供更早,更好的错误信息。

答案 2 :(得分:0)

使用密钥的高效XSLT 1.0解决方案

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:d="my:d">
 <xsl:output method="text"/>

 <d:Months>
   <month n="01">JAN</month>
   <month n="02">FEB</month>
   <month n="03">MAR</month>
   <month n="04">APR</month>
   <month n="05">MAY</month>
   <month n="06">JUN</month>
   <month n="07">JUL</month>
   <month n="08">AUG</month>
   <month n="09">SEP</month>
   <month n="10">OCT</month>
   <month n="11">NOV</month>
   <month n="12">DEC</month>
 </d:Months>

 <xsl:key name="kMonthByVal" match="month/@n" use=".."/>

  <xsl:template match="dt" name="funConvertDate">
    <xsl:param name="pstrDate" select="."/>

    <xsl:variable name="vMonthNum">
      <xsl:for-each select="document('')">
        <xsl:value-of select="key('kMonthByVal', substring($pstrDate, 4, 3))"/>
      </xsl:for-each>
    </xsl:variable>
    <xsl:value-of select=
        "concat('20', substring($pstrDate, 8, 2), '-', 
                $vMonthNum, '-', substring($pstrDate, 1, 2))"/>
  </xsl:template>
</xsl:stylesheet>

将此转换应用于以下XML文档

<dt>11-APR-16</dt>

产生了想要的正确结果:

2016-04-11

请注意

  1. 查找数字月表示的计算复杂度为O(1)(优秀的XSLT处理器使用哈希表实现密钥)。

  2. 根本不使用任何条件指示。

相关问题