cancel
Showing results for 
Search instead for 
Did you mean: 

Use Xpath filter into groovy script

0 Kudos

Hello,

Could you please tell me if it is possible to add an Xpath filter into a groovy script ?

Below is my sample in xml, I woul like to apply this filter : "//root/row[reason='ORDER_PREPARED']", then add root tags because they disappear when I apply a si,gle filter into sap cpi. My idea is to get the body into groovy script function, then apply a filter (//root/row[reason/text()='ORDER_PREPARED']) and finally add "<root>""</root>" tags.

<root>

<row>

<period_begin>2021-10-27 07:39:45.090462+00</period_begin>

<period_end/>

<line_id>6</line_id>

<reason>DLC_LIMIT_DAYS_PASSED</reason>

<product_sku>fromagers-halloumi</product_sku>

<sap_ship_to_id/>

<sap_ship_from_id>par-roty</sap_ship_from_id>

<total_stock>18</total_stock>

<sap_idn_id/>

<sap_idn_item_id/>

<sap_po_id/>

<sap_po_item_id/>

<quantity>2</quantity>

<channel>CAJOO</channel>

</row>

<row>

<period_begin>2021-10-27 07:39:45.090462+00</period_begin>

<period_end/>

<line_id>7</line_id>

<reason>DLC_LIMIT_DAYS_PASSED</reason>

<product_sku>pasquier-brioche-tressee-chocolat-630</product_sku>

<sap_ship_to_id/>

<sap_ship_from_id>par-sedaine</sap_ship_from_id>

<total_stock>8</total_stock>

<sap_idn_id/>

<sap_idn_item_id/>

<sap_po_id/>

<sap_po_item_id/>

<quantity>6</quantity>

<channel>CAJOO</channel>

</row>

</root>

Thanks for your help

MortenWittrock
Active Contributor
0 Kudos

Hi Pascal

So you want to remove all rows with the text ORDER_PREPARED? Ideally show both some sample input and expected output, and please use the CODE button when doing so. That way the XML gets formatted nicely.

Regards,

Morten

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor
0 Kudos

Hi Pascal

You can do this easily in XSLT, using the technique I've described in this blog post. In your case, the stylesheet would look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:mode on-no-match="shallow-copy" />
<xsl:output method="xml" indent="yes" />
<xsl:template match="row[reason != 'ORDER_PREPARED']" />
</xsl:stylesheet>

With this, you can turn your Filter step and Content Modifier into a single XSLT mapping step.

Regards,

Morten

Answers (2)

Answers (2)

0 Kudos

Thank you very much for your answer but I would like this structure after the xsl if it is possible :

Output

<?xml version="1.0" encoding="UTF-8"?>	
<root>
<row>
			<period_begin>2021-10-27 07:39:45.090462+00</period_begin>
			<period_end/>
			<line_id>1</line_id>
			<reason>ORDER_PREPARED</reason>
			<product_sku>biof-pdm-complet-350</product_sku>
			<sap_ship_to_id/>
			<sap_ship_from_id>par-roty</sap_ship_from_id>
			<total_stock>8</total_stock>
			<sap_idn_id/>
			<sap_idn_item_id/>
			<sap_po_id/>
			<sap_po_item_id/>
			<quantity>5</quantity>
			<channel>CAJOO</channel>
		</row>
</root>
MortenWittrock
Active Contributor
0 Kudos

Hi Pascal

I don't quite understand that. What is the difference? Also, the XML you are showing here does not have the <root> element, which you mentioned that you added in a Content Modifier.

Regards,

Morten

P.S. Please use comments on answers instead of providing new answers. Right now you've answered your own question twice, which probably wasn't your intention 😄

MortenWittrock
Active Contributor
0 Kudos

Pascal, please explain further what the difference is. This output you are showing contains an SKU ("biof-pdm-complet-350"), which is not in any of the input you've shown here.

Ideally, update the question itself with the input and the expected output. Remember to use the CODE button to embed it.

Regards,

Morten

0 Kudos

I would like to have in my output all the row node with chils nodes inside where "reason" = "ORDER_PREPARED" but inside a root tag, because on CPI when I use this filter : //root/row[reason/text()='ORDER_PREPARED'], my root tag disappear.

I would like to do this filter action whithout remove root tags, may be with a groovy script or an xsl.

Input           <?xml version="1.0" encoding="UTF-8"?>
<root>
	<row>
		<period_begin>2021-10-27 07:39:45.090462+00</period_begin>
		<period_end/>
		<line_id>6</line_id>
		<reason>DLC_LIMIT_DAYS_PASSED</reason>
		<product_sku>fromagers-halloumi</product_sku>
		<sap_ship_to_id/>
		<sap_ship_from_id>par-roty</sap_ship_from_id>
		<total_stock>18</total_stock>
		<sap_idn_id/>
		<sap_idn_item_id/>
		<sap_po_id/>
		<sap_po_item_id/>
		<quantity>2</quantity>
		<channel>CAJOO</channel>
	</row>
	<row>
		<period_begin>2021-10-27 07:39:45.090462+00</period_begin>
		<period_end/>
		<line_id>7</line_id>
		<reason>ORDER_PREPARED</reason>
		<product_sku>pasquier-brioche-tressee-chocolat-630</product_sku>
		<sap_ship_to_id/>
		<sap_ship_from_id>par-sedaine</sap_ship_from_id>
		<total_stock>8</total_stock>
		<sap_idn_id/>
		<sap_idn_item_id/>
		<sap_po_id/>
		<sap_po_item_id/>
		<quantity>6</quantity>
		<channel>CAJOO</channel>
	</row>
		<row>
		<period_begin>2021-10-27 07:39:45.090462+00</period_begin>
		<period_end/>
		<line_id>7</line_id>
		<reason>ORDER_PREPARED</reason>
		<product_sku>pasquier-brioche-tressee-chocolat-630</product_sku>
		<sap_ship_to_id/>
		<sap_ship_from_id>par-sedaine</sap_ship_from_id>
		<total_stock>8</total_stock>
		<sap_idn_id/>
		<sap_idn_item_id/>
		<sap_po_id/>
		<sap_po_item_id/>
		<quantity>6</quantity>
		<channel>CAJOO</channel>
	</row>
	</root>
Output        <?xml version="1.0" encoding="UTF-8"?>
<root>
	<row>
		<period_begin>2021-10-27 07:39:45.090462+00</period_begin>
		<period_end/>
		<line_id>7</line_id>
		<reason>ORDER_PREPARED</reason>
		<product_sku>pasquier-brioche-tressee-chocolat-630</product_sku>
		<sap_ship_to_id/>
		<sap_ship_from_id>par-sedaine</sap_ship_from_id>
		<total_stock>8</total_stock>
		<sap_idn_id/>
		<sap_idn_item_id/>
		<sap_po_id/>
		<sap_po_item_id/>
		<quantity>6</quantity>
		<channel>CAJOO</channel>
	</row>
		<row>
		<period_begin>2021-10-27 07:39:45.090462+00</period_begin>
		<period_end/>
		<line_id>7</line_id>
		<reason>ORDER_PREPARED</reason>
		<product_sku>pasquier-brioche-tressee-chocolat-630</product_sku>
		<sap_ship_to_id/>
		<sap_ship_from_id>par-sedaine</sap_ship_from_id>
		<total_stock>8</total_stock>
		<sap_idn_id/>
		<sap_idn_item_id/>
		<sap_po_id/>
		<sap_po_item_id/>
		<quantity>6</quantity>
		<channel>CAJOO</channel>
	</row>
	</root>
MortenWittrock
Active Contributor
0 Kudos

Hi Pascal

With the output you show here, my XSLT stylesheet creates the output you describe.

Regards,

Morten

0 Kudos

Thank for your help but it doesn't work I got this :

<?xml version="1.0"?>

	
		2021-10-27 07:39:45.090462+00
		
		6
		DLC_LIMIT_DAYS_PASSED
		fromagers-halloumi
		
		par-roty
		18
		
		
		
		
		2
		CAJOO
MortenWittrock
Active Contributor
0 Kudos

Hi

I've tried it in CPI with your input already, and it gives the correct output.

Regards,

Morten

0 Kudos

Yes It works in CPI, I tried before into Notepad with an extension module the result was different, but for my needs it is ok.

Thanks for your help

MortenWittrock
Active Contributor
0 Kudos

No problem. If you'd accept the answer as well, that'd be great.

Regards,

Morten

0 Kudos

Hello Morten,

Thanks for your answer.

I would like to get all the "row" nodes with the child node "sap_ship_from_id" equals "ORDER_PREPARED". I can use the filter function into CPI but I will remove root tags so I need to put a "Content modifier" after just to add new root tags.

My idea is to do it into a groovy script below

- Get the body

- Appply the Xpath filter //root/row[reason='ORDER_PREPARED']

- get the new body then add root tags "<root> + body + "</root>",

<root>

<row>

<period_begin>2021-10-27 07:39:45.090462+00</period_begin>

<period_end/>

<line_id>6</line_id>

<reason>ORDERED_PREPARED</reason>

<product_sku>fromagers-halloumi</product_sku>

<sap_ship_to_id/>

<sap_ship_from_id>par-roty</sap_ship_from_id>

<total_stock>18</total_stock>

<sap_idn_id/>

<sap_idn_item_id/>

<sap_po_id/>

<sap_po_item_id/>

<quantity>2</quantity>

<channel>CAJOO</channel>

</row>

<row>

<period_begin>2021-10-27 07:39:45.090462+00</period_begin>

<period_end/>

<line_id>7</line_id>

<reason>DLC_LIMIT_DAYS_PASSED</reason>

<product_sku>pasquier-brioche-tressee-chocolat-630</product_sku>

<sap_ship_to_id/>

<sap_ship_from_id>par-sedaine</sap_ship_from_id>

<total_stock>8</total_stock>

<sap_idn_id/>

<sap_idn_item_id/>

<sap_po_id/>

<sap_po_item_id/>

<quantity>6</quantity>

<channel>CAJOO</channel>

</row>

</root>