Skip to Content
avatar image
Former Member

XSLT mapping to replcae ns1 from output xml payload


Hi,

My main graphical mapping is returning payload as below and same is been passed to third party but we need to replace "ns1" string from output payload to 'pidx".

Could you please provide me the XSLT code to achive the same or is there any better approach to do it.

Graphical output payload:

Required Output:

Thanks & Regards,

Nid Fatima

Capture1.PNG (19.6 kB)
Capture2.PNG (19.8 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Best Answer
    Nov 18, 2014 at 09:20 PM

    If you just want to change the prefix, you can go for XMLAnonymizerBean

    refer to below wiki.

    Changing Namespaces and the encoding format of XML - Process Integration - SCN Wiki

    If you really want xslt then

    Refer to Udo's reply for sample code :

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 18, 2014 at 09:49 PM

    Hi Nida,

    As you haven't written a complete example, i have done one, may be something is lost but you can do a generic XSL for this purpose.

    From this XML:

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

    <ns1:ROOT xmlns:ns1="http://example.org"  ns1:test="" ns1:version="1.0">

    <ns1:MORETAGS>a</ns1:MORETAGS>

    </ns1:ROOT>

    If you use this XSL:

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

    <xsl:stylesheet version="1.0"

       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

       xmlns:ns1="http://example.org"

       xmlns:pidx="http://example.org"

       exclude-result-prefixes="ns1">

       <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

       <xsl:template match="/">

             <xsl:copy>

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

             </xsl:copy>

       </xsl:template>

       <xsl:template match="ns1:*">

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

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

          </xsl:element>

       </xsl:template>

       <xsl:template match="@ns1:*">

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

             <xsl:value-of select="."/>

          </xsl:attribute>

       </xsl:template>

    </xsl:stylesheet>

    You will get this output:

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

    <pidx:ROOT xmlns:pidx="http://example.org" pidx:test="" pidx:version="1.0">

    <pidx:MORETAGS>a</pidx:MORETAGS>

    </pidx:ROOT>

    Regards.

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 18, 2014 at 10:39 PM

    Hi Nida,

    you can also achive this with graphical mapping by using below code in Attribute and method UDF

    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", "pidx");

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

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

    }

      public String concate(String str1, String str2, Container container) throws StreamTransformationException{

      //String output = null;

      return (str1+str2);

      }

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 19, 2014 at 02:21 AM

    Hi Nida,

    You can just simply open the Message Mapping and Edit the <XML Namespace> section with any desired value you want. It is easy and don't need any customization. Check the below blog and give it a try:

    Original MT:

    Modified MT:

    Reference: Quick Tips: Dealing with Namespaces in XI/PI

    Regards,

    Nabendu.


    Add comment
    10|10000 characters needed characters exceeded