gpt4 book ai didi

java exslt date-time() 产生意外输出

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:44:15 25 4
gpt4 key购买 nike

在进行 xslt 转换时,时区以一种奇怪的方式序列化

我尝试在 xalan 和 jaxen 之间切换实现,但没有观察到任何变化。

试图查看手动格式化日期,但似乎无法找到使用它添加时区(例如,+05:30 或 -08:00)的方法。

有人遇到过类似的问题吗?

产生的结果

<?xml version="1.0" encoding="UTF-8"?>
<Test xmlns:date="http://exslt.org/dates-and-times" version="1.0">
2019-06-20T10:23:31+05:1800000
</Test>

预期结果

<?xml version="1.0" encoding="UTF-8"?>
<Test xmlns:date="http://exslt.org/dates-and-times" version="1.0">
2019-06-20T10:23:31+05:30
</Test>

我的示例代码如下

主.java

package test;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.TimeZone;

import static java.nio.charset.StandardCharsets.UTF_8;

public class Main {
public static void main(String[] args) {
printTransformed(TimeZone.getTimeZone("Asia/Kolkata"));
}

private static void printTransformed(TimeZone timeZone) {
TimeZone.setDefault(timeZone);
try {
final StreamSource source = new StreamSource(
Files.newInputStream(
Paths.get(PrintAllTimeZones1.class.getResource("/input.xml").toURI())));

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Templates xsltTemplate = transformerFactory.newTemplates(
new StreamSource(Files.newInputStream(
Paths.get(Application.class.getResource("/test.xslt").toURI()))));

final Transformer transformer = xsltTemplate.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8.name());
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final StreamResult result = new StreamResult(out);
transformer.transform(source, result);
System.out.println(result.getOutputStream().toString());
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}

输入.xml

<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

测试.xslt

<Test version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times">
<xsl:value-of select="date:date-time()"/>
</Test>

build.gradle

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.4.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
baseName = 'gs-spring-boot'
version = '0.1.0'
}

repositories {
mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile 'jaxen:jaxen'
testCompile("junit:junit")
compile group: 'xalan', name: 'xalan', version: '2.7.2'


compile group: 'xmlunit', name: 'xmlunit', version: '1.6'

compile group: 'org.apache.avro', name: 'avro', version: '1.9.0'

}

最佳答案

获得 2019-06-20T10:23:31+05:1800000 的原因是因为 xalan 的 ExsltDatetime 实现会添加 GMT 偏移量和 DST 偏移量(5.5 小时)。但是在计算小时和分钟的偏移量时,xalan 实现中似乎存在错误。

xalan 的 ExsltDatetime 类中的偏移代码:

int offset = cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET);
// If there is no offset, we have "Coordinated
// Universal Time."
if (offset == 0)
buff.append('Z');
else {
// Convert milliseconds to hours and minutes
int hrs = offset/(60*60*1000);
// In a few cases, the time zone may be +/-hh:30.
int min = offset%(60*60*1000);
char posneg = hrs < 0? '-': '+';
buff.append(posneg + formatDigits(hrs) + ':' + formatDigits(min));
}

上面的代码以小时为单位的偏移量为 5,这很好,但是分钟偏移量计算为 1800000,这是不正确的。

else 部分应该根据剩余偏移量正确计算分钟数,如下所示:

  // Convert milliseconds to hours and minutes
int hrs = offset/(60*60*1000);
// In a few cases, the time zone may be +/-hh:30.
//get the remaining offset in ms
offset -= (hrs*60*60*1000);
//convert remaining offset into minutes
int min = offset/(60*1000);
char posneg = hrs < 0? '-': '+';
buff.append(posneg + formatDigits(hrs) + ':' + formatDigits(min));

以上是更正后的代码,它正确地将分钟返回为 30。

要在 XSLT 中为当前日期使用不同/简单的日期格式化程序,您可以使用:

<Test version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="xalan://java.text.SimpleDateFormat"
xmlns:java="http://xml.apache.org/xslt/java">
<xsl:variable name="iso-date"
select='date:new("yyyy-MM-dd&apos;T&apos;hh:mm:ssXXX")' />
<xsl:value-of select="java:format($iso-date, java:java.util.Date.new())"/>
</Test>

你也可以试试XSLT 2.0 formatting functions

关于java exslt date-time() 产生意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56679556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com