cancel
Showing results for 
Search instead for 
Did you mean: 

[SAP CPI]: Remove Namespace in XSLT

babruvahana
Contributor
0 Kudos

Hi Experts,

I have the below input XML from the Source system. But they are sending with a namespace, so I would like to remove it.

I know that I can use the filter/groovy step before XSLT mapping to remove the namespace. But, I would like to use XSLT to remove the namespace to avoid unnecessary message steps.

Source XML:

<GoodsReceiptHeader xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.abc.com/defghij/2011/11">
	<TimeOfDelivery>2023-01-06T10:19:00</TimeOfDelivery>
	<ReceiptId>543221</ReceiptId>
	<GoodsReceiptUnits>
		<GoodsReceiptUnit>
			<Barcode>12334567890</Barcode>
			<Status>A</Status>
		</GoodsReceiptUnit>
		<GoodsReceiptUnit>
			<Barcode>0987654321</Barcode>
			<Status>A</Status>
		</GoodsReceiptUnit>
	</GoodsReceiptUnits>
</GoodsReceiptHeader><br>

XSLT:

<strong><?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cpi="http://sap.com/it/" exclude-result-prefixes="cpi" version="3.0">
    <xsl:param name="RCVPRN"/>
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:strip-space elements="*"/>
	<xsl:mode on-no-match="shallow-copy"/>
	<xsl:template match="/GoodsReceiptHeader">
		<ZWPUWBW01>
			<IDOC>
				*
				*
				*
				*
				*
			</IDOC>
		</ZWPUWBW01>
	</xsl:template>
</xsl:stylesheet></strong><b><br></b>

Any leads will be really helpful.

Regards,

Pavan

Accepted Solutions (0)

Answers (1)

Answers (1)

RobertQ
Explorer

Hello Pavan.

You can remove the namespaces you define in your stylesheet by setting exclude-result-prefixes="#all". The namespaces from your source will not be in your target xml, as you do not copy any nodes that use them. To select a value from your source you have to adjust your xslt to use the corresponding namespace from your source file.

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cpi="http://sap.com/it/" xmlns:n1="http://schemas.abc.com/defghij/2011/11" exclude-result-prefixes="#all" version="3.0">
    <xsl:param name="RCVPRN"/>
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:strip-space elements="*"/>
	<xsl:mode on-no-match="shallow-copy"/>
	<xsl:template match="/n1:GoodsReceiptHeader">
		<ZWPUWBW01>
			<IDOC>
			   <xyz>
			     <xsl:value-of select="n1:ReceiptId"/>
			   </xyz>
			</IDOC>
		</ZWPUWBW01>
	</xsl:template>
</xsl:stylesheet>

Result:

<?xml version="1.0" encoding="UTF-8"?>
<ZWPUWBW01>
   <IDOC>
      <xyz>543221</xyz>
   </IDOC>
</ZWPUWBW01>
Alertativly you can use a second XSLT script prior to the first where you delete all namespaces from the source file. In this case you do not need to adhere to the namespace in your xslt. Example from stackoverflow. But I would suggest the first methode as you only need one xslt script.Best regardsRobert
babruvahana
Contributor
0 Kudos

Hi rquindt,

Thanks a lot for the help. Your solution works fine.

I want to know if there is any way we can select the values from the Source XML by ignoring them or replacing them with empty values. Just like how we do it in groovy script.

Regards,

Pavan

RobertQ
Explorer
0 Kudos

Hello babruvahana,

you mean, to select a node without declaring/ knowing the namespace of it? If yes, you can use the local-name() function.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="#all" version="3.0">
    <xsl:param name="RCVPRN"/>
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:strip-space elements="*"/>
	<xsl:mode on-no-match="shallow-copy"/>
	<xsl:template match="/">
		<ZWPUWBW01>
			<IDOC>
			   <xyz>
			     <xsl:value-of select="//*[local-name()='GoodsReceiptHeader']/*[local-name()='ReceiptId']"/>
			   </xyz>
			</IDOC>
		</ZWPUWBW01>
	</xsl:template>
</xsl:stylesheet>

Best Regards

Robert