cancel
Showing results for 
Search instead for 
Did you mean: 

XSLT to remove namespace prefix

Former Member
0 Kudos

Hi,

I am working on Proxy to File(xml) scenario. In the output file we are getting namespace prefix for all the elements. Below is the screenshot of the same.

Our requirement is without prefix.

Can anyone help me with the XSLT code to manipulate the PI output.

PI output:

Required Output:

Thanks,

Nida Fatima

Accepted Solutions (0)

Answers (5)

Answers (5)

Jochen_H
Participant
0 Kudos

Hi Nida

If none of this works, you could try a different trick:

If you have designed the XSD in PI, you export it via copy paste to a xsd-file.

Then remove the namespace information in the .xsd-schema.

Then import this .xsd schema as an external definition back to PI and use this in the message type.

PI should then suppress the namespace information. It's a bit of a dirty trick, but I did it once successfully.

Regards

Jochen

former_member190293
Active Contributor
0 Kudos

Hi Fatima!

If you want to remove "ns1" prefix and use namespace of that prefix as value of attribute "xsi:schemaLocation" of your message's root node, you could use following XSL transformation:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"                                                                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                       version="1.0">

  

    <xsl:output indent="yes"/>  

  

     <xsl:template match="/*">

          <xsl:element name="{local-name()}">

               <xsl:attribute name="xsi:schemaLocation"><xsl:value-of select="namespace-uri(.)"/></xsl:attribute>

               <xsl:apply-templates/>

          </xsl:element>

     </xsl:template>

  

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

          <xsl:element name="{local-name()}">

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

          </xsl:element>

    </xsl:template>

  

    <xsl:template match="comment() | text() | processing-instruction()">

          <xsl:copy/>

    </xsl:template>

</xsl:stylesheet>

Regards, Evgeniy.

Harish
Active Contributor
0 Kudos

Hi Nida,

you can create a one to one message mapping and the below code in "Attributes and Methods" section. This will remove the ns1 namespace.

If you have more namespace prefix then you need to add all the namespace prefix

public void transform(TransformationInput in, TransformationOutput out)

  throws StreamTransformationException {

try {

  String sourcexml = ""; String targetxml =""; String line ="";

  InputStream ins =    in.getInputPayload().getInputStream();

  BufferedReader br = new BufferedReader( new InputStreamReader(ins));

  while ((line = br.readLine()) != null) 

  sourcexml +=line+"\n"; 

  br.close();

  targetxml =sourcexml;

  targetxml = targetxml.replaceAll("ns1:", "");

  out.getOutputPayload().getOutputStream().write(targetxml.getBytes());

  } catch (Exception e) {   throw new StreamTransformationException(e.getMessage());   }

}

reference -

Also if you really want to go for XSLT then use the below XSL code

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" version="1.0" encoding="UTF-8" />

        <xsl:template match="*">

                <xsl:element name="{local-name()}" >

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

                </xsl:element>

        </xsl:template>

</xsl:stylesheet>

but the best way is to achieve this using XMLAnonymizer module because it is standard feature.

Remove Namespace in PI by XMLAnonymizer Bean in... | SCN

regards,

Harish

former_member190293
Active Contributor
0 Kudos

Hi Nida!

If you designed your source message/data types in ESR you can use "Qualify schema" parameter in Data Type editor.

Regards, Evgeniy.

manoj_khavatkopp
Active Contributor
0 Kudos

Add Below Bean in receiver file channel to remove prefix:

AF_Modules/XMLAnonymizerBean

anonymizer.acceptNamespaces http://www.example.org/Order '' (2 single quotes)


anonymizer.quote  '


Br,

Manoj