cancel
Showing results for 
Search instead for 
Did you mean: 

Problem when using XSLT conversion program

Former Member
0 Kudos

Dears,

I have an outbound interface that required field mapping without any namespace prefixes at the beginning of each node. So I have implemented an XSLT program and use it as a mapping program in the operation mapping and it worked fine as it has removed all the prefixes. BUT my problem is, the first Document node header should contain the namespace after the <node> as a value.

this is the XSLT program code I've used to remove the prefixes:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="*">
    <xsl:element name="{local-name(.)}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*">
    <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

my XML msg looks like this:

<?xml version="1.0" encoding="utf-8" ?>
- -<n0:Document xmlns:n0="urn:xxxxxxxxxxxxxxxxxx" xmlns:prx="urn:sap.com:proxy:DR1xxxxxxxxxxxxxxxxxxxxxxxxx">
-   -<n0:subelement1>
  - <n0:subelement2>1234567</n0:subelement2>

</n0:subelement1>

</n0:Document>

After using the XSLT mapping program:

<?xml version="1.0" encoding="UTF-8" ?>

- -<Document>
- <subelement1>
- <subelement2>1234567</subelement2>

</subelement1>

<Document>

So, even the namespace value after Document is removed. I only want the name space (n0:) to be removed and keep the other namespace.

Can someone help me where can I modify the code to match want I need?!

Thank you,

Accepted Solutions (1)

Accepted Solutions (1)

iaki_vila
Active Contributor
0 Kudos

Hi Eman,

May be this is not an elegant way, but you can delete all and add the other namespace:

Source:


<?xml version="1.0" encoding="utf-8"?>

<n0:Document xmlns:n0="urn:xxxxxxxxxxxxxxxxxx" xmlns:prx="urn:sap.com:proxy:DR1xxxxxxxxxxxxxxxxxxxxxxxxx">

    <n0:subelement1>

        <n0:subelement2>1234567</n0:subelement2>

    </n0:subelement1>

</n0:Document>

XSL:


<?xml version="1.0" encoding="utf-8"?>

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

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="*">

        <xsl:element name="{local-name(.)}">

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

        </xsl:element>

    </xsl:template>

    <xsl:template match="@*">

        <xsl:attribute name="{local-name(.)}"><xsl:value-of select="."/></xsl:attribute>

    </xsl:template>

    <xsl:template match="/*">

        <xsl:element name="ns:{local-name()}" namespace="urn:sap.com:proxy:DR1xxxxxxxxxxxxxxxxxxxxxxxxx">

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

        </xsl:element>

    </xsl:template>

</xsl:stylesheet>

Target:


<?xml version="1.0" encoding="UTF-8"?>

<ns:Document xmlns:ns="urn:sap.com:proxy:DR1xxxxxxxxxxxxxxxxxxxxxxxxx">

    <subelement1>

        <subelement2>1234567</subelement2>

    </subelement1>

</ns:Document>

Regards.

Former Member
0 Kudos

Hi Eman

I agree with Inaki. There is no other way to do this in XSLT.

These will be necessary code


<?xml version="1.0" encoding="utf-8"?>

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

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="*">

        <xsl:element name="{local-name(.)}">

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

        </xsl:element>

    </xsl:template>

   

    <xsl:template match="/*">

        <xsl:element name="ns:{local-name()}" namespace="urn:sap.com:proxy:DR1xxxxxxxxxxxxxxxxxxxxxxxxx">

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

        </xsl:element>

    </xsl:template>

</xsl:stylesheet>

However you can do the same in java mapping as well.

Thanks,

Indrajit

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Eman

Xml Anonymizer bean worked perfectly for your scenario.

Module name :  AF_Modules/XMLAnonymizerBean

Module Key : 1

Parameter name :  anonymizer.acceptNamespaces

Value :  urn:sap.com:proxy:DR1xxxxxxxxxxxxxxxxxxxxxxxxx prx

output xml will be like below


<?xml version='1.0' encoding='utf-8'?>

<Document xmlns:prx='urn:sap.com:proxy:DR1xxxxxxxxxxxxxxxxxxxxxxxxx'>

<subelement1>

<subelement2>1234567</subelement2>

</subelement1>

</Document>

engswee
Active Contributor
0 Kudos

Hi Eman

Have you tried using the standard XMLAnonymizerBean module? No extra custom development needed if it fits your needs.

It allows you to specify the namespaces you want to keep (prx) in the following parameter, anything else not specified will be removed (ns0)

anonymizer.acceptNamespaces

There are a few SCN blogs for more details on the usage and example. Here are a few of them:

Rgds

Eng Swee