多个实体中断处每个循环的 jdeveloper XSLT

jdeveloper XSLT for each loop at multiple entities break

输入XML:

<Response>
         <Quote qtID="5443655">
             <Incidents>
               <Incident name="Subway1" active="true" primary="false" ActivityID="32">
                  <IncidentConfig>
                     <AccountID>CCCCCCCC</AccountID>
                  </IncidentConfig>
                  <cals>
                     <cal name="PMP" value="32.0">
                   </cal>
                     <cal name="TC" value="85.83612">
                     </cal>
                     <cal name="D" value="70.83612">
                        </cal>
                    </cals>
               </Incident>
               <Incident name="Subway2" active="true" primary="true" ActivityID="33">
                  <IncidentConfig>
                     <AccountID>DDDDD</AccountID>
                  </IncidentConfig>
                  <cals>
                     <cal name="TC" value="26.0">
                        </cal>
                     <cal name="D" value="86.83612">
                        </cal>
                    </cals>
               </Incident>
                <Incident name="Subway3" active="true" primary="false" ActivityID="33">
                  <IncidentConfig>
                     <AccountID>DDDDD</AccountID>
                  </IncidentConfig>
                  <cals>
                     <cal name="PMP" value="39.0">
                        </cal>
                     <cal name="D" value="24.83612">
                        </cal>
                    </cals>
               </Incident>
            </Incidents>
         </Quote>
      </Response>

我知道在 XSLT for-each 循环中不能中断。但是从下面的例子来看,如果我想显示名称为 PMP 的 'cal' 元素存在,那么取 PMP 值,如果不存在,取 TC 值。我尝试了以下代码,但没有用。

<charge>
  <xsl:value-of select='/Response/Quote/Incidents/Incident/cals/cal[@name = "PMP"]'/> 
</charge>

预期 XML:

  <Incident>
      <charge>32.0</charge>
      </Incident>
       <Incident>
      <charge>26.0</charge>
      </Incident>
       <Incident>
      <charge>39.0</charge>
      </Incident>

感谢任何帮助。

I want to show 'cal' element with name PMP exist then take PMP value if not, take TC value.

从字面上看,你会这样做:

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:template match="/Response">
    <root>
        <xsl:for-each select="Quote/Incidents/Incident">
            <Incident>
                <charge>
                    <xsl:variable name="pmp" select="cals/cal[@name='PMP']" />
                    <xsl:choose>
                        <xsl:when test="$pmp">
                            <xsl:value-of select="$pmp/@value"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="cals/cal[@name='TC']/@value"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </charge>
            </Incident>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>

但是如果(如您的示例所示)只有这些元素之一,您可以将代码简化为:

<xsl:template match="/Response">
    <root>
        <xsl:for-each select="Quote/Incidents/Incident">
            <Incident>
                <charge>
                    <xsl:value-of select="cals/cal[@name='PMP']/@value | cals/cal[@name='TC']/@value"/>
                </charge>
            </Incident>
        </xsl:for-each>
    </root>
</xsl:template>

同样,如果两者都可以存在,但 PMP 总是在 TC 之前,你可以这样做:

<xsl:value-of select="(cals/cal[@name='PMP'] | cals/cal[@name='TC'])[1]/@value"/>