BizTalk映射内联XSLT

huangapple go评论53阅读模式
英文:

BizTalk mapping inline XSLT

问题

<Agent><xsl:value-of select="$var:v2" /></Agent>
英文:

Input schema have field &lt;Partycode Value=&quot;Agent&quot; /&gt;. Based on the this node I need to generate one destination node. Destination is single schema. If partycode is Agent I want destination node <Agent> with value of Fname.

If partycode is not agent I want destination node <Agent> without any value. &lt;Participatent&gt;&lt;/Participatent&gt; unbounded in the schema. In destination is not unbounded

Scenario 1
Inputschema

&lt;ns0:Schema xmlns:ns0=&quot;http://Host_service_proj.Inbound_test1&quot;&gt;
  &lt;Participatent&gt;
    &lt;ParticipantDetails&gt;
      &lt;Participant&gt;
        &lt;Partycode Value=&quot;&quot; /&gt;
        &lt;FIRSTname&gt;FIRSTname_0&lt;/FIRSTname&gt;
        &lt;/Participant&gt;
     &lt;Participant&gt;
         &lt;Partycode Value=&quot;Agent&quot; /&gt;
        &lt;FIRSTname&gt;Agentname1&lt;/FIRSTname&gt;
         &lt;/Participant&gt;
    &lt;/ParticipantDetails&gt;
  &lt;/Participatent&gt;
&lt;/ns0:Schema&gt;

Expected result

&lt;ns0:CreateClaim xmlns:ns0=&quot;http://Host_service_proj.Oubound_Test1&quot;&gt;
  &lt;Agent&gt;Agentname1&lt;/Agent&gt;
&lt;/ns0:CreateClaim&gt;

Scenario 2
Inputschema

&lt;ns0:Schema xmlns:ns0=&quot;http://Host_service_proj.Inbound_test1&quot;&gt;
  &lt;Participatent&gt;
    &lt;ParticipantDetails&gt;
      &lt;Participant&gt;
          &lt;Partycode Value=&quot;&quot; /&gt;
        &lt;FIRSTname&gt;FIRSTname_0&lt;/FIRSTname&gt;
      &lt;/Participant&gt;
    &lt;/ParticipantDetails&gt;
  &lt;/Participatent&gt;
&lt;/ns0:Schema&gt;

Expected result

&lt;ns0:CreateClaim xmlns:ns0=&quot;http://Host_service_proj.Oubound_Test1&quot;&gt;
&lt;Agent&gt;&lt;/Agent&gt;
&lt;/ns0:CreateClaim&gt;

I am trying to write inline xslt for this

&lt;xsl:for-each select=&quot;Participatent/ParticipantDetails/Participant&quot;&gt;
  &lt;xsl:variable name=&quot;var:v1&quot; select=&quot;userCSharp:LogicalEq(string(Partycode/@Value) , &amp;quot;Agent&amp;quot;)&quot; /&gt;
  &lt;xsl:if test=&quot;string($var:v1)=&#39;true&#39;&quot;&gt;
    &lt;xsl:variable name=&quot;var:v2&quot; select=&quot;FIRSTname/text()&quot; /&gt;
    &lt;Agent&gt;
      &lt;xsl:value-of select=&quot;$var:v2&quot; /&gt;
    &lt;/Agent&gt;
  &lt;/xsl:if&gt;
&lt;/xsl:for-each&gt;

But I am not able to do blank &lt;Agent&gt; in destination in the second scenario.

答案1

得分: 1

你需要使用choose而不是if,并且不要使用循环,只需使用XPath。

实际上,你不需要变量和条件判断,你可以使用XPath一行代码完成。

XPath中的`[Partycode/@Value=&#39;Agent&#39;]`部分选择了你想要的Participant节点。

如果有多个Partycode等于Agent的Participant,你可以指定只选择第一个。
英文:

You need to use choose rather than an if, and don't use a loop, just use XPath.

&lt;xsl:variable name=&quot;var:v1&quot; select=&quot;/*[local-name()=&#39;Schema&#39; and namespace-uri()=&#39;http://Host_service_proj.Inbound_test1&#39;]/*[local-name()=&#39;Participatent&#39; and namespace-uri()=&#39;&#39;]/*[local-name()=&#39;ParticipantDetails&#39; and namespace-uri()=&#39;&#39;]/*[local-name()=&#39;Participant&#39; and namespace-uri()=&#39;&#39;][Partycode/@Value=&#39;Agent&#39;]/*[local-name()=&#39;FIRSTname&#39; and namespace-uri()=&#39;&#39;]&quot; /&gt;
   &lt;xsl:choose&gt;
     &lt;xsl:when test=&quot;string($var:v1)!=&#39;&#39;&quot;&gt;
       &lt;Agent&gt;
         &lt;xsl:value-of select=&quot;$var:v1&quot; /&gt;
       &lt;/Agent&gt;
     &lt;/xsl:when&gt;
     &lt;xsl:otherwise&gt;
       &lt;Agent&gt;
       &lt;/Agent&gt;
     &lt;/xsl:otherwise&gt;
   &lt;/xsl:choose&gt;

In fact you don't need variables and a decision at all, you can do it with one line with XPath.

       &lt;Agent&gt;
         &lt;xsl:value-of select=&quot;/*[local-name()=&#39;Schema&#39; and namespace-uri()=&#39;http://Host_service_proj.Inbound_test1&#39;]/*[local-name()=&#39;Participatent&#39; and namespace-uri()=&#39;&#39;]/*[local-name()=&#39;ParticipantDetails&#39; and namespace-uri()=&#39;&#39;]/*[local-name()=&#39;Participant&#39; and namespace-uri()=&#39;&#39;][Partycode/@Value=&#39;Agent&#39;]/*[local-name()=&#39;FIRSTname&#39; and namespace-uri()=&#39;&#39;]&quot; /&gt;
       &lt;/Agent&gt;

Breaking the XPath down for readability, you can see part where it has [Partycode/@Value=&#39;Agent&#39;] is the part that selects the Participant node you want.

/*[local-name()=&#39;Schema&#39; and namespace-uri()=&#39;http://Host_service_proj.Inbound_test1&#39;]
/*[local-name()=&#39;Participatent&#39; and namespace-uri()=&#39;&#39;]/*[local-name()=&#39;ParticipantDetails&#39; and namespace-uri()=&#39;&#39;]
/*[local-name()=&#39;Participant&#39; and namespace-uri()=&#39;&#39;][Partycode/@Value=&#39;Agent&#39;]
/*[local-name()=&#39;FIRSTname&#39; and namespace-uri()=&#39;&#39;]

And just in case you have more than one Participant with the Partycode = Agent, you can tell it to just select the first one.

/*[local-name()=&#39;Schema&#39; and namespace-uri()=&#39;http://Host_service_proj.Inbound_test1&#39;]
/*[local-name()=&#39;Participatent&#39; and namespace-uri()=&#39;&#39;]/*[local-name()=&#39;ParticipantDetails&#39; and namespace-uri()=&#39;&#39;]
/*[local-name()=&#39;Participant&#39; and namespace-uri()=&#39;&#39;][Partycode/@Value=&#39;Agent&#39;][1]
/*[local-name()=&#39;FIRSTname&#39; and namespace-uri()=&#39;&#39;]

huangapple
  • 本文由 发表于 2023年5月17日 15:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定