使用XSL样式表分析Google XML提要中的日期/时间信息



我需要帮助使用样式表解析GoogleXML提要中的一些日期/时间字段。到目前为止,我在这里得到了极好的帮助:谷歌卡尔XSLT

第二部分是获取离散字段中的日期和时间。这是原始的XML输出,我了解到它实际上是带有XML包装器的HTML。

    <?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:gCal="http://schemas.google.com/gCal/2005">
  <id>http://www.google.com/calendar/feeds/cqdcu7b550jienqk97ddrdfhpc%40group.calendar.google.com/public/basic</id>
  <updated>2015-02-24T23:02:37.000Z</updated>
  <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#event"/>
  <title type="text">SB Meetings</title>
  <subtitle type="text">SB Meetings</subtitle>
  <link rel="alternate" type="text/html" href="https://www.google.com/calendar/embed?src=cqdcu7b550jienqk97ddrdfhpc%40group.calendar.google.com"/>
  <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="https://www.google.com/calendar/feeds/cqdcu7b550jienqk97ddrdfhpc%40group.calendar.google.com/public/basic"/>
  <link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="https://www.google.com/calendar/feeds/cqdcu7b550jienqk97ddrdfhpc%40group.calendar.google.com/public/basic/batch"/>
  <link rel="self" type="application/atom+xml" href="https://www.google.com/calendar/feeds/cqdcu7b550jienqk97ddrdfhpc%40group.calendar.google.com/public/basic?max-results=25"/>
  <link rel="next" type="application/atom+xml" href="https://www.google.com/calendar/feeds/cqdcu7b550jienqk97ddrdfhpc%40group.calendar.google.com/public/basic?start-index=26&amp;max-results=25"/>
  <author>
    <name>xx.xx@gmail.com</name>
    <email>xx.xx@gmail.com</email>
  </author>
  <generator version="1.0" uri="http://www.google.com/calendar">Google Calendar</generator>
  <openSearch:totalResults>848</openSearch:totalResults>
  <openSearch:startIndex>1</openSearch:startIndex>
  <openSearch:itemsPerPage>25</openSearch:itemsPerPage>
  <gCal:timezone value="America/Los_Angeles"/>
  <gCal:timesCleaned value="0"/>
  <entry>
    <id>http://www.google.com/calendar/feeds/cqdcu7b550jienqk97ddrdfhpc%40group.calendar.google.com/public/basic/_6srj8chl6hhm4oppc9ij8oppcks3gp9n6kom6pb2cdij8c9gcpj3kc9d74ok6dpl8p246gpi6gqkae9j88s3gchl6t24cdhg60rk8di588s2qc9k68qjadpm6oo30c1g60mj2d1i6gs32e1i60pj0c1g</id>
    <published>2015-02-24T23:02:37.000Z</published>
    <updated>2015-02-24T23:02:37.000Z</updated>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#event"/>
    <title type="html">Project Manager Meeting</title>
    <summary type="html">When: Thu Mar 5, 2015 9:30am to 10:30am&amp;nbsp;
PST&lt;br&gt;
&lt;br&gt;Where: Conference Room NH2/CA Motor City S Vermont 12345@KAIPERM
&lt;br&gt;Event Status: confirmed
&lt;br&gt;Event Description: Chairperson: Resource-Scheduler
Required: Test User 2</summary>
    <content type="html">When: Thu Mar 5, 2015 9:30am to 10:30am 
PST&lt;br /&gt;
&lt;br /&gt;Where: Conference Room NH2/CA Motor City S Vermont 12345@KAIPERM
&lt;br /&gt;Event Status: confirmed
&lt;br /&gt;Event Description: Chairperson: Resource-Scheduler
Required: Test User 2</content>
    <link rel="alternate" type="text/html" href="https://www.google.com/calendar/event?eid=XzZzcmo4Y2hsNmhobTRvcHBjOWlqOG9wcGNrczNncDluNmtvbTZwYjJjZGlqOGM5Z2NwajNrYzlkNzRvazZkcGw4cDI0NmdwaTZncWthZTlqODhzM2djaGw2dDI0Y2RoZzYwcms4ZGk1ODhzMnFjOWs2OHFqYWRwbTZvbzMwYzFnNjBtajJkMWk2Z3MzMmUxaTYwcGowYzFnIGNxZGN1N2I1NTBqaWVucWs5N2RkcmRmaHBjQGc" title="alternate"/>
    <link rel="self" type="application/atom+xml" href="https://www.google.com/calendar/feeds/cqdcu7b550jienqk97ddrdfhpc%40group.calendar.google.com/public/basic/_6srj8chl6hhm4oppc9ij8oppcks3gp9n6kom6pb2cdij8c9gcpj3kc9d74ok6dpl8p246gpi6gqkae9j88s3gchl6t24cdhg60rk8di588s2qc9k68qjadpm6oo30c1g60mj2d1i6gs32e1i60pj0c1g"/>
    <author>
      <name>SB Meetings</name>
      <email>yy.yy@gmail.com</email>
    </author>
  </entry>
  <entry>
    <id>http://www.google.com/calendar/feeds/cqdcu7b550jienqk97ddrdfhpc%40group.calendar.google.com/public/basic/_c9ijacj668sj0or16dh38chmcos3eohk64smcdpm64qjce1k6kq3kc9d64qj0g9h65330dq16kojedq170s3gchl6t24cdhg60rk8di588oiqc9k68q36dhn60o30c1g60mj2d1i6gs32e1i60pj0c1g</id>
    <published>2015-02-24T23:02:37.000Z</published>
    <updated>2015-02-24T23:02:37.000Z</updated>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#event"/>
    <title type="html">Analyst Meeting</title>
    <summary type="html">When: Thu Feb 19, 2015 9:30am to 10:30am&amp;nbsp;
PST&lt;br&gt;
&lt;br&gt;Where: Conference Room NH4/CA Motor City S Vermont 12345
&lt;br&gt;Event Status: confirmed
&lt;br&gt;Event Description: Chairperson: Resource-Scheduler
Required: Test User 1</summary>
    <content type="html">When: Thu Feb 19, 2015 9:30am to 10:30am 
PST&lt;br /&gt;
&lt;br /&gt;Where: Conference Room NH4/CA Motor City S Vermont 12345
&lt;br /&gt;Event Status: confirmed
&lt;br /&gt;Event Description: Chairperson: Resource-Scheduler
Required: Test User 1</content>
    <link rel="alternate" type="text/html" href="https://www.google.com/calendar/event?eid=X2M5aWphY2o2NjhzajBvcjE2ZGgzOGNobWNvczNlb2hrNjRzbWNkcG02NHFqY2UxazZrcTNrYzlkNjRxajBnOWg2NTMzMGRxMTZrb2plZHExNzBzM2djaGw2dDI0Y2RoZzYwcms4ZGk1ODhvaXFjOWs2OHEzNmRobjYwbzMwYzFnNjBtajJkMWk2Z3MzMmUxaTYwcGowYzFnIGNxZGN1N2I1NTBqaWVucWs5N2RkcmRmaHBjQGc" title="alternate"/>
    <link rel="self" type="application/atom+xml" href="https://www.google.com/calendar/feeds/cqdcu7b550jienqk97ddrdfhpc%40group.calendar.google.com/public/basic/_c9ijacj668sj0or16dh38chmcos3eohk64smcdpm64qjce1k6kq3kc9d64qj0g9h65330dq16kojedq170s3gchl6t24cdhg60rk8di588oiqc9k68q36dhn60o30c1g60mj2d1i6gs32e1i60pj0c1g"/>
    <author>
      <name>SB Meetings</name>
      <email>yy.yy@gmail.com</email>
    </author>
  </entry>
</feed>

我的目标是获得以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<items>
   <item>
      <title>Project Manager Meeting</title>
      <date>3/5/2015</date>
      <starttime>9:30 AM</starttime>
      <endtime>10:30 AM</endtime>
      <where>Conference Room NH2/CA Motor City S Vermont 12345
</where>
   </item>
   <item>
      <title>Analyst Meeting</title>
      <date>2/19/2015</date>
      <starttime>9:30 AM</starttime>
      <endtime>10:30 AM</endtime>
      <where>Conference Room NH4/CA Motor City S Vermont 12345
</where>
   </item>
</items>

感谢您的帮助。

这里的想法不是重复已经给出的另一个问题的解决方案,而是专注于从该解决方案中提取日期的问题。考虑:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:atom="http://www.w3.org/2005/Atom"
exclude-result-prefixes="atom">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
    <items>
        <xsl:for-each select="//atom:entry">
            <item>
                <title>
                    <xsl:value-of select="atom:title"/>
                </title>
                <date>
                    <xsl:call-template name="extract-date">
                        <xsl:with-param name="when" select="substring-before(substring-after(atom:content, 'When: '), '&lt;br /&gt;')" />
                    </xsl:call-template>
                </date>
            </item>
        </xsl:for-each>
    </items>
</xsl:template>
<xsl:template name="extract-date">
    <xsl:param name="when"/>
    <xsl:param name="mmm" select="substring-before(substring-after($when, ' '), ' ')"/>
    <xsl:param name="d" select="substring-before(substring-after(substring-after($when, ' '), ' '), ', ')"/>
    <xsl:param name="y" select="substring-before(substring-after($when, ', '), ' ')"/>
    <xsl:param name="m" select="string-length(substring-before('JanFebMarAprMayJunJulAugSepOctNovDec', $mmm)) div 3 + 1" />
    <xsl:value-of select="concat($m, '/', $d, '/', $y)"/>
</xsl:template>
</xsl:stylesheet>

请注意,发送到命名模板的参数正是上一个问题中的表达式。

这里的结果是:

<?xml version="1.0" encoding="UTF-8"?>
<items>
   <item>
      <title>Project Manager Meeting</title>
      <date>3/5/2015</date>
   </item>
   <item>
      <title>Analyst Meeting</title>
      <date>2/19/2015</date>
   </item>
</items>

剩下的留给读者练习。

最新更新