2

XSLT を使用して、この形式から値をマップする必要があります。

<Package>
<WorkflowProcesses>
    <WorkflowProcess>            
        <Activities>                
            <Activity Name="First Activity" Id="123">                    
            </Activity>       
            <Activity Name="Second Activity" Id="456">                    
            </Activity> 
            <Activity Name="Third Activity" Id="789">                    
            </Activity> 
        </Activities>
        <Transitions>
            <Transition To="789" From="456" Id="ABC">                
            </Transition>  
            <Transition To="456" From="123" Id="XYZ">                    
            </Transition>            
        </Transitions>
    </WorkflowProcess>
</WorkflowProcesses>
</Package>

この形式に:

<variable type="State">
   <stateId type="Integer">123</stateId>
   <stateName type="String">First Activity</stateName>
   <previousStatesId type="String[]">       
   </previousStatesId>
   <nextStatesId type="String[]">
      <item>456</item>
   </nextStatesId>
</variable>           

<variable type="State">
   <stateId type="Integer">456</stateId>
   <stateName type="String">Second Activity</stateName>
   <previousStatesId type="String[]">
    <item>123</item>
       </previousStatesId>
   <nextStatesId type="String[]">
      <item>789</item>
   </nextStatesId>
</variable>    

<variable type="State">
   <stateId type="Integer">789</stateId>
   <stateName type="String">Third Activity</stateName>
   <previousStatesId type="String[]">
    <item>456</item>
   </previousStatesId>
   <nextStatesId type="String[]">
   </nextStatesId>
</variable>  

私が苦労している部分は、「トランジション」セクションから値を取得し、それらを正しい領域 (「nextStatesId」および「previousStatesId」) に割り当てることです。他のすべては期待どおりに機能しています。

これまでのところ、これを使用してアクティビティアイテムのマッピングに成功しています:

<xsl:template match="Activities">  
<xsl:apply-templates/>  
</xsl:template>

<xsl:template match="Activity [@Name]">  
<variable type="State">
    <stateId type="Integer"><xsl:value-of select="@Id"/></stateId>
     <stateName type="String"><xsl:value-of select="@Name"/></stateName>
     </variable>
</xsl:template>
</xsl:stylesheet>

これを使用して、すべての遷移アイテムを識別できます。次に、遷移の ID を現在のアクティビティと比較しようとしましたが、上記のテンプレート内では機能しません。これは、テンプレートが XML ファイル全体を表示できないため、マッピングを理解していないためだと思います。

<xsl:for-each select="Package/WorkflowProcesses/WorkflowProcess/Transitions/Transition">    
      <p>...do some stuff...</p>        
    </xsl:for-each>

通常、私がコーディングする場合は、それらを 2 つのリストに押し込んでから検索します。ここで計画していたのは、前/次の状態 ID の単純な「if」ステートメントを使用して、各アクティビティ内からすべての遷移をループすることでした...しかし、それは失敗するようです。

これを達成するための別の方法を知っている人はいますか?

4

2 に答える 2

1

この変換は、あなたが求めることを行うようです。

「to this format」にはルート要素がないため整形式ではないため、ダミー要素を追加しました<root>

変換はキーを使用しTransitionて、属性によって要素にアクセスします。

そうでなければ空の要素と出力要素にスペースと改行の内容が本当に必要なのだろうか?この変換では、代わりに自己終了の空の要素が生成されます。厳密な精度が必要な場合、変更は難しくありません。previousStatesIdnextStatesId

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:key name="transition-from" match="Transition" use="@From"/>
  <xsl:key name="transition-to" match="Transition" use="@To"/>

  <xsl:template match="/">
    <root>
      <xsl:apply-templates select="Package/WorkflowProcesses/WorkflowProcess/Activities/Activity"/>
    </root>
  </xsl:template>

  <xsl:template match="Activity">
    <xsl:variable name="from-here" select="key('transition-from', @Id)"/>
    <xsl:variable name="to-here" select="key('transition-to', @Id)"/>
    <variable type="State">
      <stateId type="Integer">
        <xsl:value-of select="@Id"/>
      </stateId>
      <stateName type="String">
        <xsl:value-of select="@Name"/>
      </stateName>
      <previousStatesId type="String[]">
        <xsl:if test="$to-here">
          <item>
            <xsl:value-of select="$to-here/@From"/>
          </item>
        </xsl:if>
      </previousStatesId>
      <nextStatesId type="String[]">
        <xsl:if test="$from-here">
          <item>
            <xsl:value-of select="$from-here/@To"/>
          </item>
        </xsl:if>
      </nextStatesId>
    </variable>
  </xsl:template>

</xsl:stylesheet>

出力

<root>
   <variable type="State">
      <stateId type="Integer">123</stateId>
      <stateName type="String">First Activity</stateName>
      <previousStatesId type="String[]"/>
      <nextStatesId type="String[]">
         <item>456</item>
      </nextStatesId>
   </variable>
   <variable type="State">
      <stateId type="Integer">456</stateId>
      <stateName type="String">Second Activity</stateName>
      <previousStatesId type="String[]">
         <item>123</item>
      </previousStatesId>
      <nextStatesId type="String[]">
         <item>789</item>
      </nextStatesId>
   </variable>
   <variable type="State">
      <stateId type="Integer">789</stateId>
      <stateName type="String">Third Activity</stateName>
      <previousStatesId type="String[]">
         <item>456</item>
      </previousStatesId>
      <nextStatesId type="String[]"/>
   </variable>
</root>
于 2013-06-07T21:30:48.383 に答える