2

私は次の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>

ID変換を使用して、構造をターゲットにコピーしています。

    <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はAltovaXMLSpyで動作し、Visual Studio 2010でテストすると、目的の出力が生成されます。ただし、BizTalkマップは次のように空のノードを生成します(正しくコピーされた他のコンテンツを削除しました)。

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

何が起こっているのか、これを修正する方法がわかりません。助言がありますか?トンありがとう

4

1 に答える 1

4

あなたの明らかな問題はここにあります:

 <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ドキュメントで誤った結果が生成されます。正確でより一般的な変換を提供することは可能ですが、これがあなたが求めているものであると私は信じています。

于 2013-03-27T03:51:41.013 に答える