2012-06-14 44 views
2

我有一个映射问题,我试图在BizTalk的映射工具中解决。BizTalk映射问题

考虑以下输入文件:

<person> 
    <ID>APersonID</ID> 
    <relatives> 
     <relative> 
      <name>Relative name 1</name> 
     </relative> 
     <relative> 
      <name>Relative name 2</name> 
     </relative> 
    </relatives> 
</person> 

注:的minOccurs相对元件的被设置为0 maxOccurs的相对元素被设置为无限

该输入应该被映射到下面的输出:

<relatives> 
    <person> 
     <ID>APersonID</ID> 
     <relative>Relative name 1</relative> 
    </person> 
    <person> 
     <ID>APersonID</ID> 
     <relative>Relative name 2</relative> 
    </person> 
<relatives> 

注:人元件具有的minOccurs的的maxOccurs的无界

我已经有了一个映射,可以将输入文件的相对元素链接到输出文件中的person元素的循环functoid中。但是现在有一种情况是我给出了以下输入文件:

<person> 
    <ID>APersonID</ID> 
    <relatives /> 
</person> 

哪些应该被映射到

<relatives> 
    <person> 
     <ID>APersonID</ID> 
    </person> 
<relatives> 

我目前的映射无法处理这种情况。有人可以提供关于如何制作/编辑映射的建议,以便两种情况都可以工作吗?

回答

3

由于我们需要在进展之前测试至少一个relatives/relative的存在性,所以事情比起初看起来要复杂一点。除了使用XSLT之外,我想不出任何其他方法 - 请参阅here,了解如何从地图中提取XSLT,并将BTM更改为使用XSLT而不是可视函数映射。

以下XSLT

<?xml version="1.0" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" 
    exclude-result-prefixes="msxsl var" 
    version="1.0" 
    xmlns:ns0="http://BizTalk_Server_Project5.Schema1"> 
    <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" /> 
    <xsl:template match="/"> 
     <xsl:apply-templates select="/ns0:person" /> 
    </xsl:template> 
    <xsl:template match="/ns0:person"> 
     <relatives> 
      <xsl:variable name="personId" select="ns0:ID/text()" /> 
      <xsl:choose> 
       <xsl:when test="not(ns0:relatives) or not(ns0:relatives/ns0:relative)"> 
        <person> 
         <ID> 
          <xsl:value-of select="$personId" /> 
         </ID> 
        </person> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:for-each select="ns0:relatives/ns0:relative"> 
         <person> 
          <ID> 
           <xsl:value-of select="$personId" /> 
          </ID> 
          <relative> 
           <xsl:value-of select="ns0:name/text()" /> 
          </relative> 
         </person> 
        </xsl:for-each> 
       </xsl:otherwise> 
      </xsl:choose> 
     </relatives> 
    </xsl:template> 
</xsl:stylesheet> 

可生产你所描述的输出。 (显然改变你的命名空间匹配,并且我假设你已经得到了elementFormDefault="qualified"(如果没有,请删除ns0前缀)

+1

嗨,谢谢你的回答,使自定义XSLT解决了我的问题。 – Casper