Search code examples
xsdbiztalkbiztalk-2009

I need my BizTalk map to stop converting xml:lang to ns1:lang


I have a map in BizTalk 2009 that is converting some data into an XML document to be sent on to another system. The target schema includes some elements with xml:lang attributes. BizTalk generates those as ns1:lang. The target system requires that the prefix xml be used.

Here is a simplified example to show what BizTalk is doing:

sample.xsd

<xs:schema targetNamespace="http://example.com/"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation="common.xsd"
             namespace="http://www.w3.org/XML/1998/namespace" />
  <xs:element name="example">
    <xs:complexType>
      <xs:attribute ref="xml:lang" />
    </xs:complexType>
  </xs:element>
</xs:schema>

common.xsd

<xs:schema xmlns:xml="http://www.w3.org/XML/1998/namespace"
           targetNamespace="http://www.w3.org/XML/1998/namespace"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:attribute name="lang" type="xs:language" />
</xs:schema>

Example of map output

<ns0:example xmlns:ns0="http://example.com/"
             xmlns:ns1="http://www.w3.org/XML/1998/namespace"
             ns1:lang="en-US" />

Is there some way to convince BizTalk to use the xml prefix?


Solution

  • As far as I know, there is no builtin way for achieving this.

    There are, however, two solutions that I can see:

    Use a Custom XML StyleSheet

    If you right-clik on the map and look carefully in the generated xsl stylesheet, you'll see a XML namespace declaration like this:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:ns1="http://www.w3.org/XML/1998/namespace"
                    ...
                    >
    ...
    <xsl:attribute name="ns1:lang">
    ...
    

    This is the default behaviour of the BizTalk mapper and you cannot do anything about it. However, if you proceed to extract the generated XSLT and use this as the backend for your map, you can change this declaration to match the expected outcome.

    • First, copy the stylesheet to the location of your project.
    • Include this stylesheet as a file in your BizTalk project
    • Update the stylesheet, so that the namespace declaration and the matching attribute prefix are correct.

    The resulting xsl stylesheet looks like this:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:xml="http://www.w3.org/XML/1998/namespace"
                    ...
                    >
    ...
    <xsl:attribute name="xml:lang">
    ...
    

    Now you can use this custom stylesheet as a backend for the map.

    • In Visual Studio, open the map.
    • Click anywhere on a blank space in the BizTalk designer surface.
    • In the map properties, locate the Custom XSL Path and specify the location of your custom stylesheet.

    Custom BizTalk Mapper XSL Path

    Use a Custom Pipeline Component

    What you're after is that the message is correct for your target recipient. So the idea is to change the offending namespace prefix as part of sending the message outside BizTalk. The transformation happens during the processing of the send pipeline.

    Nic Barden has blogged and provided some source code about this here. You can use his sample as the basis for performing replacement of namespace prefixes, rather than replacing namespaces themselves.

    I strongly encourage you to check out the whole series of posts he's done about Developing Streaming Pipeline Components. Nic has made an extensive and thorough job of describing all that's needed to author robust and enterprise-class pipeline components.