I need my BizTalk map to stop converting xml:lang

2019-08-31 14:20发布

问题:

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?

回答1:

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.

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.

  • Part 1
  • Part 2
  • Part 3
  • Part 4
  • Part 5


回答2:

The easier way to do it and have everything work is to just add the namespace declaration at the beginning of the schema definition like this.

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xml="http://www.w3.org/XML/1998/namespace">
  <xs:import schemaLocation="xml.xsd" namespace="http://www.w3.org/XML/1998/namespace" />


回答3:

In addition to Maxime's suggestions, here are the other possibilities I've found:

Ignore it and hope that the vendor’s API will take it.

I don’t think this will work. When I test the map, BizTalk gives me this error:

Output validation error: Prefix 'ns1' cannot be mapped to namespace name reserved for "xml" or "xmlns".

Hello, BizTalk!? You’re the one who decided to use ns1. Don’t complain about it to me!

Use an XSL-based script functoid to force the output.

This is based on the suggestion I received on the BizTalk forums. It requires that we fudge the output schema to use a dummy attribute that gets replaced with the xml:lang attribute by the functoid.`

Add a search/replace expression

Take the orchestration that calls the map and add an expression after it that would take the XML we’re sending to the vendor and run it through a search/replace regex to fix the namespace prefixes.