Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
126 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...