Ramjeet
Ramjeet

Reputation: 33

How to add namespace and xsi to the Incoming XML with no namespace

I have requirement where I have to add Namespace and xsi to the element from the source xml with No Namespace. In Source XML I am just getting the Nodes and there is No namespace and another program needs BizTalk to add Namespace and XSI to the XML for its processing.

I tried:

  1. Used add namespace pipeline component. (It just added namespace and not the xsi bits)
  2. Used Map for putting up the desired format and yes no luck as got just the namespace.

Need your help around this.

My source XML is like

<?xml version="1.0" encoding="UTF-16"?> 
<Document>
    <CstmrPmtStsRpt>
        <GrpHdr>
            <MsgId></MsgId>
            <CreDtTm></CreDtTm>
            <InitgPty>                 
                <Id>
                    <OrgId>
                        <BICOrBEI></BICOrBEI>
                    </OrgId>
                </Id>
            </InitgPty>
        </GrpHdr>
        <OrgnlGrpInfAndSts>
             <OrgnlMsgId></OrgnlMsgId>
            <OrgnlMsgNmId></OrgnlMsgNmId>
            <OrgnlNbOfTxs></OrgnlNbOfTxs>
            <OrgnlCtrlSum></OrgnlCtrlSum>
            <GrpSts>ACCP</GrpSts>
        </OrgnlGrpInfAndSts>
    </CstmrPmtStsRpt>
</Document>

My Required format is as below:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="MyNamespace">     
    <CstmrPmtStsRpt>
        <GrpHdr>
            <MsgId></MsgId>
            <CreDtTm></CreDtTm>
            <InitgPty>                 
                <Id>
                    <OrgId>
                        <BICOrBEI></BICOrBEI>
                    </OrgId>
                </Id>
            </InitgPty>
        </GrpHdr>
        <OrgnlGrpInfAndSts>
            <OrgnlMsgId></OrgnlMsgId>
            <OrgnlMsgNmId></OrgnlMsgNmId>
            <OrgnlNbOfTxs></OrgnlNbOfTxs>
            <OrgnlCtrlSum></OrgnlCtrlSum>
            <GrpSts>ACCP</GrpSts>
        </OrgnlGrpInfAndSts>
    </CstmrPmtStsRpt>
</Document>

Upvotes: 1

Views: 1454

Answers (2)

DTRT
DTRT

Reputation: 11040

BizTalk Answer:

First, it's a good thing the incoming document has no namespace. Xml Namespaces are far, far, far more trouble than they're worth and should be avoided/removed whenever possible.

Second the output format is not valid Xml. "MyNamespace" is not a valid URI and can't be used for a Namespace. If this is what they are asking for, they need to fix that first.

But, if you must, your process should not be "add a namespace". What you're really doing is Transforming from SysA's Document to SysB's Document. For that, use a Map. You will use to practially identical Schemas, one with and one without the Target Namespace.

The Mapper will handle xsi for you as well, if it's needed.

Upvotes: 1

FelHa
FelHa

Reputation: 1103

Use the namespace attribute of xsl:element like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

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

    <xsl:template match="*">
        <xsl:element name="{local-name()}" namespace="MyNamespace">
            <xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Edit: Since you need to work with XSLT-1.0. Use following stylesheet:

<?xml version="1.0" encoding="UTF-16"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template match="/Document">
        <Document xmlns="MyNamespace" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <xsl:apply-templates/>           
        </Document>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name()}" namespace="MyNamespace">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Note, that you need to know your rootnode's name for this (in this case Document).

Upvotes: 4

Related Questions