【问题标题】:xsl substring in xpathxpath 中的 xsl 子字符串
【发布时间】:2013-02-20 10:23:18
【问题描述】:

所以我要做的是从 xml 中获取日期中所有唯一的年份,然后遍历年份以获取该年份的所有结果。下面的代码可以工作到一定程度,但目前它只返回唯一的完整日期,因为我无法弄清楚如何通过日期字符串的一部分(前四个字符,例如 2012)获取唯一值。我想在 xpath 中使用子字符串,但我尝试过的一切似乎都失败了。有谁知道我应该在这里做什么?

所以我希望第一个结果返回的是这些值 2012,2011,2010。

这是我在下面使用的代码 - 使用 xslt 1.0。我对使用 xslt 比较陌生,所以我可能会遗漏一些简单的东西......

<!-- key at top of document -->
<xsl:key name="product" match="gdr:CompanyHistory/gdr:Event/gdr:Date" use="." />

<xsl:for-each select="gdr:CompanyHistory/gdr:Event/gdr:Date[generate-id()=generate-id(key('product',.)[1])]">
        <strong>Year = <xsl:value-of select="substring(., 1, 4)"/></strong>
        <ul>
          <xsl:variable name="Date" select="."/>
          <!-- foreach year get all results that have dates in that year.   -->
          <xsl:for-each select="gdr:CompanyHistory/gdr:Event[substring(gdr:Date, 1, 4) = $Date]">
              <li><xsl:value-of select="gdr:Description" /> - <xsl:value-of select="gdr:Date" /></li>
          </xsl:for-each>
        </ul>
      </xsl:for-each>

xml

<CompanyHistory>
<Event>
    <Date>2012-02-16T00:00:00</Date>
    <Description>description</Description>
</Event>
<Event>
    <Date>2012-02-01T00:00:00</Date>
    <Description>description</Description>
</Event>
<Event>
    <Date>2011-03-01T00:00:00</Date>
    <Description>description</Description>
</Event>
<Event>
    <Date>2011-04-01T00:00:00</Date>
    <Description>description</Description>
</Event>
<Event>
    <Date>2010-01-26T00:00:00</Date>
    <Description>description</Description>
</Event>
<Event>
    <Date>2010-01-16T00:00:00</Date>
    <Description>description</Description>
</Event>
</CompanyHistory>

我希望这有点道理。注意:我缩短了完整的 xpath 以使其更具可读性。

【问题讨论】:

  • 你的代码的问题是你有 &lt;xsl:variable name="Date" select="."/&gt; 然后在内部 for-each 你比较 substring(gdr:Date, 1, 4) = $Date (即比较一个日期的前四个字符与 整个另一个)。但是马丁的回答绝对是走的路——直接用key按年份分组。

标签: xslt xslt-1.0


【解决方案1】:

我将只为年份定义键,然后使用 Muenchian 分组。所以样式表是

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

<xsl:output method="html" indent="yes"/>

<xsl:key name="year" match="CompanyHistory/Event" use="substring(Date, 1, 4)"/>

<xsl:template match="CompanyHistory">
   <xsl:apply-templates select="Event[generate-id() = generate-id(key('year', substring(Date, 1, 4))[1])]" mode="group"/>
</xsl:template>

<xsl:template match="Event" mode="group">
  <h2>Year = <xsl:value-of select="substring(Date, 1, 4)"/></h2>
  <ul>
    <xsl:apply-templates select="key('year', substring(Date, 1, 4))"/>
  </ul>
</xsl:template>

<xsl:template match="Event">
  <li>
    <xsl:value-of select="Description"/>
    <xsl:text> - </xsl:text>
    <xsl:value-of select="Date"/>
  </li>
</xsl:template>

</xsl:stylesheet>

输入

<CompanyHistory>
<Event>
    <Date>2012-02-16T00:00:00</Date>
    <Description>description</Description>
</Event>
<Event>
    <Date>2012-02-01T00:00:00</Date>
    <Description>description</Description>
</Event>
<Event>
    <Date>2011-03-01T00:00:00</Date>
    <Description>description</Description>
</Event>
<Event>
    <Date>2011-04-01T00:00:00</Date>
    <Description>description</Description>
</Event>
<Event>
    <Date>2010-01-26T00:00:00</Date>
    <Description>description</Description>
</Event>
<Event>
    <Date>2010-01-16T00:00:00</Date>
    <Description>description</Description>
</Event>
</CompanyHistory>

变成了

<h2>Year = 2012</h2>
<ul>
   <li>description - 2012-02-16T00:00:00</li>
   <li>description - 2012-02-01T00:00:00</li>
</ul>
<h2>Year = 2011</h2>
<ul>
   <li>description - 2011-03-01T00:00:00</li>
   <li>description - 2011-04-01T00:00:00</li>
</ul>
<h2>Year = 2010</h2>
<ul>
   <li>description - 2010-01-26T00:00:00</li>
   <li>description - 2010-01-16T00:00:00</li>
</ul>

【讨论】:

  • 非常感谢,这非常有效:)。我不知道这种方法。
  • @user2087471,如果它“完美运行”,那么,请接受 Martin 的回答。只需点击答案旁边的复选标记即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多