(Noob) Working with namespaces in XSLT

D

daz_oldham

Hi All

I am re-visiting XSLT after not touching it for about three years, so
to be honest I am starting myself from scratch.

If I have XML under the namespace "http://tempuri.com/abc/2002/09",
should I declare this at the top of my XSLT?

Additionally, if I have the following XML:

<myxmlroot xmlns="http://tempuri.com/abc/2002/09">
<header somevalue="" anotherval="" />
<mainbody>
<repeateditem id="0">Zero</repeateditem>
<repeateditem id="1">One</repeateditem>
<repeateditem id="2">Two</repeateditem>
</mainbody>
</myxmlroot>

Where am I going wrong in my XSLT?

<xsl:for-each select="/myxmlroot/mainbody">
Loop<br />
</xsl:for-each>

That doesn't seem to give me any output - hence my question about the
namespace.

Many thanks

Darren
 
J

Joe Kesselman

daz_oldham said:
If I have XML under the namespace "http://tempuri.com/abc/2002/09",
should I declare this at the top of my XSLT?

Yes. Bind it to a prefix. Then use that prefix in your XPaths, even if
you used the xmlns= shorthand in your documents; XSLT has no concept of
default namespace, so all references to namespaced elements and
attributes must use prefixed names.

<xsl:for-each select="/myns:myxmlroot/myns:mainbody"
xmlns:myns="http://tempuri.com/abc/2002/09">
Loop<br />
</xsl:for-each>

(In most cases you'd declare the prefix at the top of the stylesheet;
I'm doing it here just for illustrative purposes.)
 
D

daz_oldham

Thanks Joe

I have that working now :)

Well, I do have futher problems, but I'll have a proper bash at them
before I post again!

Daz
 

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
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top