Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用xslt字符串函数更改日期格式_String_Date_Xslt_Substring - Fatal编程技术网

String 使用xslt字符串函数更改日期格式

String 使用xslt字符串函数更改日期格式,string,date,xslt,substring,String,Date,Xslt,Substring,我必须用XSLT解决一些问题,但我不知所措。我想我需要字符串长度函数、一些选定的测试和子字符串,但我不知道。问题相对简单 我的xml看起来像下面的示例。但是,我使用YYYY、MM和DD来表示日期中的数字 <date normal=”YYYMMDD”> Month, DD, YYYY</date> <date normal=”YYYY/YYYY”> YYYY-YYYY</date> <date normal=”YYYYMM”> Month

我必须用XSLT解决一些问题,但我不知所措。我想我需要字符串长度函数、一些选定的测试和子字符串,但我不知道。问题相对简单

我的xml看起来像下面的示例。但是,我使用YYYY、MM和DD来表示日期中的数字

<date normal=”YYYMMDD”> Month, DD, YYYY</date>
<date normal=”YYYY/YYYY”> YYYY-YYYY</date>
<date normal=”YYYYMM”> Month, YYYY</date>
<date normal=”YYYYMM>MM-YYYY</date>
<name normal=”Smith, John”> John Smith </name>
月,日,年
YYYY-YYYY
年月
年月日
约翰·史密斯
我需要按原样打印所有元素,除了两个具有属性的元素
normal=“YYYYMM”
。它们需要打印,但属性格式为
normal=YYYY-MM

我不能依赖元素中的材料,因为它倾向于以各种不同的格式呈现,因为它是自由文本

我一直在尝试使用字符串长度函数来标识元素日期中包含6个字符的属性值。但是我不知道如何用连字符分割输出中的字符串。我猜它使用了一个子串函数,但是我不能让所有的东西一起工作

谢谢你的建议, 克里斯汀

像这样的事

<xsl:choose>
  <xsl:when text="string-length(@normal) = 6 and number(@normal) = number(@normal)">
    <xsl:value-of select="concat(substring(@normal, 1, 4), '-', substring(@normal, 5, 2))" />
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="@normal" />
  </xsl:otherwise>
</xsl:choose>


number(@normal)=number(@normal)
检查确保
@normal
是一个数字,因为它看起来在
normal
属性中也有一些非日期值。是否存在可能是6位非日期号码的风险?

此完整且简短的转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="@normal[string-length()=6]">
  <xsl:attribute name="normal">
   <xsl:value-of select="concat(substring(.,1,4),'-',substring(.,5))"/>
  </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>
<t>
    <date normal="YYYMMDD"> Month, DD, YYYY</date>
    <date normal="YYYY/YYYY"> YYYY-YYYY</date>
    <date normal="YYYYMM"> Month, YYYY</date>
    <date normal="YYYYMM">MM-YYYY</date>
    <name normal="Smith, John"> John Smith </name>
</t>
<t>
   <date normal="YYYMMDD"> Month, DD, YYYY</date>
   <date normal="YYYY/YYYY"> YYYY-YYYY</date>
   <date normal="YYYY-MM"> Month, YYYY</date>
   <date normal="YYYY-MM">MM-YYYY</date>
   <name normal="Smith, John"> John Smith </name>
</t>

应用于以下XML文档时(提供的片段包装到单个顶部元素中):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="@normal[string-length()=6]">
  <xsl:attribute name="normal">
   <xsl:value-of select="concat(substring(.,1,4),'-',substring(.,5))"/>
  </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>
<t>
    <date normal="YYYMMDD"> Month, DD, YYYY</date>
    <date normal="YYYY/YYYY"> YYYY-YYYY</date>
    <date normal="YYYYMM"> Month, YYYY</date>
    <date normal="YYYYMM">MM-YYYY</date>
    <name normal="Smith, John"> John Smith </name>
</t>
<t>
   <date normal="YYYMMDD"> Month, DD, YYYY</date>
   <date normal="YYYY/YYYY"> YYYY-YYYY</date>
   <date normal="YYYY-MM"> Month, YYYY</date>
   <date normal="YYYY-MM">MM-YYYY</date>
   <name normal="Smith, John"> John Smith </name>
</t>

年月日
YYYY-YYYY
年月
年月日
约翰·史密斯
生成所需的正确结果:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="@normal[string-length()=6]">
  <xsl:attribute name="normal">
   <xsl:value-of select="concat(substring(.,1,4),'-',substring(.,5))"/>
  </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>
<t>
    <date normal="YYYMMDD"> Month, DD, YYYY</date>
    <date normal="YYYY/YYYY"> YYYY-YYYY</date>
    <date normal="YYYYMM"> Month, YYYY</date>
    <date normal="YYYYMM">MM-YYYY</date>
    <name normal="Smith, John"> John Smith </name>
</t>
<t>
   <date normal="YYYMMDD"> Month, DD, YYYY</date>
   <date normal="YYYY/YYYY"> YYYY-YYYY</date>
   <date normal="YYYY-MM"> Month, YYYY</date>
   <date normal="YYYY-MM">MM-YYYY</date>
   <name normal="Smith, John"> John Smith </name>
</t>

年月日
YYYY-YYYY
年月
年月日
约翰·史密斯

您好,是的,我不应该有一个六位数的非日期号码,而且永远不会在日期元素中有它。我可以将选择的属性限制为仅日期元素的普通属性。或者,您可以向我们展示一些XSL和更完整的XML视图,这样我就可以告诉您上述内容在全局中的位置。让我试试您给我的代码,如果我仍然有困难,我会回来的。我认为测试和concat函数应该可以做到这一点。我没有想过使用数字测试,但这应该可以解决问题。我也可以尝试加入测试
,这样做对吗?为测试添加xpath?非常感谢您的帮助!答案在某种程度上取决于XSL执行的上下文,但假设您处于
日期
名称
等上下文中(即
@normal
属性的直接父级),这个测试应该只适用于修改父项为
日期的
普通的
s:
我假设您使用的是XSLT1.0,因为在2.0中,任务将很简单。但是,如果您受限于旧版本的XSLT,那么您应该在问题中这样说,以避免误解。