List Info

Thread: XSL Transformations (XSLT) -> copy-of element




XSL Transformations (XSLT) -> copy-of element
user name
2006-09-29 09:38:49
Hi.

Sorry to bother you, but one part of the recommendation is
not really 
clear to me. I'm not sure about this statement in the
copy-of:

> When the result is a node-set, all the nodes in the set
are copied in 
> document order into the result tree; copying an element
node copies 
> the attribute nodes, namespace nodes and children of
the element node 
> as well as the element node itself...

What does it mean to copy namespace nodes?

I'll give you an example of what troubles me here. Below you
have a 
simple XML document

<?xml version="1.0"
encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl"
href="test.xsl" ?>
<document>
    <svg>
        <circle cx="30" cy="10"
r="5" />
    </svg>
</document>

Here is an XSL for it:

<?xml version="1.0"
encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlnssl=&q
uot;http://www
.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.or
g/2000/svg">

    <xsl:template match="/">
        <svg width="200px"
height="200px">
            <circle cx="10" cy="10"
r="5" />
            <xsl:apply-templates select="//svg"
/>
        </svg>
    </xsl:template>

    <xsl:template match="svg">
        <xsl:copy-of select="*"/>
    </xsl:template>
</xsl:stylesheet>

This should give me an XML document:

<svg xmlns="http://www.w3.org/20
00/svg" width="200px"
height="200px">
    <circle cx="10" cy="10"
r="5"/>
    <circle cx="30" cy="10"
r="5"/>
</svg>

So is this an error to consider the second (copied) circle
to be in the 
SVG namespace? To put it in another way: what is the
namespace of the 
copied element - the one defined in the XML document or in
the XSL document?

It would be quite natural to me if the resulting document
would be 
treated as if it would be like that all the time, but as
both Opera and 
Firefox display only one circle, then I'm not sure if this
is a bug on 
there side or am I wrong here?

Best regards,
Maciej Jaros.

-- 
E-mail: egilwp.pl


XSL Transformations (XSLT) -> copy-of element
user name
2006-09-29 12:57:18
> Sorry to bother you, but one part of the recommendation
is 
> not really clear to me. I'm not sure about this
statement in 
> the copy-of:
> 
> > When the result is a node-set, all the nodes in
the set are 
> copied in 
> > document order into the result tree; copying an
element node copies 
> > the attribute nodes, namespace nodes and children
of the 
> element node 
> > as well as the element node itself...
> 
> What does it mean to copy namespace nodes?

It means that when you copy an element E that has three
in-scope namespaces,
say xmlns:a="http://a/", xmlns:b="http://b/", and
xmlns="http://c/"
(plus
the XML namespace which is always in scope), then the copy
of the element
will also have those three namespaces in scope.
> 
> I'll give you an example of what troubles me here.
Below you 
> have a simple XML document
> 
> <?xml version="1.0"
encoding="UTF-8"?>
> <?xml-stylesheet type="text/xsl"
href="test.xsl" ?> <document>
>     <svg>
>         <circle cx="30" cy="10"
r="5" />
>     </svg>
> </document>

There are no namespaces in this document, other than the XML
namespace which
is always present. So copy-of when applied to the
<circle> element creates
an element which has no namespace nodes (again except for
the XML
namespace).

(stylesheet abbreviated to
         <svg width="200px"
height="200px">
            <circle cx="10" cy="10"
r="5" />
            <xsl:copy-of select="//svg/circle"
/>
        </svg>

> 
> This should give me an XML document:
> 
> <svg xmlns="http://www.w3.org/20
00/svg" width="200px"
height="200px">
>     <circle cx="10" cy="10"
r="5"/>
>     <circle cx="30" cy="10"
r="5"/>
> </svg>

No, it shouldn't: see below.
> 
> So is this an error to consider the second (copied)
circle to 
> be in the SVG namespace? To put it in another way: what
is 
> the namespace of the copied element - the one defined
in the 
> XML document or in the XSL document?

This has nothing to do with copying of namespace nodes. When
an element is
copied, the copy has the same expanded name as the original
element. The
expanded name of the copied circle element is (local name =
circle,
namespace = null). So the circle element in the result
document will not be
in the SVG namespace, it will be in the "null"
namespace. This means it must
be serialized as:

<svg xmlns="http://www.w3.org/20
00/svg" width="200px"
height="200px">
>     <circle cx="10" cy="10"
r="5"/>
>     <circle xmlns="" cx="30"
cy="10" r="5"/>
> </svg>

> It would be quite natural to me if the resulting
document 
> would be treated as if it would be like that all the
time, 
> but as both Opera and Firefox display only one circle,
then 
> I'm not sure if this is a bug on there side or am I
wrong here?
> 

I can't comment on particular products but there is no doubt
what the
correct result of this transformation is. Copying an element
never changes
its name. If you want to create an element in a different
namespace from the
original, you need to use <xsl:element> rather than
<xsl:copy-of>.

Could I suggest that in future, the best place to ask
questions about
interpretation of the XSLT language is the xsl-list at
mulberrytech.com. You
will almost always get a good answer to your question,
especially if it's
one (like this) that is frequently asked.

Regards,

Michael Kay
(personal response)


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )