EXSLT 日期:不带 document() 的格式日期模板 XSLT 1.0

发布于 2024-08-30 18:35:19 字数 3987 浏览 1 评论 0原文

我正在使用 date:format-date 模板 EXSLT 文件我使用 XSLT 1.0 和 MSXML3.0 作为处理器。

我的 date:format-date 模板 EXSLT 文件的声明是:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:date="http://exslt.org/dates-and-times"
  xmlns:str="http://exslt.org/strings"
  extension-element-prefixes="msxsl date str">
   ...
</xsl:stylesheet>

由于第 3 方限制,我无法使用 document() 函数。因此,我将月份和日期(类似地)从 XML 片段:

<date:months>
  <date:month length="31" abbr="Jan">January</date:month>
  <date:month length="28" abbr="Feb">February</date:month>
  <date:month length="31" abbr="Mar">March</date:month>
  <date:month length="30" abbr="Apr">April</date:month>
  <date:month length="31" abbr="May">May</date:month>
  <date:month length="30" abbr="Jun">June</date:month>
  <date:month length="31" abbr="Jul">July</date:month>
  <date:month length="31" abbr="Aug">August</date:month>
  <date:month length="30" abbr="Sep">September</date:month>
  <date:month length="31" abbr="Oct">October</date:month>
  <date:month length="30" abbr="Nov">November</date:month>
  <date:month length="31" abbr="Dec">December</date:month>
</date:months>

更改为变量:

 <xsl:variable name="months">
   <month length="31" abbr="Jan">January</month>
   <month length="28" abbr="Feb">February</month>
   <month length="31" abbr="Mar">March</month>
   <month length="30" abbr="Apr">April</month>
   <month length="31" abbr="May">May</month>
   <month length="30" abbr="Jun">June</month>
   <month length="31" abbr="Jul">July</month>
   <month length="31" abbr="Aug">August</month>
   <month length="30" abbr="Sep">September</month>
   <month length="31" abbr="Oct">October</month>
   <month length="30" abbr="Nov">November</month>
   <month length="31" abbr="Dec">December</month>
</xsl:variable>

相应地,我更改了最初使用 document() 函数的代码 <强>来自:
[来自 EXSLT 样式表的月份处理位]

<xsl:variable name="month-node" select="document('')/*/date:months/date:month[number($month)]" />

使用 MSXML3.0 节点集函数

<xsl:variable name="month-node" select="msxsl:node-set($months)/month[number($month)]" />

所以我认为这可行。

根据 EXLT 说明“格式模式字符串按照 JDK 1.1 的描述进行解释SimpleDateFormat 类。”[我使用当前版本]。

我根据指定月份SimpleDateFormat 类 为“dd MMMMM yyyy”,以便月份将是完整的月份名称,例如一月。但它不起作用:(我查看了 EXSLT 样式表,它有执行此操作的逻辑。还有使用“E”模式显示某一天的星期名称的逻辑,但这不起作用 从使用 document() 更改为变量会破坏它

我来说

<xsl:call-template name="date:format-date">
    <xsl:with-param name="date-time" select="'2010-07-01'"/>
    <xsl:with-param name="pattern" select="'dd MMMMM yyyy'" />
</xsl:call-template>

,也许 /j2se/1.3/docs/api/java/text/SimpleDateFormat.html" rel="nofollow noreferrer">SimpleDateFormat class 文档这应该导致 01 July 2010 ,我得到的是01 07 2010



<xsl:call-template name="date:format-date">
    <xsl:with-param name="date-time" select="'2010-07-01'"/>
    <xsl:with-param name="pattern" select="'EEE, dd MMMMM yyyy'" />
</xsl:call-template>

这应该导致 Mon, 01 July 2010 而我得到的是 , 01 07 2010

I'm using date:format-date template EXSLT file I'm using XSLT 1.0 and MSXML3.0 as the processor.

My date:format-date template EXSLT file's declaration is:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:date="http://exslt.org/dates-and-times"
  xmlns:str="http://exslt.org/strings"
  extension-element-prefixes="msxsl date str">
   ...
</xsl:stylesheet>

I cannot use document() function due to the 3rd party restrictions. So I have changed the months and days (similarly) from XML snippet:

<date:months>
  <date:month length="31" abbr="Jan">January</date:month>
  <date:month length="28" abbr="Feb">February</date:month>
  <date:month length="31" abbr="Mar">March</date:month>
  <date:month length="30" abbr="Apr">April</date:month>
  <date:month length="31" abbr="May">May</date:month>
  <date:month length="30" abbr="Jun">June</date:month>
  <date:month length="31" abbr="Jul">July</date:month>
  <date:month length="31" abbr="Aug">August</date:month>
  <date:month length="30" abbr="Sep">September</date:month>
  <date:month length="31" abbr="Oct">October</date:month>
  <date:month length="30" abbr="Nov">November</date:month>
  <date:month length="31" abbr="Dec">December</date:month>
</date:months>

to the variable:

 <xsl:variable name="months">
   <month length="31" abbr="Jan">January</month>
   <month length="28" abbr="Feb">February</month>
   <month length="31" abbr="Mar">March</month>
   <month length="30" abbr="Apr">April</month>
   <month length="31" abbr="May">May</month>
   <month length="30" abbr="Jun">June</month>
   <month length="31" abbr="Jul">July</month>
   <month length="31" abbr="Aug">August</month>
   <month length="30" abbr="Sep">September</month>
   <month length="31" abbr="Oct">October</month>
   <month length="30" abbr="Nov">November</month>
   <month length="31" abbr="Dec">December</month>
</xsl:variable>

And correspondingly, I've changed the code that originally uses document() function from:
[from month processing bit of EXSLT stylesheet]

<xsl:variable name="month-node" select="document('')/*/date:months/date:month[number($month)]" />

to use MSXML3.0 node-set function:

<xsl:variable name="month-node" select="msxsl:node-set($months)/month[number($month)]" />

So I assumed that this would work.

According to the EXLT instructions "The format pattern string is interpreted as described for the JDK 1.1 SimpleDateFormat class." [I used current version].

I'm specifing the month in accordance to SimpleDateFormat class as 'dd MMMMM yyyy' so that the month will be the full month's name, for example January. But it doesn't work :( I've looked in EXSLT stylesheet and it has got the logic to do that. Also there is logic to display the name of the week for a day using 'E' pattern, which doesn't work for me. Maybe changing from using document() to variables broke it.

Would really appreciate any help.

Many thanks!

EXAMPLES

<xsl:call-template name="date:format-date">
    <xsl:with-param name="date-time" select="'2010-07-01'"/>
    <xsl:with-param name="pattern" select="'dd MMMMM yyyy'" />
</xsl:call-template>

As I understand from SimpleDateFormat class docs this should result in 01 July 2010 and what I get is 01 07 2010

<xsl:call-template name="date:format-date">
    <xsl:with-param name="date-time" select="'2010-07-01'"/>
    <xsl:with-param name="pattern" select="'EEE, dd MMMMM yyyy'" />
</xsl:call-template>

This should result in Mon, 01 July 2010 and what I get is , 01 07 2010

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

属性 2024-09-06 18:35:19

当您在 XML 的无根片段(如月份列表)上使用 msxml:node-set 时,该函数会自动创建一个根来包含该片段。

因此,在您的情况下,您从节点集的路径是错误的:

msxsl:node-set($months)/month[number($month)]

这是错误的,因为 /month 不是 $months 的根。您可以通过跳过人工根并直接进入月份来轻松修复它:

msxsl:node-set($months)//month[number($month)]

请注意 // 运算符而不是 / 运算符。

编辑

经过测试,我发现我错了。事实上,您使用的原始片段对我来说效果很好。 // 运算符是不必要的。您确定您的 $month 变量设置正确吗?

When you use msxml:node-set on a rootless fragment of XML (like your list of months), the function automatically creates a root to contain the fragment.

So in your case your path from the node set is wrong:

msxsl:node-set($months)/month[number($month)]

This is wrong because /month is not the root of $months. You can fix it easily by skipping the artificial root and going to month directly:

msxsl:node-set($months)//month[number($month)]

Note the // operator instead of the / operator.

EDIT

Upon testing I see that I'm wrong. In fact, the original snippet that you are using works fine for me. The // operator is unnecessary. Are you sure your $month variable is being set correctly?

陌若浮生 2024-09-06 18:35:19

根据您引用的 EXSLT 页面(date:format-date 模板 EXSLT 文件):

当前没有 XSLT 处理器
我们知道这种支持
日期:本机格式化日期。

您使用什么实现? MSXML 似乎本身不支持它。

According to the EXSLT page you reference (date:format-date template EXSLT file):

There are currently no XSLT processors
that we know of that support
date:format-date natively.

What implementation are you using? It doesn't seem to be supported natively in MSXML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文