Skip to Content

Remove nodes without child fields in SAP HCI

<Root>
<Emp>
<PersonId>10001</PersonId>
<Child/>
<Child>
<F1>JOE DOE</F1>
<F2>SGP</F2>
<F3>2010-01-06</F3>
</Child>
<Child/>
<Child>
<F1>Peter Parker</F1>
<F2>AFG</F2>
<F3>2012-05-30</F3>
</Child>
<Child>
<F1>Chris Tiu</F1>
<F2>SGP</F2>
<F3>2012-01-26</F3>
</Child>
</Emp>

<Root>

Id like to remove all <Child> nodes without values

Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Best Answer
    Posted on Jan 22, 2019 at 04:38 AM

    Hello Aenan,

    Add a XSLT mapping with below code then it should work.

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="xml" encoding="Windows-1252" indent="yes"/>
    	<xsl:template match="@*|node()[./node()]">
    		<xsl:copy>
    			<xsl:apply-templates select="@*|node()"/>
    		</xsl:copy>
    	</xsl:template>
    </xsl:stylesheet>
    

    Regards,

    Sriprasad Shivaram Bhat

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 08, 2019 at 02:48 PM

    Or may be you could you the below script as well **this could be the complicated approach may be**

    import groovy.xml.MarkupBuilder
    class sortXML {
    static main(args) {
    
    String body = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><Root><Emp><PersonId>10001</PersonId><Child><F1>JOE DOE</F1><F2>SGP</F2><F3>2010-01-06</F3><F4>10001</F4></Child><Child><F1>Chris Tiu</F1><F2>SGP</F2><F3>2012-01-26</F3><F4>10001</F4></Child></Emp><Emp><PersonId>10653</PersonId><Child><F1>Test Child</F1><F2>SGP</F2><F3>2008-11-29</F3><F4>10653</F4></Child><Child><F1>jane doe</F1><F2>SGP</F2><F3>1994-01-05</F3><F4>10653</F4></Child></Emp></Root>";
            def parseXML = new XmlParser().parseText(body);
    def varStringWriter = new StringWriter();
    def varXMLBuilder   = new MarkupBuilder(varStringWriter);
    
    int empNodes = 0 ;
    int empChildNodes = 0 ;
    int childPicker = 0 ;
    
    String empNumber ;
    String empF1 ;
    String empF2;
    String empF3;
    String empF4;
    
    String nodeBody ;
    
    empNodes = parseXML.Emp.size();
    for(int empItr = 0 ; empItr < empNodes ; empItr++)
    {
    childPicker = 0 ;
    empNumber     = "${parseXML.Emp[empItr].PersonId.text().toString()}"
    empChildNodes = parseXML.Emp[empItr].Child.size();
    for(int childItr = 0 ; childItr < empChildNodes - 1 ; childItr ++)
    {
    String empF3Temp ;
    empF3     = "${parseXML.Emp[empItr].Child[childItr].F3.text().toString()}"
    empF3Temp = "${parseXML.Emp[empItr].Child[childItr+1].F3.text().toString()}"
    
    if(empF3 < empF3Temp)
    {
    childPicker = childItr+1;
    }
    }
    
    empF1 = "${parseXML.Emp[empItr].Child[childPicker].F1.text().toString()}"
    empF2 = "${parseXML.Emp[empItr].Child[childPicker].F2.text().toString()}"
    empF3 = "${parseXML.Emp[empItr].Child[childPicker].F3.text().toString()}"
    empF4 = "${parseXML.Emp[empItr].Child[childPicker].F4.text().toString()}"
    
    varXMLBuilder.Emp{
    PersonId(empNumber)
    F1(empF1);
    F2(empF2);
    F3(empF3);
    F4(empF4);
    }
    
    def xml = varStringWriter.toString();
    nodeBody = xml ;
    }
    
    nodeBody='<Root>'+nodeBody+'</Root>';
    println nodeBody ;
    
    }
    }
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.