cancel
Showing results for 
Search instead for 
Did you mean: 

delete root node and content of node via XSLT

Former Member
0 Kudos

Is it possible to delete a root node and contents, but leave the remaining xml via xslt??

i receive this:


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
 <!-- 
 XML Validation Inbound Channel Response 
  --> 
 <ser-root:createContractResponse xmlns:ser-root="http://host/ws/createContract" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns:BBP_ES_OA_UPDATE.Response xmlns:ns="urn:sap-com:document:sap:rfc:functions">
<CROSSREF>
  <ES_MA_NUMBER>contracts.Contract:MA-ICE-00000190</ES_MA_NUMBER> 
  <ES_DOC_TYPE>CTR</ES_DOC_TYPE> 
  <ES_MA_NAME>Test 1611-a</ES_MA_NAME> 



</ns:BBP_ES_OA_UPDATE.Response>
    </ser-root:createContractResponse>

but want to delete the ser-root:createContractResponse and namespaces within and leave the ns:BBP_ES_OA_UPDATE...

i had come up with this.,,,


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

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

    <xsl:template match="results">
        <xsl:copy>
            <xsl:apply-templates select="ser-root:createContractResponse /*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

but this does not work for me! any ideas?

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

i changed the "copy" to this


 <xsl:copy-of select="node()" />

and it works! thanks to everyone! 🐵

Former Member
0 Kudos

you can ignore the namespace which i can tell you for sure with the help of module adapter - XMLAnonymizerBean

also give try to MessageTransformBean to ignore the root node.

i hope this should help you.

Former Member
0 Kudos

thanks for the idea.

i have tried the anonymizer for namespace but as this is a synchronous call, it failed on the outbound.

how would i ignore the root node using messagetransformbean?

anupam_ghosh2
Active Contributor
0 Kudos

Hi,

I am not sure of the version of PI you are working. In case you are working in Pi 7.1 here is the jav ampping code which meets your requirement

input xml


 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
 <!-- 
 XML Validation Inbound Channel Response 
  --> 
 <ser-root:createContractResponse xmlns:ser-root="http://host/ws/createContract" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns:BBP_ES_OA_UPDATE.Response xmlns:ns="urn:sap-com:document:sap:rfc:functions">
<CROSSREF>
  <ES_MA_NUMBER>contracts.Contract:MA-ICE-00000190</ES_MA_NUMBER> 
  <ES_DOC_TYPE>CTR</ES_DOC_TYPE> 
  <ES_MA_NAME>Test 1611-a</ES_MA_NAME> 
</CROSSREF> 
 
 
</ns:BBP_ES_OA_UPDATE.Response>
    </ser-root:createContractResponse>

Output xml after mapping


<?xml version="1.0" encoding="UTF-8"?><ns:BBP_ES_OA_UPDATE.Response xmlns:ns="urn:sap-com:document:sap:rfc:functions">
<CROSSREF>
  <ES_MA_NUMBER>contracts.Contract:MA-ICE-00000190</ES_MA_NUMBER> 
  <ES_DOC_TYPE>CTR</ES_DOC_TYPE> 
  <ES_MA_NAME>Test 1611-a</ES_MA_NAME> 
</CROSSREF> 
 
 
</ns:BBP_ES_OA_UPDATE.Response>

java mapping code


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;

import com.sap.aii.mapping.api.StreamTransformationException;

public class removeTag extends AbstractTransformation{


	public void transform(TransformationInput arg0, TransformationOutput arg1)
	throws StreamTransformationException {
		this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());
// TODO Auto-generated method stub

}

public void execute(InputStream in, OutputStream out)
throws StreamTransformationException {
	
try
{
	DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
	DocumentBuilder builderel=factory.newDocumentBuilder();
	/*input document in form of XML*/
	Document docIn=builderel.parse(in);
	/*document after parsing*/
	Document docOut=builderel.newDocument();
	TransformerFactory tf=TransformerFactory.newInstance();
	Transformer transform=tf.newTransformer();
	Node root;
	NodeList l;
	l=docIn.getElementsByTagName("ns:BBP_ES_OA_UPDATE.Response");
	root=docOut.createElement(l.item(0).getNodeName().toString());
	Element r=(Element)root;
	r.setAttribute(l.item(0).getAttributes().item(0).getNodeName(),l.item(0).getAttributes().item(0).getNodeValue());
	root=r;
	l=l.item(0).getChildNodes();
	for(int i=0;i<l.getLength();++i)
	{
		Node temp=docOut.importNode(l.item(i),true);
		root.appendChild(temp);
		
	}
	docOut.appendChild(root);
	transform.transform(new DOMSource(docOut), new StreamResult(out)); 
}
catch(Exception e)
{
	e.printStackTrace();
}


}

public void setParameter(Map arg0) {


}

public static void main(String[] args) {
try{
	removeTag genFormat=new removeTag();
	FileInputStream in=new FileInputStream("C:\\apps\\sdn\\barry.xml");
	FileOutputStream out=new FileOutputStream("C:\\apps\\sdn\\barry1.xml");
	genFormat.execute(in,out);
}
catch(Exception e)
{
e.printStackTrace();
}
}

}

In case of pi7.0 you need little changes to the mapping structure as shown below. We do not need the function transform. All other functions will have same code as above.


public class removeTag implements StreamTransformation{




public void execute(InputStream in, OutputStream out)
throws StreamTransformationException {

}
}

public void setParameter(Map arg0) {
}

public static void main(String[] args) {
}

}

Hope this solves your problem.

regards

Anupam

Former Member
0 Kudos

Hello barry,

If you want to do this with xslt, this script will help you


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ser-root1="http://host/ws/createContract"  xmlns:ns1="urn:sap-com:document:sap:rfc:functions" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
 <xsl:template match="/ser-root1:createContractResponse">
  <xsl:copy-of select="*" /> 
  </xsl:template>
</xsl:stylesheet>

Good luck.

Former Member
0 Kudos

Hi Carlos

that almost works! it removes all the offending items and the non offending ones too! 🐵

how can i keep the tags nested inside??

and Anupam. thank you very much! i am working remotely today and cannot upload the code to the remote system to try this.

i am not so confident with Java.. i put the code into eclipse and created the jar file but it said there were errors....

Former Member
0 Kudos

Hi,

Can you put one xml of input, and the the result that you expect.

This script above, just copy the content between the tags <ns:BBP_ES_OA_UPDATE.Response> and </ns:BBP_ES_OA_UPDATE.Response>.

att.

anupam_ghosh2
Active Contributor
0 Kudos

Hi,

I am not sure of your PI version. check out this article on java mapping for PI 7.0. For PI 7.1 only external jar files will vary else the steps are same.

http://wiki.sdn.sap.com/wiki/display/XI/BeginnersguidetoJavamappingusingDOMparserinSAPXI

The extrenal jar files you need can be found within PI server. You need to add them as external jar file. Steps are mentioned in link above. you can drop me an email. I will forward you jar file required.

Regards

Anupam