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>
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.
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:
XSLT - reading XML attribute value?
<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>
<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>
Title: <xsl:value-of select="node()[name()='title']"/><br />
When: <xsl:value-of select="node()[name()='gd:when']"/><br /><br />
</xsl:for-each>
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
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:
- adding the gd prefix too, you can write your XSLT in a much more readable format:
/Chriztian
Chriztian,
That was super helpful and works! Thank you very much.
Robert
is working on a reply...