XSL - 计算for-each循环中的迭代条件

时间:2014-02-05 14:45:15

标签: xslt foreach iteration muenchian-grouping

如何检查程序何时第一次运行<xsl:if test="$DriverCount > 1">语句,以便程序只执行一次。什么时候它最后一次运行相同的if语句,以便它只执行一次其他操作? position()和数字不起作用,因为if语句不总是在position()= 1上运行。我不能使用模板函数并且无法编写任何XSL函数,因为我正在使用XF设计器并且那些关键字不能在那里使用。

XML

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
   <DETAILS>
      <NAME>SANNIE</NAME>
      <SURNAME>DELANGE</SURNAME>
   </DETAILS>
   <INSURANCE_ITEMS>
      <VEHICLE>
         <XMLDOC_VEHICLE>
            <REGULAR_DRIVER_DETAILS>
               <DRIVER_NAME>SAM</DRIVER_NAME>
               <VEH>POLO</VEH>
            </REGULAR_DRIVER_DETAILS>
         </XMLDOC_VEHICLE> -<XMLDOC_VEHICLE>
            <REGULAR_DRIVER_DETAILS>
               <DRIVER_NAME>SANNIE</DRIVER_NAME>
               <VEH>GOLF</VEH>
            </REGULAR_DRIVER_DETAILS>
         </XMLDOC_VEHICLE> <XMLDOC_VEHICLE>
            <REGULAR_DRIVER_DETAILS>
               <DRIVER_NAME>SANNIE</DRIVER_NAME>
               <VEH>GOLF</VEH>
            </REGULAR_DRIVER_DETAILS>
         </XMLDOC_VEHICLE> <XMLDOC_VEHICLE>
            <REGULAR_DRIVER_DETAILS>
               <DRIVER_NAME>SAM</DRIVER_NAME>
               <VEH>MAZDA</VEH>
            </REGULAR_DRIVER_DETAILS>
         </XMLDOC_VEHICLE>
      </VEHICLE>
   </INSURANCE_ITEMS>
</ROOT>

XSL

<xsl:for-each select="/ROOT/INSURANCE_ITEMS/VEHICLE/XMLDOC_VEHICLE[not(REGULAR_DRIVER_DETAILS/DRIVER_NAME = preceding-sibling::XMLDOC_VEHICLE/REGULAR_DRIVER_DETAILS/DRIVER_NAME )]">
   <xsl:variable select="REGULAR_DRIVER_DETAILS/DRIVER_NAME" name="Driver"/>
   <xsl:variable select="count(/ROOT/INSURANCE_ITEMS/VEHICLE/XMLDOC_VEHICLE/REGULAR_DRIVER_DETAILS[DRIVER_NAME=$Driver])" name="DriverCount"/>
   <xsl:if test="$DriverCount > 1">
      <xsl:value-of select="'Hello '"/>
      <xsl:value-of select="REGULAR_DRIVER_DETAILS/DRIVER_NAME"/>
      <xsl:value-of select="' your cars are: '"/>
      <xsl:for-each select="/ROOT/INSURANCE_ITEMS/VEHICLE/XMLDOC_VEHICLE[REGULAR_DRIVER_DETAILS/DRIVER_NAME=$Driver]">
         <xsl:value-of select="REGULAR_DRIVER_DETAILS/VEH"/>
         <xsl:if test="$Driver = following-sibling::XMLDOC_VEHICLE/REGULAR_DRIVER_DETAILS/DRIVER_NAME">
            <xsl:value-of select="' and '"/>
         </xsl:if>
      </xsl:for-each>
   </xsl:if>
</xsl:for-each>

2 个答案:

答案 0 :(得分:0)

您可以使用其他过滤器当前相同类型之后的其他REGULAR_DRIVER_DETAILS/VEH 进一步检查:

<xsl:for-each select="/ROOT/INSURANCE_ITEMS/VEHICLE/XMLDOC_VEHICLE
 [REGULAR_DRIVER_DETAILS/DRIVER_NAME=$Driver]
 [not(following::REGULAR_DRIVER_DETAILS/VEH = REGULAR_DRIVER_DETAILS/VEH)]">

结果:

  

你好SAM你的车是:POLO和MAZDAHello SANNIE你的车是:GOLF

即。 Sannie的其他GOLF被省略。

然而,这整个方法看起来确实非常重要。可能是新工具的时间: - )

答案 1 :(得分:0)

谢谢,muenchian方法解决了我的问题。

  

<xsl:for-each select="INSURANCE_ITEMS/VEHICLE/XMLDOC_VEHICLE[count(. | key('cars-by-driver', REGULAR_DRIVER_DETAILS/DRIVER_NAME)[1]) = 2 and not(REGULAR_DRIVER_DETAILS/DRIVER_NAME = following-sibling::XMLDOC_VEHICLE/REGULAR_DRIVER_DETAILS/DRIVER_NAME )]">

  <xsl:if test="position() != 1">
    <xsl:value-of select="'; and '"/>
  </xsl:if>
  <xsl:if test="position() = 1">
    <xsl:value-of select="'We note that '"/>
  </xsl:if>

  <xsl:value-of select="REGULAR_DRIVER_DETAILS/DRIVER_NAME"/>
  <xsl:value-of select="' is noted as the regular driver on more than one vehicle: '"/>
  <xsl:for-each select="key('cars-by-driver', REGULAR_DRIVER_DETAILS/DRIVER_NAME)">
    <xsl:value-of select="VEH"/>

    <xsl:choose>
      <xsl:when test="position() &lt; last() - 1">
        <xsl:value-of select="', '"/>
      </xsl:when>
      <xsl:when test="position() = last() - 1">
        <xsl:value-of select="' and '"/>
      </xsl:when>
    </xsl:choose>

  </xsl:for-each>
  <xsl:if test="position() = last()">
    <xsl:value-of select="'. Remember: it is imperative that you supply the details of the correct regular driver, which is the individual who will be driving the vehicle most often in any monthly period, as the premium is calculated on the profile of that person.'"/>
  </xsl:if>
</xsl:for-each>

使用XML:

<INSURANCE_ITEMS>
    <VEHICLE>
        <XMLDOC_VEHICLE>    
            <VEH>POLO</VEH>
            <REGULAR_DRIVER_DETAILS>
                <DRIVER_NAME>SANNIE</DRIVER_NAME>           
            </REGULAR_DRIVER_DETAILS>
        </XMLDOC_VEHICLE>
        <XMLDOC_VEHICLE>    
        <VEH>HYUNDAI</VEH>
            <REGULAR_DRIVER_DETAILS>
                <DRIVER_NAME>SAREL</DRIVER_NAME>
                </REGULAR_DRIVER_DETAILS>
        </XMLDOC_VEHICLE>
        <XMLDOC_VEHICLE>    
        <VEH>GOLF</VEH>
            <REGULAR_DRIVER_DETAILS>
                <DRIVER_NAME>SAM</DRIVER_NAME>                  
            </REGULAR_DRIVER_DETAILS>
        </XMLDOC_VEHICLE>
        <XMLDOC_VEHICLE>    
        <VEH>MAZDA</VEH>
            <REGULAR_DRIVER_DETAILS>
                <DRIVER_NAME>SAM</DRIVER_NAME>
                </REGULAR_DRIVER_DETAILS>
        </XMLDOC_VEHICLE>
        <XMLDOC_VEHICLE>    
        <VEH>BAKKIE</VEH>
            <REGULAR_DRIVER_DETAILS>
                <DRIVER_NAME>SAREL</DRIVER_NAME>
                </REGULAR_DRIVER_DETAILS>
        </XMLDOC_VEHICLE>
    </VEHICLE>
</INSURANCE_ITEMS>

输出:

我们注意到SAM被认为是多辆车的常规驾驶员:GOLF和MAZDA; SAREL被认为是多辆车的常规驾驶员:HYUNDAI和BAKKIE。请记住:您必须提供正确的常规驾驶员的详细信息,即在任何月度期间最常驾驶车辆的人员,因为保险费是根据该人员的个人资料计算的。