Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robert Valcourt 71 posts 104 karma points
    Jul 20, 2012 @ 13:56
    Robert Valcourt
    0

    XSLT - reading XML attribute value?

    I’m trying to parse an XML feed in Umbraco using XSLT. Everything works fairly well except the following:
    Source XML structure:
    <entry>
    <title type='text'>Karate Class (Juniors, 6 to 13 years of age)</title>
    <gd:when startTime='2012-07-23T18:00:00.000-07:00'/>
    </entry>
    <entry>
    <title type='text'>Karate Class (Juniors, 6 to 13 years of age) version 2</title>
    <gd:when startTime='2012-07-24T18:00:00.000-07:00'/>
    </entry>
    Here is my XSLT:
    <xsl:variable name="google" select="umbraco.library:GetXmlDocumentByUrl('https://www.google.com/calendar/feeds/tewinkel%40uniserve.com/public/full?orderby=starttime&amp;max-results=5&amp;singleevents=true&amp;sortorder=ascending&amp;futureevents=true',1)"/>
    <xsl:for-each select="$google//node()[name()='entry']">
    Title: <xsl:value-of select="node()[name()='title']"/><br />
    When: <xsl:value-of select="node()[name()='gd:when']"/><br /><br />
    </xsl:for-each>
    This XSLT renders the <title> parameter correctly, but I can’t seem to figure out how to read the <gd:when> parameter which has an attribute of startTime='value’ rather than a value between the open/close tag like Title does.
    Can you point me in the right direction! Thx!
  • Jeroen Breuer 4909 posts 12266 karma points MVP 5x admin c-trib
    Jul 21, 2012 @ 18:07
    Jeroen Breuer
    1

    Perhaps it's because you're having a namespace in the xml. More info here: http://msdn.microsoft.com/en-us/library/ms950779.aspx.

    Jeroen

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Jul 25, 2012 @ 00:56
    Chriztian Steinmeier
    1

    Hi Robert,

    It's a namespace issue like Jeroen suggests - so here's how to do it all properly (including fetching the element values without using the ugly name() "hack" :-)

    You just need to tell your XSLT that some elements are in another namespace - you do that by copying the xmlns declarations for the prefixes you need to use. Now, in your case, the whole XML file has a default namespace (the Atom namespace) which you can tell by the xmlns="..." at the beginning - when there's no prefix after the xmlns (e.g.: xmlns:prefix="...") the namespace will be the default. However, you need to specify a prefix when using it from XSLT, to be able to match the elements, so you can just assign it to the atom prefix - like this:

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

    - adding the gd prefix too, you can write your XSLT in a much more readable format:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        xmlns:atom="http://www.w3.org/2005/Atom"
        xmlns:gd="http://schemas.google.com/g/2005"
        exclude-result-prefixes="umb atom gd"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:template match="/">
            <!-- Grab feed XML -->
            <xsl:variable name="google" select="umbraco.library:GetXmlDocumentByUrl(...)" />
    
            <!-- List all the <entry> elements -->
            <xsl:apply-templates select="$google/atom:feed/atom:entry" />
        </xsl:template>
    
        <!-- Template for an entry in the feed -->
        <xsl:template match="atom:entry">
            Title: <xsl:value-of select="atom:title" />
            When: <xsl:value-of select="gd:when/@startTime" /><br /><br />
        </xsl:template>
    
    </xsl:stylesheet>

    /Chriztian

  • Robert Valcourt 71 posts 104 karma points
    Jul 25, 2012 @ 01:04
    Robert Valcourt
    0

    Chriztian,

    That was super helpful and works! Thank you very much.

    Robert

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies