cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to Change the Namespace Prefix with XSLT Mapping as Well as XML AnonymizerBean

0 Kudos
2,263

Hello All,

I am facing issue to change the namespace prefixes of a xml file to produce the required output. I have used XMLAnonymizer bean module at the file receiver channel and in the message log I see " Successfully Anonymized" but no change in the delivered message. I tried using the below xslt code in the OM to change the namespace prefix as below. I see the online tools are successfully converting but in the ESR I get the below error.

  • javax.xml.transform.TransformerException: java.lang.RuntimeException: Namespace for prefix 'ubl' has not been declared.

ns0->ubl
ns1->ext
ns2->cbc
ns4->wknl
ns5->cac


XSLT used:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:ns3="order2cash:i2d:ubl:ext:PdfGeneration"
xmlns:ns4="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration"
xmlns:ns5="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:ubl="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:wknl="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="ns0 ns1 ns2 ns4 ns5">

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns0:*">
<xsl:element name="ubl:{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="ns1:*">
<xsl:element name="ext:{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="ns2:*">
<xsl:element name="cbc:{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="ns4:*">
<xsl:element name="wknl:{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="ns5:*">
<xsl:element name="cac:{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>

</xsl:template>
</xsl:stylesheet>

Parameters used in the Anonymizer Bean:

anonymizer.acceptNamespaces

urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ubl
urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2 ext
urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2 cbc
urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2 cac
order2cash:i2d:ubl:ext:PdfGeneration ''
order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration wknl

anonymizer.quote '

Kindly help me out to resolve this requirement.


Regards,

Indumathi K

Accepted Solutions (1)

Accepted Solutions (1)

former_member190293
Active Contributor
0 Kudos

Hi!

Take a look at XSL transformation code I've provided in this thread:

https://answers.sap.com/questions/13005115/replace-namespace-prefix-in-xml-using-xslt-mapping.html?c...

Regards, Evgeniy.

0 Kudos

Hi Evgeniy,

Thank you so much.I am successfully able to replace the prefixes..But the namespace comes for every node and I had one more xslt to remove them but for The node"Extension Content" the expected output is like below , they want couple of namespaces in the extension content node and prefixes to be removed for the first two elements.. Could you please let me know if I can remove one prefix from only a couple of nodes.

<?xml version="1.0" encoding="utf-8"?>
<ubl:Invoice xmlns:ubl="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:UBLExtensions>
<ext:UBLExtension>
<cbc:ID>PDF Generation</cbc:ID>
<ext:ExtensionVersionID>1.0</ext:ExtensionVersionID>
<ext:ExtensionContent xmlns="order2cash:i2d:ubl:ext:PdfGeneration" xmlns:wknl="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration">
<PdfGeneration>
<PdfLayoutCode>NL1</PdfLayoutCode>
<KixCode>3012LE59</KixCode>

<wknl:InvoiceStream>REGULIER</wknl:InvoiceStream>
<wknl:BillType>GROTE KLANT</wknl:BillType>
<wknl:SpecialInvoiceTreatment>REGULIER</wknl:SpecialInvoiceTreatment>
<wknl:DealId>41075610 | periode 01 juli 2019 t/m 26 november 2019 - ref. factuur: 1870145474</wknl:DealId>
</PdfGeneration>
</ext:ExtensionContent>
</ext:UBLExtension>

Regards,

Indu

0 Kudos

Hi Evegeniy,

And also when I tried changing all the prefixes some confusion happens in the ExtensionContentPart ns3: Namespace.

Input:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Invoice xmlns:ns0="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<ns1:UBLExtensions xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ns1:UBLExtension><ns2:ID xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">PDF Generation</ns2:ID>
<ns1:ExtensionVersionID>1.0</ns1:ExtensionVersionID>
<ns1:ExtensionContent>
<ns3:PdfGeneration xmlns:ns3="order2cash:i2d:ubl:ext:PdfGeneration">
<ns3:PdfLayoutCode>NL3</ns3:PdfLayoutCode>
<ns3:KixCode>2408ZE2</ns3:KixCode>
<ns4:InvoiceStream xmlns:ns4="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration">SCHULINKOPL</ns4:InvoiceStream>
<ns4:BillType xmlns:ns4="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration">STANDAARD</ns4:BillType>
<ns4:SpecialInvoiceTreatment
xmlns:ns4="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration">REGULIER</ns4:SpecialInvoiceTreatment>
<ns4:DealId xmlns:ns4="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration">12345</ns4:DealId>
</ns3:PdfGeneration>
</ns1:ExtensionContent>
</ns1:UBLExtension>
</ns1:UBLExtensions>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:ns4="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration"
xmlns:ns5="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:ubl="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:wknl="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="ns0 ns1 ns2 ns4 ns5">

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:template match="/">


<xsl:copy>

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

</xsl:copy>


</xsl:template>

<xsl:template match="ns0:*">

<xsl:element name="ubl:{local-name()}" namespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">

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

</xsl:element>

</xsl:template>
<xsl:template match="ns1:*">

<xsl:element name="ext:{local-name()}" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">

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

</xsl:element>

</xsl:template>
<xsl:template match="ns2:*">

<xsl:element name="cbc:{local-name()}" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">

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

</xsl:element>

</xsl:template>
<xsl:template match="ns4:*">

<xsl:element name="wknl:{local-name()}" namespace="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration">

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

</xsl:element>

</xsl:template>
<xsl:template match="ns5:*">

<xsl:element name="cac:{local-name()}" namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">

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

</xsl:element>

</xsl:template>
</xsl:stylesheet>

Output:

I have removed ns3 namespace with someother xslt in the previous step as I didn't want it. but that part is coming collapsed after the above xslt. Not sure what is going wrong. Any guidance would be appreciated.


<?xml version="1.0" encoding="UTF-8"?>
<ubl:Invoice xmlns:ubl="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<ext:UBLExtensions xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:UBLExtension>
<cbc:ID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">PDF Generation</cbc:ID>
<ext:ExtensionVersionID>1.0</ext:ExtensionVersionID>
<ext:ExtensionContent>NL32408ZE2<wknl:InvoiceStream xmlns:wknl="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration">SCHULINKOPL</wknl:InvoiceStream>
<wknl:BillType xmlns:wknl="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration">STANDAARD</wknl:BillType>
<wknl:SpecialInvoiceTreatment xmlns:wknl="order2cash:i2d:wolterskluwer
:ubl:ext:PdfGeneration">REGULIER</wknl:SpecialInvoiceTreatment>
<wknl:DealId xmlns:wknl="order2cash:i2d:wolterskluwer:ubl:ext:PdfGeneration">12345</wknl:DealId>
</ext:ExtensionContent>
</ext:UBLExtension>
</ext:UBLExtensions>

Answers (2)

Answers (2)

0 Kudos

prefixchangexsl.txtinput.txt

Hi @evgeniy.kolmakov,


Attached the Input XML. I have two requirements 1. To remove the namespaces from individual nodes .2 To Change the Prefix names. I used the attached the XSLT but while changing the prefix its removing the attribute name like below.

<cbc:LineExtensionAmount xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">EUR35.38</cbc:LineExtensionAmount>

But it should be like below, could you please help me on this

<ns2:LineExtensionAmount xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" currencyID="EUR">35.38</ns2:LineExtensionAmount


Regards,

Indu

former_member190293
Active Contributor

Hi!

Just add template to your XSLT to copy attribute nodes:

<xsl:template match="@*">
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
            </xsl:copy>
        </xsl:template>

Regards, Evgeniy.

0 Kudos

Hi Evgeniy,

Thank you. I have achieved my Target XML format as expected.

Regards,

Indu

0 Kudos

samplexml.txt

Hi All,

Attaching the sample output xml which is needed.

Regards

Indumathi K