Skip to Content
0

XSLT in SAP: namespace issue

Jul 20, 2017 at 01:49 PM

68

avatar image

Dear experts!

I'm new to XSLT.

I'm trying to create a transformation which would remove a certain tag from an XML file. The tag I'm trying to remove has a namespace declaration. The idea is to get out of the following input:

<code><?xml version="1.0" encoding="utf-8"?><br><ToBeRemovedxlmns:prx="urn:something"><br><n0:ToRemainxlmns:n0="https://somethingelse.com/def.xsd"><br><Data>
...
</Data><br></n0:ToRemain><br></ToBeRemoved>

the following:

<code><?xml version="1.0" encoding="utf-8"?><br><n0:ToRemainxlmns:n0="https://somethingelse.com/def.xsd"><Data>
...
</Data><br></n0:ToRemain>

I'm using the following XSLT:

<code><xsl:transformversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><br><xsl:strip-spaceelements="*"/><br><xsl:templatematch="node()|@*"><br>
<xsl:copy><br><xsl:apply-templatesselect="node()|@*"/><br></xsl:copy>

<br></xsl:template><br><xsl:templatematch="ToBeRemoved"><br><xsl:apply-templatesselect="node()|@*"/><br></xsl:template><br>

</xsl:transform>

Unfortunately, the namespace declaration from doesn't get completely deleted, but "sticks" somehow to the next tag :

<code><?xml version="1.0" encoding="utf-8"?><br><n0:ToRemainxlmns:n0="https://somethingelse.com/def.xsd"<br>xlmns:prx="urn:something">

<br><Data>
...
</Data><br></n0:ToRemain>

Any ideas?

Thanks!!!

Alexander.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

avatar image
Former Member Jul 21, 2017 at 01:51 PM
0

Hi Alexander,

this is because of the declaration of the namespace of <ToBeRemoved>. <ToRemain> inherits the namespace of <ToBeRemoved>, as <ToBeRemoved> has no namespace prefix. So, <ToRemain> has had the namespace before, but inherited and not declared. This thread explains this behaviour quite good from another perspective:

https://stackoverflow.com/questions/25788871/xml-do-child-nodes-inherit-parents-namespace-prefix

So the output XML is right by definition. I hope this helps.

Kind regards

Share
10 |10000 characters needed characters left characters exceeded
Evgeniy Kolmakov Mar 05 at 12:57 PM
0

Hi Alexander!

In this case you should use recursive template to create new elements with same names and data instead of copying the original elements.

As Moritz has already mentioned when you copy element from source tree - you copy it together with namespace info which is inherited from parent element.

Regrads, Evgeniy.

Show 1 Share
10 |10000 characters needed characters left characters exceeded

One more thought: if your XSL transformation is intended just to remove unwanted namespace from XML document - maybe you could give XMLAnonymizerBean a try?

Regards, Evgeniy.

0