首页 > 解决方案 > 多个实体处每个循环的 jdeveloper XSLT 中断

问题描述

输入 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 循环中 break 是不可能的。但从以下示例中,如果我想显示名为 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>

任何帮助表示赞赏。

标签: xslt-1.0soajdeveloper-11g

解决方案


我想显示名称为 PMP 的“cal”元素存在,然后取 PMP 值,如果不存在,取 TC 值。

从字面上理解,你会这样做:

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"/>

推荐阅读