2017-04-25 50 views
0

我需要根据使用xslt v2的输入文件获取每个月的日期。这里是我的样本数据:使用xslt获取每个月的日期

<Data> 
    <Field>March/02/2017/February/16/1989/December/19/2015</Field> 
</Data> 

我的XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0"> 
<xsl:output method="xml" indent="yes"/> 
<xsl:template match="Data"> 
    <xsl:copy> 
     <xsl:apply-templates/> 
    </xsl:copy> 
</xsl:template> 
<xsl:variable name="months" select="'January','February','March','April','May','June','July','August','September','October','November','December'"/> 
<xsl:template match="Field"> 
    <xsl:variable name="Output"> 
     <xsl:analyze-string select="normalize-space(.)" regex="([A-Z][a-z]+)"> 
      <xsl:matching-substring> 
       <xsl:number value="index-of($months, regex-group(1))" format="01"/> 
      </xsl:matching-substring> 
     </xsl:analyze-string> 
    </xsl:variable> 
    <Result> 
     <xsl:value-of select="$Output"/> 
    </Result> 
</xsl:template> 
</xsl:stylesheet> 

生成的输出

<Data> 
    <Result>030212</Result> 
</Data> 

产生的输出是每个月的位置,但我想填充每月之后的日期。像这样的:

<Data> 
    <March>02/2017</March> 
    <February>16/1989</February> 
    <December>19/2015</December> 
</Data> 

另外,我有一个问题,如果在测试文件中的月是大写或小写的,没有填充的输出。我

希望你能帮助我。谢谢。

+0

要生成的输出将是可怕的处理。你确定你想制作这种格式吗? –

+0

是的,这是我需要生成的。但是输出元素名称只是一个例子。 – Charlotte

回答

1

我不确定我是否理解$monthsindex-of()的原因。它看起来像一切都已在Field

例...

XSLT 2.0

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

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

    <xsl:template match="Field"> 
    <xsl:analyze-string select="normalize-space()" regex="(\w+)/(\d{{2}}/\d{{4}})"> 
     <xsl:matching-substring> 
     <xsl:element name="{regex-group(1)}"> 
      <xsl:value-of select="regex-group(2)"/> 
     </xsl:element> 
     </xsl:matching-substring> 
    </xsl:analyze-string> 
    </xsl:template> 

</xsl:stylesheet> 

输出

<Data> 
    <March>02/2017</March> 
    <February>16/1989</February> 
    <December>19/2015</December> 
</Data>