xslt-1.0 - 多个实体处每个循环的 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>
任何帮助表示赞赏。
解决方案
我想显示名称为 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"/>
推荐阅读
- javascript - 当我使用 env 文件时,当我阅读 vars 时我得到了未定义
- css - 如何使用 CSS 相对于标题定位我的元素
- flutter - 颤振:从颤动的列表视图中删除带有删除图标的小部件
- flutter - 在有像 1,00,000 这样的大量孩子的列表视图中使用 AutomaticKeepAliveClientMixin 是否很好?
- javascript - 如何在 Javascript/Vue 中创建双 IF 条件?
- javascript - 我想在 ReactJs 的 onClick 中有 2 种方法,但它不起作用
- reactjs - 在 React 中使用 Slick.js 与“infinite: true”,幻灯片被渲染多次
- css - 如何删除活动选项卡下的边框
- rest - 如何在 Insomnia 上禁用主机名验证
- java - 使用给定的模数和指数加密字符串