API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Required namespaces in output XML message

    Posted 27 days ago

    Required namespaces in output XML message

    Hello team,

    I have a question.
    I'm developing an API that receives a message in JSON format. Using a map node, I'm mapping and transforming JSON to XML (since my backend only receives the message in XML). However, the XML format expected by the backend is the following:
    <?xml version="1.0" encoding="utf-8"?>
    <message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://google.net/schema" xsi:schemaLocation="/www/services/xxx/ABC/webcontent/schemas/RQ123.xsd">
        <version>1.0</version>
        <header>
            <operationCode>123</operationCode>
            <origin>
                <country>CO</country>
            </origin>
            <target>
                <country>CO</country>
            </target>
        </header>
        <key>
            <grossAmount>
            </grossAmount>
            <documentsList>
                <document>
                </document>
                <document>
                </document>
            </documentsList>
            <serviceScript>SOAPXML123.XML</serviceScript>
        </key>
    </message>
    and The JSON structure sent by the user is the following:
    {
    "grossAmount": {
        "currency": {
            "code": "CO",
            "operationCode": "123"
        },
        "amount": 1
    },
    "maxDueDate": "2025-07-30",
    "documents": [
        "amount": 1
        ]
    }

    I need to know how I can include that namespace '<message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://google.net/schema" xsi:schemaLocation="/www/services/xxx/ABC/webcontent/schemas/RQ123.xsd">'
    in the output of the message? Because in all my tests I can't include those values ​​within the namespace since they are constants, they never change.
    I'm using an XSL node after mapping with the map node, where I'm using the following code:
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        exclude-result-prefixes="xsl xsi">

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

    <!-- Copy everything as is -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Replace any root node named "message" regardless namespace -->
    <xsl:template match="/*">
        <message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://bac.net/schema"
            xsi:schemaLocation="/www/services/bco/cus/webcontent/schemas/RQ09243.xsd">
    <xsl:apply-templates select="@* | node()"/>
    </message>
    </xsl:template>

    </xsl:stylesheet>

    But I keep getting the XML response without the expected namespace.

    Any suggestions? It would be very helpful.
    Thanks



    ------------------------------
    Jimmy Perilla
    ------------------------------


  • 2.  RE: Required namespaces in output XML message

    Posted 27 days ago

    Can you post your map config and the output xml you are currently getting? Map action allows you to define the output namespace, so you shouldn't need to run it through a second xslt action to do this.



    ------------------------------
    Brendon Stephens
    ------------------------------



  • 3.  RE: Required namespaces in output XML message

    Posted 27 days ago

    In the map node I have tried the following ways:
    - Extracting the value: xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance" xmlns="http://google.net/schema" xsi:schemaLocation="/www/services/xxx/ABC/webcontent/schemas/RQ123.xs using a GatewayScript, saving it in a context variable, and then calling it in the message property of the map node $(context.ctx.newMessage); and it has not worked for me.
    - burning the value directly into the map node configuration ("http://www.w3.org/2001/XMLSchema-instance" xmlns="http://google.net/schema" xsi:schemaLocation="/www/services/xxx/ABC/webcontent/schemas/RQ123.xs"); and it doesn't work.
    - declaring a variable with the desired value, and it didn't work either.

    I've attached sample images.

    Any other suggestions?
    Thanks a lot.



    ------------------------------
    Jimmy Perilla
    ------------------------------



  • 4.  RE: Required namespaces in output XML message

    Posted 26 days ago

    The screenshots are not very useful. You'll need to post your map code and the output xml if you want feedback on it. Is the xsi:schemaLocation definition a requirement? Will the backend work without it?



    ------------------------------
    Brendon Stephens
    ------------------------------



  • 5.  RE: Required namespaces in output XML message

    Posted 26 days ago

    Okay.
    I've attached the API assembly, the XML message I'm getting with the current mapping, a screenshot of the flow, and the XML message with the structure the backend expects.
    And the answer to your question: no, the backend doesn't work without that attribute.

    Thanks so much for the help, and let me know if you need anything else.



    ------------------------------
    Jimmy Perilla
    ------------------------------

    Attachment(s)

    txt
    MessageXmlOutput.txt   1 KB 1 version
    txt
    Assembly.txt   17 KB 1 version


  • 6.  RE: Required namespaces in output XML message

    Posted 26 days ago
    Edited by Brendon Stephens 26 days ago

    It looks like you are pretty close, but you did not define the namespaces correctly when building the output schema.

    Here is how you can define a namespaced element, and also a namespaced attribute (which is what xsi:schemaLocation is)

    outputs:
      message:
        schema:
          $schema: http://json-schema.org/draft-04/schema#
          type: object
          properties:
            message:
              type: object
              xml:
                prefix: msg # optional prefix
                namespace: http://bac.net/schema
              properties:
                schemaLocation:
                  type: string
                  xml:
                    attribute: true
                    prefix: xsi
                    namespace: http://www.w3.org/2001/XMLSchema-instance
                    name: "@schemaLocation"

    Then later on in your actions config you can define the default value for your attribute.

    actions:
      - set: message.message.@schemaLocation
        default: /my/dev/schema

    I'm not convinced you will need the xsi:schemaLocation - you could probably omit that.



    ------------------------------
    Brendon Stephens
    ------------------------------