2012-09-21 22 views
1

我有一個情況如下。我正在合併2個xml文件。我比較兩個文件中特定類型的節點,並檢查它們的某些特定屬性是否相同。如果該屬性相同,則合併該節點,如果不合並,則複製文件一的節點。請看下面的邏輯我們如何修改和訪問參數的值?

for-each(file1/nodes) 
    boolean variable var set to false 
    for-each (file2/nodes) 
     when(some_condition) 
     var = true 
    /for-each 
    if(var = false) 
     do-something 
/for-each 

我的問題是,我無法設置和保留這個變量。由於我對xslt非常陌生,因此需要使用xslt來完成此操作。

<xslt:for-each select="$file1_coverage/packages/package"> 
<xslt:variable name="file1_package_name" select="@name" /> 
<!-- I want to declare a boolean variable and set it false--> 
<xslt:for-each select="$file2_coverage/packages/package"> 
    <xslt:variable name="file2_package_name" select="@name" /> 
    <xslt:choose> 
     <xslt:when test="$file1_package_name=$file2_package_name"> 
      <!-- Set the boolean variable to true--> 
      <package> 
       <xslt:attribute name="branch-rate"> 
        <xslt:value-of select="(($file1_package_branch_rate * $file1_package_branch_total) + ($file2_package_branch_rate * $file2_package_branch_total)) div ($file1_package_branch_total + $file2_package_branch_total)" /> 
       </xslt:attribute> 
      </package> 
     </xslt:when> 
</xslt:for-each> 
<xslt:when test="boolean Variable is false">         
     <package> 
      <xslt:copy-of select="$file1_package/@*" /> 
      <xslt:copy-of select="$file1_package/*" /> 
     </package> 
</xslt:when> 
</xslt:for-each> 
+0

你可以發佈一些簡單的示例輸入和預期的輸出?什麼版本的XSLT? –

+0

添加示例代碼。我的要求在評論 – Messiah

+0

考慮發佈一個XML輸入示例和您想用XSLT創建的相應輸出示例,那麼這裏的人可以用適當的XSLT方法來幫助您解決這個問題。我擔心發佈一些強制性僞代碼不允許我們提供具體的XSLT代碼,因爲XSLT不是命令式語言。由於您似乎正在處理兩個輸入文檔,請告訴我們您是否正在使用XSLT 1.0或2.0,因爲2.0可以更好地支持使用不同的輸入文檔輕鬆進行交叉引用(例如'key('key-name','鍵值',documentNode)')。 –

回答

0

booleanVariable超出範圍的第一xsl:when後 - 其範圍僅是xsl:when

內定義一個布爾變量的正確和有效的方法是

<xsl:variable name="vboolVar"> 
<xsl:choose> 
    <xsl:when test="some-condition">true</xsl:when> 
    <xsl:otherwise></xsl:otherwise> 
</xsl:choose> 
</xsl:variable> 

然後在其父元素的範圍內的任何位置使用此變量。

當然,可以定義相同只是

<xsl:variable name="vboolVar" select="some-condition"/> 
0

你並不需要一個布爾變量。改用兩個循環。在第一個處理所有匹配的條目,並在第二個條目不匹配。

<xslt:for-each select="$file1_coverage/packages/package"> 
    <xslt:variable name="file1_package_name" select="@name" /> 
    <!-- handle all matching entries --> 
    <xslt:for-each select="$file2_coverage/packages/package"> 
         <xslt:variable name="file2_package_name" select="@name" /> 
     <xslt:choose> 
          <xslt:when test="$file1_package_name=$file2_package_name"> 
              <package> 
                  <xslt:attribute name="branch-rate"> 
                       <xslt:value-of select="(($file1_package_branch_rate * $file1_package_branch_total) + ($file2_package_branch_rate * $file2_package_branch_total)) div  
          ($file1_package_branch_total + $file2_package_branch_total)" /> 
        </xslt:attribute> 
       </package> 
      </xslt:when> 
     <xslt:choose> 
    </xslt:for-each> 
    <!-- … and now the not matching entries --> 
    <xslt:for-each select="$file2_coverage/packages/package"> 
         <xslt:variable name="file2_package_name" select="@name" /> 
     <xslt:choose> 
          <xslt:when test="not ($file1_package_name=$file2_package_name)"> 
              <package> 
                 <xslt:copy-of select="$file1_package/@*" /> 
              <xslt:copy-of select="$file1_package/*" /> 
       </package> 
      </xslt:when> 
     <xslt:choose> 
    </xslt:for-each> 
</xslt:for-each> 
相關問題