RE: [xsl] Adding Missing Element

Subject: RE: [xsl] Adding Missing Element
From: "Plana, Richard" <Richard.Plana@xxxxxxxxxxxxxxxx>
Date: Wed, 23 Jan 2008 19:32:25 -0500
-----Original Message-----
From: David Carlisle [mailto:davidc@xxxxxxxxx]
Sent: Wednesday, January 23, 2008 4:45 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Adding Missing Element



> One question, though: currently, all added nodes from the template
seem
> to have an xmlns:n attribute added to the element. Where can I switch
> off the functionality that does this? (and yes, <A> belongs to the
> namespace n).

XSLT will not add namespace declarations to the output if that namespace
binding is already in scope from an ancestor element. So barring a bug
in your system, the output is probably not quite as this description
would indicate, can you post a small example.

Input file:

<?xml version="1.0"?>
<foo xmlns="http://bar.com/ns";>
<A/>
<A><B/></A>
<A/>
<A><B><C/></B></A>
</foo>

Transform:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0"
               xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
               xmlns:c="http://bar.com/ns";
>

        <!-- The identity transform -->
        <xsl:template match="@*|node()">
                <xsl:copy>
                        <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
        </xsl:template>

        <xsl:template match="c:A">
                <xsl:variable name="count"
select="count(descendant-or-self::c:B)"/>
                <xsl:copy>
                        <xsl:copy-of select="@*"/>
                        <xsl:apply-templates/>
                        <xsl:if test="$count = 0">
                                <B>
                                        ZZZZZZZ
                                </B>
                        </xsl:if>
                </xsl:copy>
        </xsl:template>
</xsl:transform>

Output:

<?xml version="1.0" encoding="UTF-8"?><foo xmlns="http://bar.com/ns";>
<A><B xmlns:c="http://bar.com/ns";>
                                        ZZZZZZZ
                                </B></A>
<A><B/></A>
<A><B xmlns:c="http://bar.com/ns";>
                                        ZZZZZZZ
                                </B></A>
<A><B><C/></B></A>
</foo>

Bug in my XSLT engine?
--

Richard Plana

Current Thread