XSLT - 测试每个节点的条件,并在满足条件时中断测试

时间:2013-02-03 17:04:54

标签: xslt xslt-1.0

我想为两种类型的情况返回1个输出。第一种情况必须循环通过节点以确定它是否满足。这是XML:

<in:inputs 
  xmlns:in="http://www.composite.net/ns/transformation/input/1.0">
   <in:result name="GetCart">
      <root xmlns="">
        <USC_Purchase ProductPrice="95.0000" 
                      PriceRuleID="1810" 
                      PurchaseQuantity="-1.00" 
                      PaymentNotRequiredQuantity="0.00" 
                      PaymentRequiredQuantity="-1.00" 
                      PaymentRequiredTotal="-95.000000" 
                      PurchaseStatus="R" 
                      RefundTotalAllowed="0.00">
          <USC_Product_PriceRule 
            PriceRuleID="1810" 
            PriceRuleName="Full Attendee" 
            PriceRulePriority="1" 
            PriceRuleStatus="A" 
            WebUserGroups="13CONF-M001" 
            ExcludeWebUserGroups="" 
            ProductPrice="95.0000" 
            ExternalCode="" 
            PercentOfProductCode="" 
            OptionID="0" 
            FriendlyName="Discounted Rate" 
            StartEndRestrictionID="0" 
            ClassID="0" 
            IsHidden="0"/>
        </USC_Purchase>
        <USC_Purchase ProductPrice="55.0000" 
                      PurchaseQuantity="-4.00" 
                      PaymentNotRequiredQuantity="0.00" 
                      PaymentRequiredQuantity="-4.00" 
                      PaymentRequiredTotal="-220.000000" 
                      PurchaseStatus="R" 
                      RefundTotalAllowed="568.00">
          <USC_Product_PriceRule/>
        </USC_Purchase>

这是我未完成的XSLT,它从第一个USC_Purchase节点开始:

    <xsl:choose>
      <xsl:when test="@PurchaseStatus='R' 
        and ($purchase_total*($purchase_total >=0)
           - $purchase_total*($purchase_total &lt; 0)) 
           > @RefundTotalAllowed">
       We are having issues processing your refund online. 
       Please contact us for assistance.
      </xsl:when>
      <xsl:otherwise>
        <!-- insert credit card form here -->
      </xsl:otherwise>

这很有效......只有当第一个产品符合这些条件时。其他产品未经检查。 xsl:choose语句顶部的for-each循环将返回多条消息,如果任何产品通过正常,也会返回信用卡表单。 (GRR!)

我的问题是 - 是否可以循环多个购买节点并在遇到单个案例后停止?

以下是步骤(如果我的解释是将任何人抛弃):

  1. 选择两个输出(错误信息和信用卡表格)。

  2. 对于每个USC_Purchase节点,如果&#39; X&#39;在任何节点上满足条件,显示单个错误消息。

  3. 否则,请显示信用卡表格。

  4. 如果需要更多信息,请告知我们。

    修改

    当然,purchase_total由所有paymentrequiredtotals的总和决定,所以:

    <xsl:variable 
      name="purchase_total" 
      select="sum(USC_Purchase/@PaymentRequiredTotal)" />
    

2 个答案:

答案 0 :(得分:2)

好的,我想我终于了解了你的要求。由于其功能特性,XSLT不倾向于使用“循环直到”逻辑(可以完成,但通常在另一种方法可用时不使用)。相反,测试通常同时应用于所有可能的目标,以查看是否满足任何条件。我相信以下内容应该做你想做的事情:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

  <xsl:template match="root">
    <xsl:variable
      name="purchase_total"
      select="sum(USC_Purchase/@PaymentRequiredTotal)" />
    <xsl:variable
      name="purchase_total_abs"
      select="$purchase_total * (1 - 2 * ($purchase_total &lt; 0))" />

    <xsl:choose>
      <xsl:when test="USC_Purchase[@PurchaseStatus  ='R' and
                                   $purchase_total_abs > @RefundTotalAllowed]">
        We are having issues processing your refund online. Please contact us for assistance.
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="CreditCardForm" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="CreditCardForm">
    <!-- Credit form-->
  </xsl:template>
</xsl:stylesheet>

为了使公式保持简短,首先purchase_total,然后确定其绝对值,然后进行测试以查看是否有USC_Purchase个匹配错误条件。如果是,则显示错误消息,如果没有,则显示信用卡表单。

答案 1 :(得分:0)

这种稍微缩短的转换会根据需要处理每个USC_Purchase元素 - 结果中会清楚地显示

<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:variable name="vTotalPaymentRequired"
   select="sum(/*/*/root/USC_Purchase/@PaymentRequiredTotal)"/>

 <xsl:template match="USC_Purchase">
  Purchase <xsl:value-of select="position()"/>
   <xsl:choose>
    <xsl:when test=
     "@PurchaseStatus  ='R'
    and not(@RefundTotalAllowed + $vTotalPaymentRequired >= 0)">
         <xsl:text>
          We are having issues processing your refund online.
         </xsl:text>
         <xsl:text> Please contact us for assistance.</xsl:text>
    </xsl:when>
    <xsl:otherwise>
     Credit Form displayed here
    </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
 <xsl:template match="USC_Product_PriceRule"/>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<in:inputs
  xmlns:in="http://www.composite.net/ns/transformation/input/1.0">
   <in:result name="GetCart">
      <root xmlns="">
        <USC_Purchase ProductPrice="95.0000"
                      PriceRuleID="1810"
                      PurchaseQuantity="-1.00"
                      PaymentNotRequiredQuantity="0.00"
                      PaymentRequiredQuantity="-1.00"
                      PaymentRequiredTotal="-95.000000"
                      PurchaseStatus="R"
                      RefundTotalAllowed="0.00">
          <USC_Product_PriceRule
            PriceRuleID="1810"
            PriceRuleName="Full Attendee"
            PriceRulePriority="1"
            PriceRuleStatus="A"
            WebUserGroups="13CONF-M001"
            ExcludeWebUserGroups=""
            ProductPrice="95.0000"
            ExternalCode=""
            PercentOfProductCode=""
            OptionID="0"
            FriendlyName="Discounted Rate"
            StartEndRestrictionID="0"
            ClassID="0"
            IsHidden="0"/>
        </USC_Purchase>
        <USC_Purchase ProductPrice="55.0000"
                      PurchaseQuantity="-4.00"
                      PaymentNotRequiredQuantity="0.00"
                      PaymentRequiredQuantity="-4.00"
                      PaymentRequiredTotal="-220.000000"
                      PurchaseStatus="R"
                      RefundTotalAllowed="568.00">
          <USC_Product_PriceRule/>
        </USC_Purchase>
    </root>
  </in:result>
</in:inputs>

产生了想要的正确结果:

  Purchase 1
          We are having issues processing your refund online.
          Please contact us for assistance.
  Purchase 2
     Credit Form displayed here
相关问题