2017-08-27 40 views
1
 Source Xml : 
    <Lot> 
      <LotDetails> 
       <GCode>Ship</GCode> 
       <ProductQuantity>2</ProductQuantity> 
      </LotDetails 
      <LotDetails> 
       <GCode>Reject</GCode> 
       <ProductQuantity>4</ProductQuantity> 
      </LotDetails> 
      <LotDetails> 
       <GCode>Gross</GCode> 
       <ProductQuantity>3</ProductQuantity> 
      </LotDetails> 
      <LotDetails> 
       <GCode>Acceptable</GCode> 
       <ProductQuantity>5</ProductQuantity> 
      </LotDetails> 
     </Lot> 
     <Lot> 

     <LotDetails> 
       <GCode>Ship</GCode> 
       <ProductQuantity>2</ProductQuantity> 
      </LotDetails 
      <LotDetails> 
       <GCode>Reject</GCode> 
       <ProductQuantity>4</ProductQuantity> 
      </LotDetails> 
      <LotDetails> 
       <GCode>Gross</GCode> 
       <ProductQuantity>3</ProductQuantity> 
      </LotDetails> 
      <LotDetails> 
       <GCode>Acceptable1</GCode> 
       <ProductQuantity>5</ProductQuantity> 
      </LotDetails> 
     </Lot> 

     <LotDetails> 
       <GCode>Ship1</GCode> 
       <ProductQuantity>2</ProductQuantity> 
      </LotDetails 
      <LotDetails> 
       <GCode>Reject</GCode> 
       <ProductQuantity>4</ProductQuantity> 
      </LotDetails> 
      <LotDetails> 
       <GCode>Gross</GCode> 
       <ProductQuantity>3</ProductQuantity> 
      </LotDetails> 
      <LotDetails> 
       <GCode>Acceptable1</GCode> 
       <ProductQuantity>5</ProductQuantity> 
      </LotDetails> 
     </Lot> 
     TargetXml Field : 

    <Lot><LotQty>5</LotQty></Lot> 
    <Lot> <LotQty>2</LotQty></Lot> 
    <Lot><LotQty>3</LotQty> </Lot> 

     Here the condition to check to map the target field is : 

      If LotDetails/GCode = "Acceptable" and the corresponding ProductQuantity not (null and zero) 

      <LotQty> --> 5 [taken from the corresponding ProductQuantity ] 

      else if LotDetails/GCode = "Ship" and the corresponding ProductQuantity not (null and zero) 

      <LotQty> --> 2 

      else if LotDetails/GCode = "Gross" and the corresponding ProductQuantity not (null and zero) 

      <LotQty> --> 3 

      else 

      <LotQty> --> 0 

     Here the LotQty should happen only once for a particular iteration of <Lot> based on any of the conditions 
     if any first condition is satisfied then the map LotQTY from the corresponding ProductQuantity ? 
When we use for each LotDetails- we get the qty correctly but it produces multiple results for LotQTY inside each loop as below 
<Lot> 
     <LotQty>5</LotQty> 
     <LotQty>2</LotQty> 
     <LotQty>3</LotQty> 
</Lot> 

    <Lot>  
     <LotQty>2</LotQty> 
     <LotQty>3</LotQty> 
</Lot> 
    <Lot> 
     <LotQty>3</LotQty> 
</Lot> 

When i only use IF conditions with out for each the qty is always selected from the first matched condition 
<Lot><LotQty>5</LotQty></Lot> 
<Lot> <LotQty>5</LotQty></Lot> 
    <Lot><LotQty>5</LotQty> </Lot 


What i need is as below is only one lotQty for each lot based on first satisfied condition with the corresponding qty 


    <Lot><LotQty>5</LotQty></Lot> 
    <Lot><LotQty>2</LotQty></Lot> 
    <Lot><LotQty>3</LotQty></Lot> 

Not sure on the template mach usage a being a beginner on xslt usage 

如何根據相應的LotDetails/GCode獲取正確的LotDetails/ProductQuantity,並且不使用每個LotDetails/GCode? 任何可以從if動態傳遞滿足條件的方法,並使用LotDetails選擇正確的ProductQuantity或從當前節點中選擇ProductQuantity?XSLT條件在foreach中只發生一次

Please let know xslt experts on the best way to do this ? 
+0

您的問題不清楚。請在給定示例中顯示預期的結果,並在必要時添加更多示例。 - FYI,'xsl:for-each'不是一個循環,不能退出或繼續。如果你想要一個結果,不要使用'xsl:for-each'。 –

+0

@Dev,嘗試將整個條件放入if標籤或模板的匹配條件中。否則,如果您因爲某種原因需要for-each,請創建xsl變量並將其用作標誌。一旦顯示了所需的值,將該變量設置爲false以忽略任何其他值。 – derloopkat

+0

感謝您輸入 - 我變化到單個如果與對每個條件 – Dev

回答

0

考慮下面的例子:

XML

<Lots> 
    <Lot> 
     <LotDetails> 
      <GCode>Ship</GCode> 
      <ProductQuantity>2</ProductQuantity> 
     </LotDetails> 
     <LotDetails> 
      <GCode>Reject</GCode> 
      <ProductQuantity>4</ProductQuantity> 
     </LotDetails> 
     <LotDetails> 
      <GCode>Gross</GCode> 
      <ProductQuantity>3</ProductQuantity> 
     </LotDetails> 
     <LotDetails> 
      <GCode>Acceptable</GCode> 
      <ProductQuantity>5</ProductQuantity> 
     </LotDetails> 
    </Lot> 
    <Lot> 
     <LotDetails> 
      <GCode>Ship</GCode> 
      <ProductQuantity>2</ProductQuantity> 
     </LotDetails> 
     <LotDetails> 
      <GCode>Reject</GCode> 
      <ProductQuantity>4</ProductQuantity> 
     </LotDetails> 
     <LotDetails> 
      <GCode>Gross</GCode> 
      <ProductQuantity>3</ProductQuantity> 
     </LotDetails> 
     <LotDetails> 
      <GCode>Acceptable</GCode> 
     </LotDetails> 
    </Lot> 
    <Lot> 
     <LotDetails> 
      <GCode>Ship</GCode> 
     </LotDetails> 
     <LotDetails> 
      <GCode>Reject</GCode> 
      <ProductQuantity>4</ProductQuantity> 
     </LotDetails> 
     <LotDetails> 
      <GCode>Gross</GCode> 
      <ProductQuantity>3</ProductQuantity> 
     </LotDetails> 
     <LotDetails> 
      <GCode>Acceptable</GCode> 
      <ProductQuantity>0</ProductQuantity> 
     </LotDetails> 
    </Lot> 
</Lots> 

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/Lots"> 
    <xsl:copy> 
     <xsl:for-each select="Lot"> 
      <xsl:copy> 
       <Quantity> 
        <xsl:choose> 
         <xsl:when test="LotDetails[GCode='Acceptable' and ProductQuantity > 0]"> 
          <xsl:value-of select="LotDetails[GCode='Acceptable']/ProductQuantity"/> 
         </xsl:when> 
         <xsl:when test="LotDetails[GCode='Ship' and ProductQuantity > 0]"> 
          <xsl:value-of select="LotDetails[GCode='Ship']/ProductQuantity"/> 
         </xsl:when> 
         <xsl:when test="LotDetails[GCode='Gross' and ProductQuantity >0]"> 
          <xsl:value-of select="LotDetails[GCode='Gross']/ProductQuantity"/> 
         </xsl:when> 
         <xsl:otherwise>0</xsl:otherwise> 
        </xsl:choose> 
       </Quantity> 
      </xsl:copy> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

結果

<?xml version="1.0" encoding="UTF-8"?> 
<Lots> 
    <Lot> 
    <Quantity>5</Quantity> 
    </Lot> 
    <Lot> 
    <Quantity>2</Quantity> 
    </Lot> 
    <Lot> 
    <Quantity>3</Quantity> 
    </Lot> 
</Lots> 
+0

感謝邁克爾您的快速回應 - 但我有更多的問題。可以請讓知道,如果如以下的複製命令對應於輸入和輸出元件的 <的xsl:for-每個選擇= 「地塊」> Dev

+0

'的xsl:copy'執行當前節點的淺拷貝 - 在我的例子中,'Lots'和'Lot'被如此複製。 –

+0

感謝Michael的快速反應 - 但我還有其他幾個問題。請讓我們知道下面的複製命令是否與輸入和輸出元素相對應。這是因爲我使用此示例代表輸入和輸出的實際代碼不同的標籤名稱副本在輸出中創建不需要的輸入元素標籤。請告知該評論是否不明確 – Dev