XSLT Noob with a problem

TripleJay

Usenet Archive
Joined
Jul 3, 2010
Messages
2
Reaction score
0
Hi,
I am quite new to XML and XSLT and therefore this question might be a bit silly...
however, here is my problem:

I would like to translate the feed from my google contacts to a new format suitable for my telephone.

This is the google feed:
<feed xmlns='http://www.w3.org/2005/Atom'
xmlns:eek:penSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/"CUMBRHo_fip7ImA9WxRbGU0."'> <id></id> <updated>2008-12-10T10:04:15.446Z</updated> <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' /> <title>Elizabeth Bennet's Contacts</title> <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.google.com/m8/feeds/contacts/[email protected]/ full' /> <link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.google.com/m8/feeds/contacts/[email protected]/ full' /> <link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='http://www.google.com/m8/feeds/contacts/[email protected]/full/ batch' /> <link rel='self' type='application/atom+xml' href='http://www.google.com/m8/feeds/contacts/[email protected]/full? max-results=25' /> <author> <name>Elizabeth Bennet</name> <email></email> </author> <generator version='1.0' uri='http://www.google.com/m8/feeds'> Contacts </generator> <openSearch:totalResults>1</openSearch:totalResults> <openSearch:startIndex>1</openSearch:startIndex> <openSearch:itemsPerPage>25</openSearch:itemsPerPage> <entry gd:etag='"Qn04eTVSLyp7ImA9WxRbGEUORAQ."'> <id> http://www.google.com/m8/feeds/contacts/[email protected]/base/c9012de </id> <updated>2008-12-10T04:45:03.331Z</updated> <app:edited xmlns:app='http://www.w3.org/2007/ app'>2008-12-10T04:45:03.331Z</app:edited> <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' /> <title>Fitzwilliam</title> <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' href='http://www.google.com/m8/feeds/photos/media/liz %40gmail.com/c9012de' gd:etag='"KTlcZWs1bCp7ImBBPV43VUV4LXEZCXERZAc."' /> <link rel='self' type='application/atom+xml' href='http://www.google.com/m8/feeds/contacts/[email protected]/ full/c9012de' /> <link rel='edit' type='application/atom+xml' href='http://www.google.com/m8/feeds/contacts/[email protected]/ full/c9012de' /> <gd:phoneNumber rel='http://schemas.google.com/g/2005#home' primary='true'> 456 </gd:phoneNumber> <gd:extendedProperty name='pet' value='hamster' /> <gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/[email protected]/base/ 270f' /> </entry> </feed>

This is my XSLT
<?xml version="1.0"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> <xsl:template match="/"> <phonebooks> <phonebook> <xsl:for-each select="feed/entry"> <contact> <category /> <person> <realName> <xsl:value-of select="title"/> </realName> <imageUrl /> </person> <telephony> tbd... </telephony> <services> <email /> </services> <setup> <ringTone /> <ringVolume /> </setup> </contact> </xsl:for-each> </phonebook> </phonebooks> </xsl:template> </xsl:transform>

An this is the output:
ubuntu@ubuntu:~/Downloads/g2fritzbook2$ xsltproc fritzbook.xsl short.xml <?xml version="1.0"?> <phonebooks><phonebook/></phonebooks>

I would have expected one instance of the for-each instruction with name/title.
What's wrong here?

Regards, TripleJay
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
TripleJay said:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> <xsl:template match="/"> <phonebooks> <phonebook> <xsl:for-each select="feed/entry">
What's wrong here?

The default namespace declaration in the XML needs to be taken into account. With XSLT 2.0 and an XSLT 2.0 processor you could use xpath-default-namespace="http://www.w3.org/2005/Atom" on the xsl:stylesheet element and keep your XSLT unchanged but if you use XSLT
1.0 and an XSLT 1.0 processor then in your stylesheet you need to bind a prefix to the namespace URI and use that prefix in path expressions to qualify element names:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:at="http://www.w3.org/2005/Atom" version="1.0" exclude-result-prefixes="at">

...
<xsl:for-each select="at:feed/at:entry">
...
<xsl:value-of select="at:title"/>
 

Peter Flynn

Usenet Archive
Joined
Jan 2, 2000
Messages
1,813
Reaction score
0
Martin said:
The default namespace declaration in the XML needs to be taken into account.

Quite apart from this, there is an easier way than to use for-each. XSLT is declarative, not procedural, so it would be better to say

<xsl:template match="/">
<phonebooks>
<phonebook>
<xsl:apply-templates select="at:feed/at:entry"/>
</phonebook>
</phonebooks>
</xsl:template>

<xsl:template match="entry">
<contact>
<category/>
<person>
<realName>
<xsl:value-of select="at:title"/>
</realName>
<imageUrl />
</person>

....etc...

</xsl:template>

This makes it much easier to maintain.

///Peter
 

TripleJay

Usenet Archive
Joined
Jul 3, 2010
Messages
2
Reaction score
0
Quite apart from this, there is an easier way than to use for-each. XSLT is declarative, not procedural, so it would be better to say

<xsl:template match="/">
  <phonebooks>
    <phonebook>
      <xsl:apply-templates select="at:feed/at:entry"/>
    </phonebook>
  </phonebooks>
</xsl:template>

<xsl:template match="entry">
  <contact>
    <category/>
    <person>
      <realName>
        <xsl:value-of select="at:title"/>
      </realName>
      <imageUrl />
    </person>

...etc...

</xsl:template>

This makes it much easier to maintain.

///Peter

Great, this worked out!
Martin, thank you very much for the solution. I really should have read the basics before starting to play around ;-) Peter, another thanks for your advise, this really makes the structure easier - I'll take it into acccount.

Regards, TripleJay
 

Hermann Peifer

Usenet Archive
Joined
Nov 16, 2007
Messages
27
Reaction score
0
Quite apart from this, there is an easier way than to use for-each. XSLT is declarative, not procedural, so it would be better to say

<xsl:template match="/">
<phonebooks>
<phonebook>
<xsl:apply-templates select="at:feed/at:entry"/>
</phonebook>
</phonebooks>
</xsl:template>

<xsl:template match="entry">

<xsl:template match="at:entry">

Hermann
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,244
Members
248,133
Latest member
DujonT

Latest Threads

Top