Xml 根据XSL中的另一个元素获取元素值
我需要根据之前检索到的价格检索供应商代码 价格是这样收回的Xml 根据XSL中的另一个元素获取元素值,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我需要根据之前检索到的价格检索供应商代码 价格是这样收回的 <xsl:template match="Price"> <xsl:if test="position() = 1"> <xsl:value-of select="."/> </xsl:if> </xsl:template> <xsl:apply-templates select="current-group()/Price">
<xsl:template match="Price">
<xsl:if test="position() = 1">
<xsl:value-of select="."/>
</xsl:if>
</xsl:template>
<xsl:apply-templates select="current-group()/Price">
<xsl:sort select="." data-type="text" order="ascending"/>
</xsl:apply-templates>
我能做些什么来获取该价格的供应商代码吗
<xsl:variable name="SupplierCode" select="current-group()/SupplierCode"/>
XML示例
<Search-Request search-term="1 tb">
<Items>
<Item href="SEA1TBST31000524AS.jpg" model="ST31000524AS">
<Name>Seagate Hard disk 1TB ST31000524AS 3.5"</Name>
<Price>60.50</Price>
<SupplierCode>TECSEA1TB</SupplierCode>
<Supplier>TEC001</Supplier>
<Manufacturer>Seagate</Manufacturer>
</Item>
<Item href="" model="ST31000524AS">
<Name>Seagate Hard disk 1TB ST31000524AS 3.5 inch</Name>
<Price>55.50</Price>
<SupplierCode>SCASEA1TB</SupplierCode>
<Supplier>SCA001</Supplier>
<Manufacturer>Seagate</Manufacturer>
</Item>
</Items>
</Search-Request>
希捷硬盘1TB ST31000524AS 3.5“
60.50
TECSEA1TB
TEC001
希捷
希捷硬盘1TB ST31000524AS 3.5英寸
55.50
SCASEA1TB
SCA001
希捷
此外,我正在以以下方式分组
<xsl:for-each-group select="Items/Item" group-by="@model">
对于每个“型号”,您都在尝试为该型号找到最便宜的商品
要获取当前价格元素的供应商代码,您可以这样做
<xsl:template match="Price">
<xsl:if test="position() = 1">
<xsl:value-of select="."/>
<xsl:value-of select="../SupplierCode" />
</xsl:if>
</xsl:template>
然后,您可以有一个与项目元素匹配的模板,在这个模板中,您可以轻松访问价格和代码
<xsl:apply-templates select="current-group()">
<xsl:sort select="Price" data-type="number" order="ascending"/>
</xsl:apply-templates>
<xsl:template match="Item">
<xsl:if test="position() = 1">
<xsl:value-of select="Price" />
<xsl:value-of select="Code" />
</xsl:if>
</xsl:template>
编辑:在回答您的评论时,如果您只需要最低价格的商品的SupplierCode,您可以更改与Item元素匹配的模板,以仅输出供应商代码,然后将xsl:apply templates包装在一个变量中
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/*">
<xsl:for-each-group select="Items/Item" group-by="@model">
<xsl:variable name="SupplierCode">
<xsl:apply-templates select="current-group()">
<xsl:sort select="Price" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:value-of select="$SupplierCode" />
</xsl:for-each-group>
</xsl:template>
<xsl:template match="Item">
<xsl:if test="position() = 1">
<xsl:value-of select="SupplierCode"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
实际上,还有另一种方法,可以使用XSLT2.0中的min操作符
<xsl:value-of select="current-group()[number(Price)=min(current-group()/Price)]/SupplierCode" />
但是,这不一定像前面的方法那样有效,因为您正在有效地检查“current-group()“两次。首先获取最小值,然后再次在当前组中找到价格最低的商品。我们可以查看源XML吗?很抱歉,我可能会问一些愚蠢的问题,但是我如何检索供应商代码呢?我已经扩展了我的答案,希望能让它更清楚。