Skip to Content
avatar image
Former Member

Need Help in XSLT Mapping: Remove Namespace & Prefix except one element


The source structure coming from a webservice contains xml namespace in all elements. I need to remove it from all elements except the top element. I can't use XML anonymizer bean for some reason. The structure is like:

<RecordNode xmlns="urn:xxyy:xyz">

   <Field1 xmlns="urn:xxyy:xyz"> 12000</Field1>

   <Field2 xmlns="urn:xxyy:xyz"> 900</Field2>


Thus I need to remove this section (xmlns="urn:xxyy:xyz">) from all fields i.e field1,field2,.....last field Except the top element i.e RecordNode. I could get XSLT mapping for removing all namespaces but how do I do it for the above case. Please note that after XSLT mapping, the field say field1 should look like:




Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Apr 26, 2013 at 05:15 AM

    Hi Singh,

                       Please try below XSLT code

    <xsl:stylesheet version="1.0" xmlns:xsl=""> <xsl:output method="xml" omit-xml-declaration="yes"/>  <xsl:template match="@* | node()">        <xsl:copy>            <xsl:apply-templates select="@* | node()"/>        </xsl:copy>    </xsl:template></xsl:stylesheet>

    For this input

    <RecordNode xmlns="urn:xxyy:xyz">   <Field1 xmlns="urn:xxyy:xyz"> 12000</Field1>   <Field2 xmlns="urn:xxyy:xyz"> 900</Field2></RecordNode>

    using the xslt I get this output

    <RecordNode xmlns="urn:xxyy:xyz">   <Field1> 12000</Field1>   <Field2> 900</Field2></RecordNode>



    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Anupam Ghosh

      Hi Anupam, this was helpful to me!

      But taking one step further do you know how to add a namespace into an element that is not root? Taking this same example lets say I want to go to:

      1. <RecordNode xmlns="urn:xxyy:xyz"> 
      2.    <Field1> 12000</Field1> 
      3.    <Field2 xmlns="urn:abcdefg"> 900</Field2> 
      4. </RecordNode> 
  • avatar image
    Former Member
    Apr 25, 2013 at 11:07 PM

    Hi You can use JavaMapping/

    public class SOAP_Document_to_XML_Document extends AbstractTransformation {    Document docOut = null;    //Constants_JM Const = new Constants_JM();    /** Creates a new instance of Main */    public SOAP_Document_to_XML_Document() {    } private void execute(InputStream in, OutputStream out)  throws StreamTransformationException {                DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();        try {            DocumentBuilder builderel=factory.newDocumentBuilder();            Document docIn=builderel.parse(in);            docOut=builderel.newDocument();            String sResult = null;                        Node node;                                    NodeList nListRec = docIn.getElementsByTagName("*");            node = nListRec.item(0);                    Node clon = docOut.importNode(node,true);            docOut.appendChild(clon);                        sResult = convertDocument_String(docOut);                         sResult = sResult.replace("xmlns=\"urn:xxyy:xyz\"","");                        out.write(sResult.toString().getBytes("UTF-8"));            out.close();                                } catch (Exception ex) {            ex.printStackTrace();            System.out.println("Error ---" + ex.getMessage().toString() );        }    } public static String convertDocument_String(Document Doc){        String sResult ="";        StringWriter writer = new StringWriter();        try {            DOMSource domSource = new DOMSource(Doc);            StreamResult result = new StreamResult(writer);            TransformerFactory tf = TransformerFactory.newInstance();            Transformer transformer = tf.newTransformer();            transformer.transform(domSource, result);        } catch (Exception ex) {            ex.printStackTrace();        }        sResult = writer.toString();        return sResult;    }    public void transform(TransformationInput arg1, TransformationOutput arg2) throws StreamTransformationException {     this.execute(arg1.getInputPayload().getInputStream(), arg2.getOutputPayload().getOutputStream());    }}RegardsLucho
    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Luis,

      I need to remove namespaces from all elements except say 'RecordSet'. Will the above code do that??