Search code examples
biztalkbiztalk-2013

BizTalk map not working when namespace prefix is missing


I can't get my BizTalk map to work with messages after envelope debatching. The map seems to require a namespace prefix but the debatched message doesn't have a prefix. If I add a prefix to the root, like this <ns0:Encounter xmlns:ns0="http://hl7.org/fhir/Encounters"> then the map works correctly when I use test map in visual studio. Without the prefix I still get an output from the map but only constants are mapped into the destination schema, nothing is mapped from the source schema.

I get the following error message for every mapped element

"error btm1044: Input validation error: The 'value' attribute is not declared."

I have tried changing the value of elementFormDefault from unqualified to qualified as suggested on a separate post but still no luck.

After envelope debatching I end up with the following XML. Note that there is no namespace prefix. If I stop the Send Port and look at the debatched messages the MessageType is http://hl7.org/fhir/Encounters#Encounter .

  <?xml version="1.0" encoding="utf-8"?>
<Encounter xmlns="http://hl7.org/fhir/Encounters">
    <id value="ac34e2c2-6080-4c46-9ec5-d7340a7c4177" />
    <extension url="https://api-foo.org/documents/fhir/extensions/encounter-facility">
        <valueString value="foo" />
    </extension>
    <extension url="https://api-foo.org/documents/fhir/extensions/encounter-service">
        <valueString value="fooo" />.......

My schema looks like this

  <?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://hl7.org/fhir/Encounters" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://hl7.org/fhir/Encounters" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Encounter">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="id">
          <xs:complexType>
            <xs:attribute name="value" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
        <xs:element maxOccurs="unbounded" name="extension">
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="0" name="valueInteger">
                <xs:complexType>
                  <xs:attribute name="value" type="xs:unsignedByte" use="required" />
                </xs:complexType>
              </xs:element>
              <xs:element minOccurs="0" name="valueString">
                <xs:complexType>
                  <xs:attribute name="value" type="xs:string" use="required" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="url" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element> ............

Is there a way to get the map/schema to work with the message as is, or a way to get the debatched message to have the prefix.

I also have a custom pipeline component to change the namespace of the incoming message before debatching. I'm not sure if this could be causing the issue the code is below.

public IBaseMessage Execute(IPipelineContext pc, IBaseMessage inmsg)
    {
        if (Enabled)
        {
            try
            {
                IBaseMessagePart bodyPart = inmsg.BodyPart;

                if (bodyPart != null)
                {
                    string json;

                    using (Stream originalDataStream = bodyPart.GetOriginalDataStream())
                    {
                        if (originalDataStream != null)
                        {
                            //Read the json message
                            using (TextReader tr = new StreamReader(originalDataStream))
                            {
                                json = tr.ReadToEnd();
                            }

                            //Use FHIR-NET-API to create a FHIR resource from the json
                            Hl7.Fhir.Serialization.ResourceReader resourceReader = new Hl7.Fhir.Serialization.ResourceReader(FhirJsonParser.CreateFhirReader(json), ParserSettings.Default);

                            //switch the namespace
                            var doc = XElement.Parse(Hl7.Fhir.Serialization.FhirSerializer.SerializeToXml(resourceReader.Deserialize()));

                            XNamespace toNs = Namespace;
                            doc.DescendantsAndSelf().Attributes().Where(a => a.IsNamespaceDeclaration).Remove();
                            var ele = doc.DescendantsAndSelf();
                            foreach (var el in ele)
                                el.Name = toNs + el.Name.LocalName;

                            //Create the new BizTalk message
                            var memoryStream = new MemoryStream();
                            doc.Save(memoryStream);
                            //  memoryStream.Write(resourceXmlBytes, 0, resourceXmlBytes.Length);
                            memoryStream.Position = 0;
                            inmsg.BodyPart.Data = memoryStream;
                        }
                    }
                }

                return inmsg;
            }
            catch (Exception e)
            {
                GenericEventLogger.LogEvent(
                ExceptionSource,
                String.Format("An exception [{0}] occured in [{1}( )]. \n\nMessage: \n{2} \n\nStack Trace: \n{3}",
                                e.GetType().Name,
                                System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName,
                                e.Message,
                                e.StackTrace),
                EventLogEntryType.Error,
                999);
                throw;
            }
        }

        return inmsg;
    }

Here is the Envelope Schema

    <?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://hl7.org/fhir/Encounters" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://hl7.org/fhir/Encounters" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:annotation>
    <xs:appinfo>
      <b:schemaInfo is_envelope="yes" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" />
    </xs:appinfo>
  </xs:annotation>
  <xs:element name="Bundle">
    <xs:annotation>
      <xs:appinfo>
        <b:recordInfo body_xpath="/*[local-name()='Bundle' and namespace-uri()='http://hl7.org/fhir/Encounters']/*[local-name()='entry' and namespace-uri()='http://hl7.org/fhir/Encounters']/*[local-name()='resource' and namespace-uri()='http://hl7.org/fhir/Encounters']" />
      </xs:appinfo>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="total">
          <xs:complexType>
            <xs:attribute name="value" type="xs:unsignedByte" use="required" />
          </xs:complexType>
        </xs:element>
        <xs:element maxOccurs="unbounded" name="entry">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="resource">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="Encounter">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:any />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Solution

  • Since you're using the JSON representations of FHIR, the first question should be do you even need to use Xml Namespaces.

    When working with native JSON content in BizTalk, my recommendation is to use the Empty Namespace in all Xml processing.

    You can refer to this Wiki Article for more details: BizTalk: Simplify BizTalk Dev by Using the Empty Namespace