XSLT - 提取字符串值和格式日期

时间:2013-11-10 19:08:17

标签: xml xslt xslt-1.0

我有以下 XML

 <?xml version="1.0" encoding="utf-8"?>
<FilterPanel>
<FilterCategory DisplayName="Publication Date" ShowMoreLink="True">
<Filters>
  <Filter>
    <Value>Any Publication Date</Value>
    <Tooltip>Any Publication Date</Tooltip>
    <Selection>Implied</Selection>
    <Count></Count>
    <Percentage></Percentage>
  </Filter>
  <Filter>
    <Value>Before 2013-11-08T03:49:32.4999680Z</Value>
    <Tooltip>Before 2013-11-08T03:49:32.4999680Z</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>From 2013-11-08T03:49:32.4999680Z to 2013-11-08T09:46:47.4999808Z</Value>
    <Tooltip>From 2013-11-08T03:49:32.4999680Z to 2013-11- 
08T09:46:47.4999808Z</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>From 2013-11-08T09:46:47.4999808Z to 2013-11-08T16:23:55.9998464Z</Value>
    <Tooltip>From 2013-11-08T09:46:47.4999808Z to 2013-11-   
        08T16:23:55.9998464Z</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>2013-11-08T16:23:55.9998464Z or later</Value>
    <Tooltip>2013-11-08T16:23:55.9998464Z or later</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
</Filters>
</FilterCategory>
 <FilterCategory DisplayName="Review Date" ShowMoreLink="True">
<Filter>SameasPublciationDateOutput</Filter>
</FilterCategory>
<FilterCategory DisplayName="TEST" ShowMoreLink="True">
<Filter></Filter>
</FilterCategory>
</FilterPanel>

对于DisplayName为发布日期和审核日期的FilterCatergories,我希望在

中更改Value节点的日期格式
  

DDMMYYYY HH:MM

格式在给定值中。

输出应如下所示:

<FilterCategory DisplayName="Publication Date" ShowMoreLink="True">
<Filters>
  <Filter>
    <Value>Any Publication Date</Value>
    <Tooltip>Any Publication Date</Tooltip>
    <Selection>Implied</Selection>
    <Count></Count>
    <Percentage></Percentage>
  </Filter>
  <Filter>
    <Value>Before 08-11-2013 03:49</Value>
    <Tooltip>Before 08-11-2013 03:49</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>From 08-11-2013 03:49 to 08-11-2013 09:46</Value>
    <Tooltip>From 08-11-2013 03:49 to 08-11-2013 09:46</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>08-11-2013 09:46 to 08-11-2013 16:23</Value>
    <Tooltip>08-11-2013 09:46 to 08-11-2013 16:23</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>08-11-2013 16:23 or later</Value>
    <Tooltip>08-11-2013 16:23 or later</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
</Filters>

我是XSLT的新手,并不太确定如何实现这一目标。任何帮助都将受到高度赞赏。

谢谢。

2 个答案:

答案 0 :(得分:0)

XSLT 1.0不是我的首选语言,但是

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="text()[contains(.,'T') and contains(.,'Z')]" name="T">
      <xsl:param name="t" select="." />
      <xsl:variable name="a" select="substring-before($t,'T')" />
      <xsl:variable name="l" select="string-length($a)" />
      <xsl:variable name="d" select="substring($a,$l - 1,2)" />
      <xsl:variable name="m" select="substring($a,$l -4,2)" />
      <xsl:variable name="y" select="substring($a,$l -9,4)" />
      <xsl:variable name="b" select="substring-after($t,'T')" />
      <xsl:variable name="c" select="substring-before($b,'Z')" />
      <xsl:variable name="z" select="substring-after($b,'Z')" />
      <xsl:value-of select="concat(substring($a,1,$l -10),$d,'-',$m,'-',$y,' ', substring($c,1,5),' ',$z)" />
   </xsl:template>
   <xsl:template match="text()[contains(.,'T') and contains(.,'Z') and contains(.,' to ')]" priority="2">
      <xsl:call-template name="T">
         <xsl:with-param name="t" select="substring-before(.,' to ')" />
      </xsl:call-template>
      <xsl:text>to</xsl:text>
      <xsl:call-template name="T">
         <xsl:with-param name="t" select="substring-after(.,' to ')" />
      </xsl:call-template>
   </xsl:template>
</xsl:stylesheet>

输入:

<FilterPanel>
<FilterCategory DisplayName="Publication Date" ShowMoreLink="True">
<Filters>
  <Filter>
    <Value>Any Publication Date</Value>
    <Tooltip>Any Publication Date</Tooltip>
    <Selection>Implied</Selection>
    <Count></Count>
    <Percentage></Percentage>
  </Filter>
  <Filter>
    <Value>Before 2013-11-08T03:49:32.4999680Z</Value>
    <Tooltip>Before 2013-11-08T03:49:32.4999680Z</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>From 2013-11-08T03:49:32.4999680Z to 2013-11-08T09:46:47.4999808Z</Value>
    <Tooltip>From 2013-11-08T03:49:32.4999680Z to 2013-11-08T09:46:47.4999808Z</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>From 2013-11-08T09:46:47.4999808Z to 2013-11-08T16:23:55.9998464Z</Value>
    <Tooltip>From 2013-11-08T09:46:47.4999808Z to 2013-11-08T16:23:55.9998464Z</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>2013-11-08T16:23:55.9998464Z or later</Value>
    <Tooltip>2013-11-08T16:23:55.9998464Z or later</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
</Filters>
</FilterCategory>
 <FilterCategory DisplayName="Review Date" ShowMoreLink="True">
<Filter>SameasPublciationDateOutput</Filter>
</FilterCategory>
<FilterCategory DisplayName="TEST" ShowMoreLink="True">
<Filter></Filter>
</FilterCategory>
</FilterPanel>

输出

<?xml version="1.0" encoding="utf-8"?><FilterPanel>
<FilterCategory DisplayName="Publication Date" ShowMoreLink="True">
<Filters>
  <Filter>
    <Value>Any Publication Date</Value>
    <Tooltip>Any Publication Date</Tooltip>
    <Selection>Implied</Selection>
    <Count/>
    <Percentage/>
  </Filter>
  <Filter>
    <Value>Before 08-11-2013 03:49 </Value>
    <Tooltip>Before 08-11-2013 03:49 </Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>From 08-11-2013 03:49  to 08-11-2013 09:46 </Value>
    <Tooltip>From 08-11-2013 03:49  to 08-11-2013 09:46 </Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>From 08-11-2013 09:46  to 08-11-2013 16:23 </Value>
    <Tooltip>From 08-11-2013 09:46  to 08-11-2013 16:23 </Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
  <Filter>
    <Value>08-11-2013 16:23  or later</Value>
    <Tooltip>08-11-2013 16:23  or later</Tooltip>
    <Selection>Deselected</Selection>
    <Count>1568</Count>
    <Percentage>0.25</Percentage>
  </Filter>
</Filters>
</FilterCategory>
 <FilterCategory DisplayName="Review Date" ShowMoreLink="True">
<Filter>SameasPublciationDateOutput</Filter>
</FilterCategory>
<FilterCategory DisplayName="TEST" ShowMoreLink="True">
<Filter/>
</FilterCategory>
</FilterPanel>

答案 1 :(得分:-1)

([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2}).([0-9]*)(Z|[+-]?[0-9]{2}:[0-9]{2})

替换为

\3\2\1 \4:\5

传入格式是否已修复? XSD日期时间?或者可能会有所不同? dateTime可以有+/- 02:30之类的时区,而不是Z.你应该考虑它们。