我如何让 XSLT 向用户显示本地化日期(在用户代理的区域设置中)。
例如,给定一个date
ISO 格式的 xml ( 2013-09-04T10:46:19.658
):
<?xml version="1.0" encoding="utf-8"?>
<Stuff>
<Created>2013-09-04T10:46:19.658</Created>
</Stuff>
以及样式表的开头:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" version="5.0" doctype-system="about:legacy-compat"/>
<xsl:template match="/Stuff">
<html>
<body>
Created: <xsl:value-of select="format-date('Created')"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
当然,所需的输出取决于用户代理的语言环境(例如客户端的浏览器):
Created: 01/11/34
Created: 05.09.13
Created: 05.09.2013
Created: 05/09 2013
Created: 05/09/2013
Created: 05-09-13
Created: 05-09-2013
Created: 09.05.2013
Created: 09/05/2013
Created: 13.09.05
Created: 2013.09.05
Created: 2013.09.05.
Created: 2013/09/05
Created: 2013/9/5
Created: 2013-09-05
Created: 2013-9-5
Created: 29/10/34
Created: 5. 9. 2013
Created: 5.09.2013
Created: 5.9.2013
Created: 5.9.2013 г.
Created: 5.9.2013.
Created: 5//09//2013
Created: 5/09/2013
Created: 5/9/2013
Created: 5/9/2556
Created: 5-9-2013
Created: 9/5/2013
XSLT 是否支持本地化?
它在客户端上运行
XML 到 HTML 的转换发生在客户端。由于转换发生在客户端上,客户端(显然)知道自己的语言环境。
例如,给客户一些xml
:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso_Handheld.xslt' media='handheld'?>
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso_iPhone.xslt' media='only screen and (max-device-width: 480px)'?>
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso.xslt' media='all'?>
<Stuff>
<Created>2013-09-04T10:46:19.658</Created>
</Stuff>
注意客户端指示使用哪个XSLT
行。
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso_Handheld.xslt' media='handheld'?>
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso_iPhone.xslt' media='only screen and (max-device-width: 480px)'?>
<?xml-stylesheet type='text/xsl' href='/Styles/Contoso.xslt' media='all'?>
User-Agent获取 XSLT、转换 XML 并将其显示给用户。所有这些处理都发生在客户端的语言环境中。