Google日历XML样式表XSL [英] Google Calendar XML Stylesheet XSL

查看:101
本文介绍了Google日历XML样式表XSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用样式表解析Google Calendar XML输出时需要帮助.进行了彻底的搜索并尝试了不同的解决方案,但是在最近的API更改之后,Google似乎已经更改了其格式.

有人成功完成这项工作吗?

示例摘录已复制到此处: http://johnt.net/xmlextract.txt

此处是所需的输出: http://johnt.net/desiredoutput.txt

非常感谢!

解决方案

您输入的问题是它所携带的信息没有被构造为XML.实际上,大多数都不是完全结构化的-只是XML封装中的HTML有效负载.这使得提取单个数据元素非常困难.

为您提供一个起点,请考虑以下样式表:

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>
                <when>
                    <xsl:value-of select="substring-before(substring-after(atom:content, 'When: '), '&lt;br /&gt;')" />
                </when>
                <where>
                    <xsl:value-of select="substring-before(substring-after(atom:content, 'Where: '), '&lt;br /&gt;')" />
                </where>
            </item>
        </xsl:for-each>
    </items>
</xsl:template>

</xsl:stylesheet>

应用于以下示例输入(基本上是示例示例,在纠正丢失的结束标记之后!):

<?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@KAIPERM
&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@KAIPERM
&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>
      <when>Thu Mar 5, 2015 9:30am to 10:30am 
PST</when>
      <where>Conference Room NH2/CA Motor City S Vermont 12345@KAIPERM
</where>
   </item>
   <item>
      <title>Analyst Meeting</title>
      <when>Thu Feb 19, 2015 9:30am to 10:30am 
PST</when>
      <where>Conference Room NH4/CA Motor City S Vermont 12345@KAIPERM
</where>
   </item>
</items>

Need help in parsing Google Calendar XML output using a stylesheet. Did a thorough search and tried different solutions but it seems Google might have changed their format after the recent API change.

Anyone successful in getting this to work?

A sample extract has been copied here: http://johnt.net/xmlextract.txt

And a desired output is here: http://johnt.net/desiredoutput.txt

Thanks so much!

解决方案

The problem with your input is that the information it carries is not structured as XML. In fact, most of it is not structured at all - it's just HTML payload riding in an XML wrapper. This makes it very difficult to extract individual data elements.

To provide you with a starting point, consider the following stylesheet:

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>
                <when>
                    <xsl:value-of select="substring-before(substring-after(atom:content, 'When: '), '&lt;br /&gt;')" />
                </when>
                <where>
                    <xsl:value-of select="substring-before(substring-after(atom:content, 'Where: '), '&lt;br /&gt;')" />
                </where>
            </item>
        </xsl:for-each>
    </items>
</xsl:template>

</xsl:stylesheet>

When applied to the following example input (which is basically your example response, after correcting a missing closing tag!):

<?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@KAIPERM
&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@KAIPERM
&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>

the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<items>
   <item>
      <title>Project Manager Meeting</title>
      <when>Thu Mar 5, 2015 9:30am to 10:30am 
PST</when>
      <where>Conference Room NH2/CA Motor City S Vermont 12345@KAIPERM
</where>
   </item>
   <item>
      <title>Analyst Meeting</title>
      <when>Thu Feb 19, 2015 9:30am to 10:30am 
PST</when>
      <where>Conference Room NH4/CA Motor City S Vermont 12345@KAIPERM
</where>
   </item>
</items>

这篇关于Google日历XML样式表XSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆