【问题标题】:Mapping recursive structures in BizTalk在 BizTalk 中映射递归结构
【发布时间】:2013-03-26 19:30:26
【问题描述】:

我有以下 XML

     <response>
       <Contacts>
         <Contact>
           <Name>John Doe</Name>
           <Age>48</Age>
           <DOB>
             <Day>12</Day>
             <Month>6</Month>
             <Year>1964</Year>
           </DOB>
           <Contacts>
             <Contact>
                <Name>Jane Walsh</Name>
                <Age>30</Age>
                <DOB>
                  <Day>24</Day>
                  <Month>3</Month>
                  <Year>1983</Year>
                </DOB>
             </Contact>
             <Contact>
               <Name>Rob Marsh</Name>
               <Age>55</Age>
               <DOB>
                 <Day>1</Day>
                 <Month>Feb</Month>
                 <Year>1958</Year>
               </DOB>
             </Contact>
           </Contacts>
         </Contact>
       </Contacts>
    </response>

我正在使用身份转换将结构复制到目标。

    <xsl:template name = "testTemplate" match="/*[local-name()='response']/*">
      <xsl:apply-templates mode="copy-no-ns" select="response"/>
    </xsl:template>
    <!-- Selectively mass copy some of the nodes without namespaces -->
    <xsl:template mode="copy-no-ns" match="*">
      <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates mode="copy-no-ns" select="node()"/>
      </xsl:element>
    </xsl:template>

XSL 在 Altova XMLSpy 中工作,当我使用 Visual Studio 2010 对其进行测试时,它也会产生所需的输出。但是 BizTalk 地图会产生如下的空节点(删除了正确复制的其他内容)。

    <Contacts>
      <Contact>
        <Contacts>
          <Contact />
          <Contact />
        </Contacts>
      <Contact>
    </Contacts>

我不知道发生了什么以及如何解决此问题。有什么建议么?非常感谢

【问题讨论】:

    标签: xslt mapping inline biztalk


    【解决方案1】:

    您的明显问题在这里:

     <xsl:template name = "testTemplate" match="/*[local-name()='response']/*">
          <xsl:apply-templates mode="copy-no-ns" select="response"/>
     </xsl:template>
    

    此模板匹配顶部元素 response 的任何子元素(在本例中,仅匹配名为 Contacts 的元素。

    然后它将模板应用于匹配的Contacts 元素的所有子元素,这些子元素名为response但是,Contacts 元素没有名为 response 的子元素。此时转换无法再产生任何输出。

    最后的结果就是

    <response>
    
    
    </response>
    

    这是完整的转换

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template name = "testTemplate" match="/*[local-name()='response']/*">
          <xsl:apply-templates mode="copy-no-ns" select="response"/>
     </xsl:template>
    
        <!-- Selectively mass copy some of the nodes without namespaces -->
     <xsl:template mode="copy-no-ns" match="*">
       <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates mode="copy-no-ns" select="node()"/>
       </xsl:element>
     </xsl:template>
    </xsl:stylesheet>
    

    应用于提供的 XML 文档时:

    <response>
        <Contacts>
            <Contact>
                <Name>John Doe</Name>
                <Age>48</Age>
                <DOB>
                    <Day>12</Day>
                    <Month>6</Month>
                    <Year>1964</Year>
                </DOB>
                <Contacts>
                    <Contact>
                        <Name>Jane Walsh</Name>
                        <Age>30</Age>
                        <DOB>
                            <Day>24</Day>
                            <Month>3</Month>
                            <Year>1983</Year>
                        </DOB>
                    </Contact>
                    <Contact>
                        <Name>Rob Marsh</Name>
                        <Age>55</Age>
                        <DOB>
                            <Day>1</Day>
                            <Month>Feb</Month>
                            <Year>1958</Year>
                        </DOB>
                    </Contact>
                </Contacts>
            </Contact>
        </Contacts>
    </response>
    

    结果如上解释:

    <response>
    
    
    </response>
    

    解决方案

    只需替换

    <xsl:apply-templates mode="copy-no-ns" select="response"/>
    

    <xsl:apply-templates mode="copy-no-ns" select="node()"/>
    

    完整的变换变成

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template name = "testTemplate" match="/*[local-name()='response']/*">
          <xsl:apply-templates mode="copy-no-ns" select="node()"/>
     </xsl:template>
    
        <!-- Selectively mass copy some of the nodes without namespaces -->
     <xsl:template mode="copy-no-ns" match="*">
       <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
         <xsl:copy-of select="@*"/>
         <xsl:apply-templates mode="copy-no-ns" select="node()"/>
       </xsl:element>
     </xsl:template>
    </xsl:stylesheet>
    

    当对提供的 XML 文档(如上)应用此转换时,会产生想要的结果

    <response>
       <Contact>
          <Name>John Doe</Name>
          <Age>48</Age>
          <DOB>
             <Day>12</Day>
             <Month>6</Month>
             <Year>1964</Year>
          </DOB>
          <Contacts>
             <Contact>
                <Name>Jane Walsh</Name>
                <Age>30</Age>
                <DOB>
                   <Day>24</Day>
                   <Month>3</Month>
                   <Year>1983</Year>
                </DOB>
             </Contact>
             <Contact>
                <Name>Rob Marsh</Name>
                <Age>55</Age>
                <DOB>
                   <Day>1</Day>
                   <Month>Feb</Month>
                   <Year>1958</Year>
                </DOB>
             </Contact>
          </Contacts>
       </Contact>
    </response>
    

    请注意

    此转换假定元素没有属性。如果这个假设不成立,那么转换会在任何具有属性的 XML 文档上产生错误的结果。可以提供正确且更通用的转换,但我相信这就是您所要求的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 2020-09-30
      • 1970-01-01
      • 2017-04-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多