XSLT ..使用包含多个响应的XML的最小值设置变量

时间:2022-08-26 22:38:26

First ... This forum has helped me many, many times in finding answers to my questions without having to ask my own. Thank you to those answering. I promise to do better this time with following this forums protocol.

首先......这个论坛帮助了我很多次,无需问自己的问题就找到了我的问题的答案。谢谢那些回答。我保证这次遵循这个论坛协议会做得更好。

I'm not totally positive that my question is stated well enough so I'll draw it out a bit .....

我并不完全肯定我的问题已经说得好,所以我会把它画出来......

The XML I am getting contains a 'set' of results within the response that each needs to have their own 'minimum' for my importing.

我得到的XML在响应中包含一组“结果”,每个结果都需要为我的导入设置自己的“最小值”。

Here's the very condensed look just to give you an idea:

这是非常浓缩的外观,只是为了给你一个想法:

<?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
  <GetLowestOfferListingsForASINResult ASIN="0470067802" status="Success"></GetLowestOfferListingsForASINResult>
  <GetLowestOfferListingsForASINResult ASIN="0684177722" status="Success"></GetLowestOfferListingsForASINResult>
  <GetLowestOfferListingsForASINResult ASIN="0470052325" status="Success"></GetLowestOfferListingsForASINResult>
  <GetLowestOfferListingsForASINResult ASIN="0470182601" status="Success"></GetLowestOfferListingsForASINResult>
  <GetLowestOfferListingsForASINResult ASIN="0525950869" status="Success"></GetLowestOfferListingsForASINResult>
</GetLowestOfferListingsForASINResponse>

Further more there are actually 3 occurrences of the 'Amount' element within each result returned ....

此外,实际返回的每个结果中有3次'Amount'元素出现....

Currently my variable takes every 'Amount' within the overall response and not from each result set.

目前,我的变量在整体响应中取每个'Amount'而不是每个结果集。

XLST :

XLST:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
xmlns:amz="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" exclude-result-prefixes="amz ns2">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
    <xsl:template match="/">
    <xsl:variable name="MIN_Landed">
        <xsl:for-each select="//amz:Price/amz:LandedPrice/amz:Amount">
            <xsl:sort data-type="number" order="ascending"/>
            <xsl:if test="position()=1"><xsl:value-of select="."/></xsl:if>
        </xsl:for-each>
    </xsl:variable>
    <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
            <ERRORCODE>0</ERRORCODE>
            <PRODUCT BUILD="" NAME="" VERSION=""/>
            <DATABASE DATEFORMAT="M/d/yyyy" LAYOUT="" NAME="" RECORDS="1" TIMEFORMAT="h:mm:ss a"/>
            <METADATA>
                <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="MIN_Landed" TYPE="TEXT"/>
                <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ASIN" TYPE="TEXT"/>
                <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ItemCondition" TYPE="TEXT"/>
                <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ItemSubCondition" TYPE="TEXT"/>
                <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="LandedPrice" TYPE="TEXT"/>
                <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="ListingPrice" TYPE="TEXT"/>
                <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Shipping" TYPE="TEXT"/>
            </METADATA>
            <RESULTSET>
                <xsl:attribute name="FOUND">1</xsl:attribute>
                <xsl:for-each select="amz:GetLowestOfferListingsForASINResponse/amz:GetLowestOfferListingsForASINResult/amz:Product/amz:LowestOfferListings/amz:LowestOfferListing">
                    <ROW>
                        <xsl:attribute name="MODID">0</xsl:attribute>
                        <xsl:attribute name="RECORDID">1</xsl:attribute>
                        <COL>
                            <DATA>
                                <xsl:value-of select="$MIN_Landed"/>
                            </DATA>
                        </COL>
                        <COL>
                            <DATA>
                                <xsl:value-of select="../../amz:Identifiers/amz:MarketplaceASIN/amz:ASIN"/>
                            </DATA>
                        </COL>
                        <COL>
                            <DATA>
                                <xsl:value-of select="amz:Qualifiers/amz:ItemCondition"/>
                            </DATA>
                        </COL>
                        <COL>
                            <DATA>
                                <xsl:value-of select="amz:Qualifiers/amz:ItemSubcondition"/>
                            </DATA>
                        </COL>
                        <COL>
                            <DATA>
                                <xsl:value-of select="amz:Price/amz:LandedPrice/amz:Amount"/>
                            </DATA>
                        </COL>
                        <COL>
                            <DATA>
                                <xsl:value-of select="amz:Price/amz:ListingPrice/amz:Amount"/>
                            </DATA>
                        </COL>
                        <COL>
                            <DATA>
                                <xsl:value-of select="amz:Price/amz:Shipping/amz:Amount"/>
                            </DATA>
                        </COL>
                    </ROW>
                </xsl:for-each>
            </RESULTSET>
        </FMPXMLRESULT>
    </xsl:template>
</xsl:stylesheet>

Here is a test XML ....

这是一个测试XML ....

<?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
    <GetLowestOfferListingsForASINResult  ASIN="0060784776"  status="Success">
        <Product>
            <Identifiers>
                <MarketplaceASIN>
                    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
                    <ASIN>0060784776</ASIN>
                </MarketplaceASIN>
            </Identifiers>
            <LowestOfferListings>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Good</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>8.98</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>4.99</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Collectible</ItemCondition>
                        <ItemSubcondition>Mint</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>8.98</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>4.99</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>VeryGood</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>9.00</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>9.00</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>0.00</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Acceptable</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>9.44</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>5.45</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>New</ItemCondition>
                        <ItemSubcondition>New</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>9.45</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>9.45</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>0.00</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>VeryGood</ItemSubcondition>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>9.45</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>5.46</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>New</ItemCondition>
                        <ItemSubcondition>New</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>9.95</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>9.95</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>0.00</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
            </LowestOfferListings>
        </Product>
    </GetLowestOfferListingsForASINResult>
    <GetLowestOfferListingsForASINResult  ASIN="0140171223"  status="Success">
        <Product>
            <Identifiers>
                <MarketplaceASIN>
                    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
                    <ASIN>0140171223</ASIN>
                </MarketplaceASIN>
            </Identifiers>
            <LowestOfferListings>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Good</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>6.99</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.00</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Acceptable</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>6.99</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.00</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Mint</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>7.49</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.50</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Acceptable</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>8.48</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>4.49</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>New</ItemCondition>
                        <ItemSubcondition>New</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>8.49</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>4.50</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Mint</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>8.78</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>4.79</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Good</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>8.98</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>4.99</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>New</ItemCondition>
                        <ItemSubcondition>New</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>8.99</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>5.00</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
            </LowestOfferListings>
        </Product>
    </GetLowestOfferListingsForASINResult>
    <GetLowestOfferListingsForASINResult  ASIN="014100181X"  status="Success">
        <Product>
            <Identifiers>
                <MarketplaceASIN>
                    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
                    <ASIN>014100181X</ASIN>
                </MarketplaceASIN>
            </Identifiers>
            <LowestOfferListings>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>VeryGood</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>0.00</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Good</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>0.00</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Good</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>0.00</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Good</ItemSubcondition>
                        <FulfillmentChannel>Merchant</FulfillmentChannel>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>4.00</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>0.01</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Good</ItemSubcondition>
                    </Qualifiers>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>4.00</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>0.01</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>USD</CurrencyCode>
                            <Amount>3.99</Amount>
                        </Shipping>
                    </Price>
                </LowestOfferListing>
            </LowestOfferListings>
        </Product>
    </GetLowestOfferListingsForASINResult>
</GetLowestOfferListingsForASINResponse>

1 个解决方案

#1


1  

If I understand you correctly, your problem is that your code is currently finding the lowest Amount value in the document, and you want instead to find the lowest amount in each amz:LowestOfferListing.

如果我理解正确,您的问题是您的代码当前正在查找文档中的最低金额值,而您希望在每个amz中找到最低金额:LowestOfferListing。

You will need to change two things.

你需要改变两件事。

First, instead of setting the value of $MIN_Landed once, you will need to bind that variable once for each result set. So move the xsl:variable declaration into the for-each which will be evaluated once for each result set.

首先,不需要将$ MIN_Landed的值设置一次,而是需要为每个结果集绑定一次该变量。因此,将xsl:variable声明移动到for-each中,每个结果集将对其进行一次计算。

Second, the value you want is the lowest amount within the result set, not the lowest value within the document. So you will need to change the select attribute in the expression that determines the value of the $MIN_Landed variable so that it selects only amz:Amount elements that occur within the current result set. (Hint: start the select expression with './/', not '//'.)

其次,您想要的值是结果集中的最低金额,而不是文档中的最低值。因此,您需要更改表达式中的select属性,该属性确定$ MIN_Landed变量的值,以便它仅选择当前结果集中出现的amz:Amount元素。 (提示:用'.//'开始选择表达式,而不是'//'。)

#1


1  

If I understand you correctly, your problem is that your code is currently finding the lowest Amount value in the document, and you want instead to find the lowest amount in each amz:LowestOfferListing.

如果我理解正确,您的问题是您的代码当前正在查找文档中的最低金额值,而您希望在每个amz中找到最低金额:LowestOfferListing。

You will need to change two things.

你需要改变两件事。

First, instead of setting the value of $MIN_Landed once, you will need to bind that variable once for each result set. So move the xsl:variable declaration into the for-each which will be evaluated once for each result set.

首先,不需要将$ MIN_Landed的值设置一次,而是需要为每个结果集绑定一次该变量。因此,将xsl:variable声明移动到for-each中,每个结果集将对其进行一次计算。

Second, the value you want is the lowest amount within the result set, not the lowest value within the document. So you will need to change the select attribute in the expression that determines the value of the $MIN_Landed variable so that it selects only amz:Amount elements that occur within the current result set. (Hint: start the select expression with './/', not '//'.)

其次,您想要的值是结果集中的最低金额,而不是文档中的最低值。因此,您需要更改表达式中的select属性,该属性确定$ MIN_Landed变量的值,以便它仅选择当前结果集中出现的amz:Amount元素。 (提示:用'.//'开始选择表达式,而不是'//'。)