Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
jazzGrewal22
Contributor
Contributor

use tXMLMap to read non-constant namespace tags from a SOAP response

Hi All,

I would appreciate if someone can help me determining a way with which I can ignore namespaces in talend tXMLMap component.

I am calling a WebService and reading the response using tXMLMap as it has to extract data using nested loops. The data exraction part is fine, it is just that for a few tags, the SOAP response is not giving a specific prefix.

for example sometime it adds a ns3: infront of a property, and sometimes it adds ns2: infront of the same property. Which makes it difficult to determine which one to use. If I use ns2 ,and the response comes back with ns3, etl results in nullPointer exception.

0695b00000UxdnAAAR.png

Is there any way in tXMLMap we can ignore the namespace prefix ?

Labels (4)
1 Solution

Accepted Solutions
Anonymous
Not applicable

You need to speak with the provider of the web service to resolve this legitimately. The namespace is used to identify fields accurately. How do you know that ns3:value actually holds the same data as ns2:value? This needs to be understood before you go down the route of ignoring the namespace.

 

However, if you need to remove the namespace tags, I have quickly knocked up some code that will do this. This is heavily "borrowed" from a couple of sources. You will need to run this code on your output XML, save it and regenerate your tXMLMap using the processed XML. This will build your XML schema for your tXMLMap. You will also need to run this code after every time you get XML back from your service. But it will remove the namespace tags which will keep your XML consistent.

 

Take a look at the following Talend Routine.....

 

***********************************************************************************************************************************

package routines;

 

 

import javax.xml.transform.Source;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import java.io.StringReader;

import java.io.StringWriter;

import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.stream.StreamSource;

 

 

public class RemoveXMLNamespace {

 

   

  public static String removeNamespace(String xml) {

   

   String strXslt = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\r\n" +

   "<xsl:template match=\"node()\">\r\n  <xsl:copy>\r\n   <xsl:apply-templates select=\"node()|@*\" />\r\n" +   

   "</xsl:copy>\r\n </xsl:template>\r\n\r\n <xsl:template match=\"*\">\r\n  <xsl:element name=\"{local-name()}\">\r\n" +    

   "<xsl:apply-templates select=\"node()|@*\" />\r\n  </xsl:element>\r\n </xsl:template>\r\n\r\n <xsl:template match=\"@*\">\r\n" +  

   "<xsl:attribute name=\"{local-name()}\">\r\n   <xsl:apply-templates select=\"node()|@*\" />\r\n  </xsl:attribute>\r\n" +

   "</xsl:template>\r\n</xsl:stylesheet>";

   

   String returnVal = "";

   

   try{

      TransformerFactory factory = TransformerFactory.newInstance();

      Source xslt = new StreamSource(new StringReader(strXslt));

      Transformer transformer = factory.newTransformer(xslt);

 

      Source text = new StreamSource(new StringReader(xml));

     

      StringWriter writer = new StringWriter();

   

     

      transformer.transform(text, new StreamResult(writer));

      returnVal = writer.toString();

     } catch (TransformerConfigurationException e) {

     e.printStackTrace();

     } catch (TransformerException e) {

     e.printStackTrace();

     }

   

   return returnVal;

  }

}

 

 

View solution in original post

1 Reply
Anonymous
Not applicable

You need to speak with the provider of the web service to resolve this legitimately. The namespace is used to identify fields accurately. How do you know that ns3:value actually holds the same data as ns2:value? This needs to be understood before you go down the route of ignoring the namespace.

 

However, if you need to remove the namespace tags, I have quickly knocked up some code that will do this. This is heavily "borrowed" from a couple of sources. You will need to run this code on your output XML, save it and regenerate your tXMLMap using the processed XML. This will build your XML schema for your tXMLMap. You will also need to run this code after every time you get XML back from your service. But it will remove the namespace tags which will keep your XML consistent.

 

Take a look at the following Talend Routine.....

 

***********************************************************************************************************************************

package routines;

 

 

import javax.xml.transform.Source;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import java.io.StringReader;

import java.io.StringWriter;

import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.stream.StreamSource;

 

 

public class RemoveXMLNamespace {

 

   

  public static String removeNamespace(String xml) {

   

   String strXslt = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\r\n" +

   "<xsl:template match=\"node()\">\r\n  <xsl:copy>\r\n   <xsl:apply-templates select=\"node()|@*\" />\r\n" +   

   "</xsl:copy>\r\n </xsl:template>\r\n\r\n <xsl:template match=\"*\">\r\n  <xsl:element name=\"{local-name()}\">\r\n" +    

   "<xsl:apply-templates select=\"node()|@*\" />\r\n  </xsl:element>\r\n </xsl:template>\r\n\r\n <xsl:template match=\"@*\">\r\n" +  

   "<xsl:attribute name=\"{local-name()}\">\r\n   <xsl:apply-templates select=\"node()|@*\" />\r\n  </xsl:attribute>\r\n" +

   "</xsl:template>\r\n</xsl:stylesheet>";

   

   String returnVal = "";

   

   try{

      TransformerFactory factory = TransformerFactory.newInstance();

      Source xslt = new StreamSource(new StringReader(strXslt));

      Transformer transformer = factory.newTransformer(xslt);

 

      Source text = new StreamSource(new StringReader(xml));

     

      StringWriter writer = new StringWriter();

   

     

      transformer.transform(text, new StreamResult(writer));

      returnVal = writer.toString();

     } catch (TransformerConfigurationException e) {

     e.printStackTrace();

     } catch (TransformerException e) {

     e.printStackTrace();

     }

   

   return returnVal;

  }

}