Subject: Re: [xsl] [XSLT 1.0] Strip unused namespaces, but retain namespace declarations for elements with QName values? From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Wed, 30 Mar 2011 15:03:11 -0400 |
Hi Folks,
I want to transform this:
-------------------------------------------- <t:Test xmlns:t="http://www.test.org" xmlns:unused="http://www.unused.org" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<t:faultcode>SOAP:client</t:faultcode>
</t:Test> --------------------------------------------
to this:
------------------------------------ <Test xmlns="http://www.test.org" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<faultcode>SOAP:client</faultcode>
</Test> ------------------------------------
That is, I want to:
1. Replace namespace-qualified elements with a default namespace, e.g.,
Change this: <t:Test xmlns:t="..."
to this: <Test xmlns="..."
2. Delete unused namespace declarations, e.g.,
Delete this: xmlns:unused="..."
3. Retain a namespace declaration if it declares a namespace prefix that is used in an element QName value, e.g.,
Retain this: xmlns:soap="..."
because the prefix is used in this element's value: <faultcode>SOAP:client</faultcode>
The following template rule does (1) and (2) but fails to implement (3):
<xsl:template match="*[namespace-uri()]">
<xsl:element name="{local-name()}" namespace="{namespace-uri()}"> <xsl:apply-templates select="@* | node()"/> </xsl:element>
</xsl:template>
Any ideas? I am using XSLT 1.0
<xsl:template match="*"> <xsl:element name="{local-name()}" namespace="{namespace-uri()}"> <xsl:for-each select=".//namespace::*"> <xsl:if test="(..//*)[namespace-uri()=current() and namespace-uri()!=namespace-uri(current()/..)] <xsl:copy-of select="."/> </xsl:if> </xsl:for-each> <xsl:apply-templates select="@*|node()"/> </xsl:element> </xsl:template>
<xsl:template match="*"> <xsl:element name="{local-name()}" namespace="{namespace-uri()}"> <xsl:for-each select=".//namespace::*"> <!-- ( ( find an element using the namespace but not using the parent's namespace ) or ( find text content beginning with the namespace prefix ) )--> <xsl:if test="(..//*)[namespace-uri()=current() and namespace-uri()!=namespace-uri(current()/..)] or (..|..//*)[starts-with(.,concat(name(current()),':'))]"> <xsl:copy-of select="."/> </xsl:if> </xsl:for-each> <xsl:apply-templates select="@*|node()"/> </xsl:element> </xsl:template>
~/t/ftemp $ cat roger.xml <t:Test xmlns:t="http://www.test.org" xmlns:unused="http://www.unused.org" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
</Test>~/t/ftemp $ ~/t/ftemp $ cat roger.xsl <?xml version="1.0" encoding="US-ASCII"?> <?xml-stylesheeta href="abc.xsl" type="text/xsl"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()"><!--identity for all other nodes--> <xsl:copy> <xsl:copy-of select=".//namespace::*"/> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
<xsl:template match="*" priority="1"> <xsl:element name="{local-name()}" namespace="{namespace-uri()}"> <xsl:for-each select=".//namespace::*"> <!-- ( ( find an element using the namespace but not using the parent's namespace ) or ( find text content beginning with the namespace prefix ) )--> <xsl:if test="(..//*)[namespace-uri()=current() and namespace-uri()!=namespace-uri(current()/..)] or (..|..//*)[starts-with(.,concat(name(current()),':'))]"> <xsl:copy-of select="."/> </xsl:if> </xsl:for-each> <xsl:apply-templates select="@*|node()"/> </xsl:element> </xsl:template>
-- Contact us for world-wide XML consulting & instructor-led training Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] [XSLT 1.0] Strip unused n, Michael Kay | Thread | [xsl] xsl elements in template not , Christian Schouten |
Re: [xsl] [XSLT 1.0] A template tha, Michael Kay | Date | [xsl] xsl elements in template not , Christian Schouten |
Month |