Re: [xsl] Removing unwanted namespaces

Subject: Re: [xsl] Removing unwanted namespaces
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 11 Jan 2012 15:02:05 -0500
Ram,

My code can be rewritten in XSLT 1.0:

<xsl:template match="*">
  <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<xsl:template match="/*">
  <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
    <xsl:copy-of select="//namespace::*[.=namespace-uri(..)]"/>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

(The redundant copying of namespace nodes here could be an expensive operation, and won't work properly if you have clashes among your prefixes. You could avoid this with Muenchian grouping but I'm not sure it would be worth it.)

Plus, I hope that namespace-savvy readers are checking my work since this is a vexing area and I am not at all certain my advice is uniformly good. :-P

Cheers,
Wendell

On 1/11/2012 2:36 PM, ram wrote:
Thanks for the reply Wendell and Matthieu. I have to use xslt 1.0, unfortunatly i cant use them


--- On Wed, 11/1/12, Wendell Piez<wapiez@xxxxxxxxxxxxxxxx> wrote:


From: Wendell Piez<wapiez@xxxxxxxxxxxxxxxx>
Subject: Re: [xsl] Removing unwanted namespaces
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Date: Wednesday, 11 January, 2012, 11:05 PM
Ram,

It will look something like this (XSLT 2.0):

<xsl:template match="*">
   <xsl:copy copy-namespaces="no">
     <xsl:copy-of select="@*"/>
     <xsl:apply-templates/>
   </xsl:copy>
</xsl:template>

<xsl:template match="soapenv:Header/*"/>

However, note that while this meets your requirement as
stated, it does not provide declarations at the top for all
namespaces in use, which you probably want. This is somewhat
trickier.

Assuming your input maps namespaces to prefixes one-to-one,
you could address that problem like this:

<xsl:template match="/*">
   <xsl:copy copy-namespaces="no">
     <xsl:copy-of select="@*"/>
     <xsl:for-each-group select="//*"
group-by="namespace-uri(.)">
       <xsl:namespace
name="{prefix-from-QName(node-name(.))}">
         <xsl:value-of
select="current-grouping-key()"/>
       </xsl:namespace>
     </xsl:for-each-group>
     <xsl:apply-templates/>
   </xsl:copy>
</xsl:template>

You might want to check this paper out (or at any rate, the
stylesheets bundled with it, which offer more comprehensive
solutions to namespace problems):

http://www.ncbi.nlm.nih.gov/books/NBK62086/

I hope this helps,
Wendell

On 1/11/2012 10:55 AM, ram wrote:
Hi,
      I have a soap request which
contains header and body information. I want to remove the
content inside the header and the namespaces for those which
are inside the  header .
Here is my example

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:con="http://abcd.com/abc/context"; xmlns:dat="http://test.com/test/testContracts"; xmlns:head="http://abcd.com/abc/header"; xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";>
     <soapenv:Header>

<head:abcdHeader>

<head:Props>



</head:Props>

<head:Routing>



</head:Routing>

</head:abcdHeader>

<con:Context>

<con:CtxProps>

context info

</con:CtxProps>

</con:Context>
     </soapenv:Header>
     <soapenv:Body>
         Body content
     </soapenv:Body>
</soapenv:Envelope>


The output i am expecting is like below <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:dat="http://test.com/test/testContracts";
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";>
<soapenv:Header>

     </soapenv:Header>
     <soapenv:Body>
         Body content
     </soapenv:Body>
</soapenv:Envelope>


I tried the following xslt
code which removes the Header Info, but it still keeping the
namespaces which references the ones inside the
header.


<xsl:template
match="@*|node()">

<xsl:copy>

     <xsl:apply-templates
select="@*|node()"/>

</xsl:copy>
     </xsl:template>
     <xsl:template
match="//*[local-name()='abcdHeader'] ">


</xsl:template>
<xsl:template
match="//*[local-name()='Context'] "/>



The output that i am getting

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:dat="http://test.com/test/testContracts";
xmlns:head="http://abcd.com/abc/header";
xmlns:con="http://abcd.com/abc/context";
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";>
     <soapenv:Header>

     </soapenv:Header>
     <soapenv:Body>
         Body content
     </soapenv:Body>
</soapenv:Envelope>

i dont want to header and
context namesspaces in the output. How to fix this issue.

--kk




-- ====================================================================== Wendell Piez

mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.
         http://www.mulberrytech.com
17 West Jefferson Street
           Direct Phone:
301/315-9635
Suite 207

             Phone:
301/315-9631
Rockville, MD  20850

        Fax: 301/315-8285
----------------------------------------------------------------------
   Mulberry Technologies: A Consultancy Specializing in
SGML and XML
======================================================================



-- ====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================

Current Thread