Re: [xsl] different versions of xmlns:foo are never usable together?

Subject: Re: [xsl] different versions of xmlns:foo are never usable together?
From: andrew welch <andrew.j.welch@xxxxxxxxx>
Date: Thu, 14 Jul 2005 16:40:56 +0100
> I'm working on a project that's using XML & XSLT to do lots of munging
> of files.  I've run into an odd problem that I'm hoping isn't a issue of
> who-controls-what.
>
> For example, I've got two files with recipes in them, cake.xml and
> muffin.xml.
>
> In cake.xml is
>    <food:recipe xmlns:food="http://who/knows/Food/1.0";> ... </food:recipe>
>
> In muffin.xml is
>    <food:recipe xmlns:food="http://who/knows/Food/1.1";> ... </food:recipe>
>
> I'd like to use both of them on a style sheet like:
>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> version="1.0"
>         xmlns:food="http://who/knows/Food/1.0";>
>  <xsl:output method="text" encoding="UTF-8"/>
>  <xsl:strip-space elements="*"/>
>  <xsl:template match="food:recipe">
>   <xsl:value-of select="food:name"/>: <xsl:value-of
select="food:calories"/>
>  </xsl:template>
> </xsl:stylesheet>
>
> But the xmlns:food string not being the same makes is selective over
> which it's willing to use.

If two elements have the same local name but are in different
namespaces you really need to see them as two different elements.

The element name is the namespace and the local name joined together -
the xml parser expands the prefix out to the full namespace and then
reports that.  In your case you would have
<http://who/knows/Food/1.0:recipe> and
<http://who/knows/Food/1.1:recipe> which makes it more obvious that
the two are different elements all together.  (I always visualised the
namespace and the local-name to be set in concrete, but that theory
gets broken in 2.0 as there is a copy-namespaces="no" attribute on
copy and copy-of)

If you want to match both in one template then you will need to
declare both namespaces with different prefixes and then match both
elements, eg:

xmlns:food1.0="http://who/knows/Food/1.0";
xmlns:food1.1="http://who/knows/Food/1.1";

template match="food1.0:recipe|food2.0:recipe"

cheers
andrew

Current Thread