简体   繁体   English

XSLT-解析具有相同名称的2个元素的XML上的1个元素

[英]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.

相关问题 使用 XSLT 转换对所有元素具有相同名称的 XML - Transforming XML that has same name for all elements using XSLT XSLT:我需要使用顺序相同的元素来解析具有相同元素名称的xml,以便映射到具有不同名称的另一个xml - XSLT : I need to parse the xml with same element name with sequence of order to map in to another xml with different name 对具有相同名称的节点的xml进行XSLT转换 - XSLT transformation for an xml that has nodes with the same name 多个相同元素的XSLT变量元素名称 - XSLT for Variable Element Name for multiple same elements 在XSLT 1.0中,当特定XML元素是多个具有相同名称的元素之一时,如何访问它的属性? - In XSLT 1.0, how can I access attributes of a particular XML element when it is one of multiple elements with the same name? 使用NSXMLparser,如何解析具有相同名称标签但元素不同的xml文件? - using NSXMLparser, how do you parse an xml file which has same name tags but in different elements? 解析 xml 文件并使用 powershell 从具有相同名称的元素中获取值? - Parse a xml file and fetch the value from the elements which has same name using powershell? 使用 SAX 解析器,您如何解析具有相同名称标签但具有不同元素的 xml 文件? - using SAX parser, how do you parse an xml file which has same name tags but in different elements? 使用XSLT删除具有相同名称的XML层次结构元素? - Remove XML hierarchy elements with the same name using XSLT? 基本XML / XSLT - 当存在多个同名元素时的值 - Basic XML/XSLT - value-of when there are multiple elements of the same name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM