[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Re: Re: Removing namespaces from source document (
I don't think it is a good idea to strip elements of all namespaces they don't immediately use. This ends up with re-defining the same namespace over and over again on many different elements -- both difficult to understand and to change/maintain. For example, the following xml source: <root xmlns:good-ns="http://good.com" xmlns:bad-ns="http://bad.com"> <parent color="red"> <good-ns:child shape="square"> some text </good-ns:child> </parent> <parent color="blue"> <good-ns:child shape="triangle"> some text </good-ns:child> </parent> <parent color="green"> <good-ns:child shape="circle"> some text </good-ns:child> </parent> </root> will be transformed into: <root> <parent color="red"> <good-ns:child xmlns:good-ns="http://good.com" shape="square"> some text </good-ns:child> </parent> <parent color="blue"> <good-ns:child xmlns:good-ns="http://good.com" shape="triangle"> some text </good-ns:child> </parent> <parent color="green"> <good-ns:child xmlns:good-ns="http://good.com" shape="circle"> some text </good-ns:child> </parent> </root> Cheers, Dimitre Novatchev. "Christopher R. Maden" <crism at maden dot org> wrote: At 00:15 8/4/02, Dimitre Novatchev wrote: > <xsl:template match="*"> > <xsl:element name="{name()}" namespace="{namespace-uri(.)}"> > <xsl:copy-of select="namespace::*[name() != 'bad-ns']" /> > <xsl:apply-templates select="node()|@*" /> > </xsl:element> > </xsl:template> I was almost there... This example still requires hard-coding of the specific namespaces that aren't desired - in otherwords, it's a sort of blacklist filter. It's also possible to do this constructively: <!-- For all elements: --> <xsl:template match="*"> <xsl:choose> <!-- If there is no prefix, and no default namespace in scope: --> <xsl:when test="substring-before(name(current()),':') = '' and not(namespace::*[name() = ''])"> <!-- Just create the element with no namespace. --> <xsl:element name="{name()}"> <xsl:apply-templates select="node()|@*" /> </xsl:element> </xsl:when> <!-- If there's no prefix, but there is a default namespace in scope: --> <xsl:when test="substring-before(name(current()),':') = ''"> <!-- Make the element with the default namespace. --> <xsl:element name="{name()}" namespace="{namespace-uri(.)}"> <!-- Copy the default namespace. --> <xsl:copy-of select="namespace::*[name*() = '']"/> <xsl:apply-templates select="node()|@*"/> </xsl:element> </xsl:when> <!-- But if there is a prefix, or a default namespace: --> <xsl:otherwise> <!-- Then create the element with the right namespace, --> <xsl:element name="{name()}" namespace="{namespace-uri(.)}"> <!-- and copy the namespace actually used by this element. --> <xsl:copy-of select="namespace::* [name() = substring-before(name(current()),':')]"/> <xsl:apply-templates select="node()|@*" /> </xsl:element> </xsl:otherwise> </xsl:choose> </xsl:template> __________________________________________________ Do You Yahoo!? Yahoo! Tax Center - online filing with TurboTax http://taxes.yahoo.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|