cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

HI,

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>

</RecordNode>

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:

<Field1>12000</Field1>

Regards,

Singh

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi Singh,

                   Please try below XSLT code

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<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>

Regards

Anupam

Former Member
0 Kudos

Hi Anupam,

It worked exactly as desired. Could you please explain the above code especially the match pattern.

anupam_ghosh2
Active Contributor
0 Kudos

Hi Singh,

              

The <xsl:template> element is used to build templates (or rules to apply when specific match is found).

The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression. Using wildcard such as *,@*,node() can be used to select unknown XML elements. The meanings of these are shown below

WildcardDescription
*Matches any element node
@*Matches any attribute node
node()Matches any node of any kind

After root node is matched it is copied to target with its attribute,namespace and value. Then <xsl:copy> element creates a copy of the current node or the root node.

Namespace nodes of the current node are automatically copied as well, but child nodes and attributes of the current node are not automatically copied. Hence namepace information of child nodes do not flow to target XML.

Regards

Anupam

Former Member
0 Kudos

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> 

Answers (1)

Answers (1)

Former Member
0 Kudos

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());

    }

}

Regards

Lucho

Former Member
0 Kudos

Hi Luis,

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