Search code examples
jsonxmlxsdbiztalkbiztalk-2010

BizTalk - Putting a namespace on an attribute?


Referencing this example of using "json:Array": Converting between JSON and XML

I want BizTalk schema that will build an XML element like this:

  <role json:Array='true'>Admin</role>

I tried adding a schema to my project called FakeJSONArraySchema.xsd, and then on my main schema, I did an "imports". The normal way to use an "imports" is to create a "child record" then change it's "Data Structure Type". But sets that "child record" to the root element of the referenced schema. I just need an attribute.

In the above example, the element "role" needs to be in the namespace of the main schema.

If all else fails, I will try editing the .XSD directly. I was hoping this could be done using the Visual Studio graphical interface.

See related question: Details about the json:Array feature of Newtonsoft.JSON XML to JSON converter


Solution

  • As Sprotty has said in his comment, set the Attribute FormDefault or the Attribute Field Form to Qualified to get a namespace prefix on the attribute.

    Attribute FormDefault Attribute Field Form

    Example schema with FormDefault set.

    <?xml version="1.0" encoding="utf-16"?>
    <xs:schema xmlns="http://james.newtonking.com/projects/json" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" attributeFormDefault="qualified" targetNamespace="http://james.newtonking.com/projects/json" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="Root">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Role">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:string">
                    <xs:attribute name="Array" type="xs:boolean" />
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    However it won't make the Namespace prefix JSON, but just the default NS0. However that should hopefully not matter as long as it references the correct namespace.

    <ns0:Root xmlns:ns0="http://james.newtonking.com/projects/json">
      <Role ns0:Array="true">Role_0</Role>
    </ns0:Root>