[英]XSLT - parse 1 element on XML that has 2 elements same name
I'm trying to get a value from an element, problem is that the element can appear several times and is not nested within another element that I need to compare. 我试图从一个元素中获取一个值,问题是该元素可以出现几次并且没有嵌套在我需要比较的另一个元素中。
Here's part of my XML: 这是我的XML的一部分:
<sim_pur_pric>
<spp_code>001</spp_code>
<spp_price>213.0136</spp_price>
<spp_unit>ea</spp_unit>
<spp_curr>USD</spp_curr>
<spp_cost_comp>001</spp_cost_comp>
</sim_pur_pric>
<sim_pur_pric>
<spp_code>005</spp_code>
<spp_price>212.498553</spp_price>
<spp_unit>ea</spp_unit>
<spp_curr>USD</spp_curr>
<spp_cost_comp>001</spp_cost_comp>
</sim_pur_pric>
<storage_conditions />
<cust_po />
What I need to get is the value of spp_price but only for spp_code equal to 001, however as you can see the element sim_pur_pric appears twice for different spp_code. 我需要获取的是spp_price的值,但仅适用于等于001的spp_code,但是您会看到元素sim_pur_pric对于不同的spp_code出现了两次。
This is what I have on my xsl (part of it): 这就是我在xsl上所拥有的(部分):
<xsl:template match="sim_pur_pric">
<xsl:choose>
<xsl:when test="spp_code"='001'">
<xsl:choose>
<xsl:when test="string-length(spp_price) != 0">
<xsl:value-of select='spp_price'/><xsl:text>|</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>0|</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:text>0|</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
However it does not work... :( does any of you experimented guys know how can I get the value I want? I appreciate your time and help 但是,它不起作用... :((你们中有没有经过实验的人知道我如何获得自己想要的价值?我感谢您的时间和帮助
One last thing, it has to be XSL Version 1.0 since I'm parsing on Unix with xsltproc 最后一件事,它必须是XSL版本1.0,因为我正在使用xsltproc在Unix上进行解析
The issue that you have can only be fixed from outside of your "sim_pur_pric" template. 您只能从“ sim_pur_pric”模板之外解决此问题。 Currently, it applies to both "sim_pur_pric" nodes. 当前,它适用于两个“ sim_pur_pric”节点。 You need XPath to test for the existence of a valid "sim_pur_pric" node. 您需要XPath来测试是否存在有效的“ sim_pur_pric”节点。
<xsl:apply-template match="sim_pur_pric">
Is replaced with something like this: 被替换为以下内容:
<xsl:choose>
<xsl:when test="sim_pur_price[spp_code='001' and string-length(spp_price) != 0]">
<xsl:value-of select="sim_pur_price[spp_code='001']"/><xsl:text>|</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>0|</xsl:text>
</xsl:otherwise>
</xsl:choose>
I hope this helps. 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.