如何解决排序后从下一个现有元素获取价值
我有这个xml
:
<ns4:Dmpp ProductId="oLRbYoE+VfG=" deliveryEnvironment="P" code="1482223" codeType="CNK">
<ns4:Data from="2020-03-01">
<Reimbursable>false</Reimbursable>
</ns4:Data>
<ns4:Data from="2012-06-01" to="2016-07-14">
<Price>6.3100</Price>
<Reimbursable>false</Reimbursable>
</ns4:Data>
<ns4:Data from="2016-07-15" to="2019-12-11">
<Reimbursable>false</Reimbursable>
</ns4:Data>
<ns4:Data from="2019-12-12" to="2020-02-29">
<Price>7.5000</Price>
<Reimbursable>true</Reimbursable>
</ns4:Data>
</ns4:Dmpp>
有了这个xsl
<xsl:for-each select="ns4:Dmpp">
<xsl:text>INSERT INTO DMPPACKAGING VALUES ('</xsl:text>
<xsl:value-of select="@ProductId" />
<xsl:text>','</xsl:text>
<xsl:value-of select="@deliveryEnvironment" />
<xsl:text>','</xsl:text>
<xsl:for-each select="ns4:Data">
<xsl:sort select="translate(@from,'-','')" order="descending" data-type="number" />
<xsl:if test="position() = 1">
<xsl:value-of select="@from" />
<xsl:text>','</xsl:text>
<xsl:choose>
<xsl:when test="@to !=''">
<xsl:value-of select="@to"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'0000-00-00'" />
</xsl:otherwise>
</xsl:choose>
<xsl:text>','</xsl:text>
<xsl:value-of select="../@codeType" />
<xsl:text>','</xsl:text>
<xsl:value-of select="../@code" />
<xsl:text>','</xsl:text>
<xsl:value-of select="Reimbursable" />
<xsl:text>','</xsl:text>
<xsl:choose>
<xsl:when test="Price !=''">
<xsl:value-of select="Price"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="../ns4:Data/following-sibling::ns4:Data/Price" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:for-each>
<xsl:text>');</xsl:text>
</xsl:for-each>
在此示例中您可以看到,Position() = 1
仅给我<Reimbursable>
元素的数据。
我想了解更多有关产品价格的信息。 对于我的代码,只有在排序元素的第二个元素中才可以得到它,但并非总是如此。
任何帮助将不胜感激!
解决方法
实际上,我的建议是:
<xsl:text>MOST RECENT REIMBURSABLE: </xsl:text>
<xsl:for-each select="ns4:Data[Reimbursable]">
<xsl:sort select="@from" order="descending" />
<xsl:if test="position() = 1">
<xsl:value-of select="@from"/>
</xsl:if>
</xsl:for-each>
<xsl:text> MOST RECENT PRICE: </xsl:text>
<xsl:for-each select="ns4:Data[Price]">
<xsl:sort select="@from" order="descending" />
<xsl:if test="position() = 1">
<xsl:value-of select="@from"/>
</xsl:if>
</xsl:for-each>
,
这是我通过@ michael.hor257k的建议解决问题的方法:
我只会将代码放在<xsl:choose>
里面,在那里我需要获取更新的Price
元素。
<xsl:choose>
<xsl:when test="Price !=''">
<xsl:value-of select="Price"/>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="../ns4:Data/Price">
<xsl:sort select="translate(../@from,'-','')" order="descending" data-type="number" />
<xsl:if test="position() = 1">
<xsl:value-of select="." />
</xsl:if>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。