我正在尝试创建 XSL sheet Sum 和 Count 函数不会从嵌套 xml 中获取结果

Im trying to create a XSL sheet Sum and Count function doesn't fetch result from a nested xml

  1. 我正在尝试创建一个 xml 文件,它给我预期的输出如下,所以基本上我的风格 sheet 根据以下几个条件从 xml 中提取数据.
  2. 应该通过将 cusid 作为参数传递来获取输出,所以如果我传递 cusid= a1234。 (详情请参考附件xml文件)
  3. 预期输出如下,它应该在单个根节点中,这意味着客户如下:

    <?xml version="1.0" encoding="UTF-8"?>
<customer name="Joe Malone"
          state="AZ"
          zip="45643"
          orders="2"
          number_items="8"/>
我正在使用 XSL:1.0

  1. 上面的输出是订单的数量,所以对于 cusid=a1234(来自 xml 文件)我们有 2 个订单,number_items 是数量的总和(orders/order/inventory/quantity) 对于那个特定的 cusid。
  2. 因此,如果我将另一个 cusid=z5678 作为参数传递,我应该获得该 cusid 的相应详细信息
  3. 附上我的尝试,我可以从中获得名称、状态和 zip 但是它针对给定的 cusid 显示两次,我不确定我的逻辑是否不正确(for 循环)。
  4. 还附上了使计数和求和函数起作用但未获得所需结果的尝试。我被困在这一点上。非常感谢任何解决此问题的意见。

这是我的 amazon.xsl 文件

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="cusid"/>
    <xsl:key name="data-by-cusid" match="customer" use="@cusid" />
        <xsl:template match="customers/customer">
            <xsl:variable name="customer-data" select="key('data-by-cusid', $cusid)" />
                         <xsl:for-each select="$customer-data">
                                <customer name= "{name}" state ="{state}" zip="{zip}" />
                         </xsl:for-each>
        </xsl:template>
</xsl:stylesheet>
这是 xml 文件 amazonorder.xml

<?xml version="1.0" encoding="UTF-8"?>
<customers>
    <customer cusid="a1234">
        <name>Joe Malone</name>
        <state>AZ</state>
        <zip>45643</zip>
        <orders>
            <order oid="44470">
                <date>11/2/2021</date>
                <inventory id="p5148">
                    <description>Football</description>
                    <quantity>2</quantity>
                </inventory>
                <inventory id="sb2818">
                    <description>camping equipment</description>
                    <quantity>2</quantity>
                </inventory>
                <inventory id="c1215">
                    <description>light bulbs</description>
                    <quantity>2</quantity>
                </inventory>
            </order>
            <order oid="23421">
                <date>10/15/2020</date>
                <inventory id="lcb8876">
                    <description>Pillows</description>
                    <quantity>1</quantity>
                </inventory>
                <inventory id="bc9976">
                    <description>Mattress</description>
                    <quantity>1</quantity>
                </inventory>
            </order>
        </orders>
    </customer>
        <customer cusid="z5678">
            <name>Brandy Mccarthy</name>
            <state>CA</state>
            <zip>60144</zip>
            <orders>
                <order oid="12778">
                    <date>3/20/2021</date>
                    <inventory id="q4170">
                        <description>basketball</description>
                        <quantity>6</quantity>
                    </inventory>
                    <inventory id="cv6334">
                        <description>Shirts</description>
                        <quantity>2</quantity>
                    </inventory>
                    <inventory id="f7665">
                        <description>joggers</description>
                        <quantity>2</quantity>
                    </inventory>
                </order>
                <order oid="35679">
                    <date>8/8/2021</date>
                    <inventory id="mnc9933">
                        <description>camera</description>
                        <quantity>8</quantity>
                    </inventory>
                    <inventory id="zx1154">
                        <description>lamp</description>
                        <quantity>3</quantity>
                    </inventory>  
                    <inventory id="yu1484">
                        <description>bag</description>
                        <quantity>4</quantity>
                    </inventory>
                </order>
            </orders>
        </customer>
    </customers>

这是我对计数和求和函数的尝试,但它不起作用(给出编译错误)

 <xsl:template match="orders/order">
                <order count = "{count($data-by-orderid)}">
           <xsl:for-each select = "$data-by-orderid" >
               <xsl:select oid = "{$oid}" />
        </xsl:for-each>
                </order>
        </xsl:template> 

    <!--This is how I attempted to make the sum  work but this did not  workout for me to get the output as expected which I mentioned at the start of the post-->
        <xsl:template match="orders/order/inventory">
        <xsl:for-each select="orders/order/inventory">
            <Customer> 
                <TotalPurchase> 
                    <xsl:value-of select="sum(orders/order/inventory/quantity)"/>
                </TotalPurchase>
            </Customer>
        </xsl:for-each>
    </xsl:template>

我相信你可以做到这一点:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="cusid"/>

<xsl:key name="data-by-cusid" match="customer" use="@cusid" />

<xsl:template match="/">
    <xsl:variable name="customer" select="key('data-by-cusid', $cusid)" />
    <xsl:variable name="orders" select="$customer/orders/order" />
    <customer name="{$customer/name}" state="{$customer/state}" zip="{$customer/zip}" orders="{count($orders)}" number_items="{sum($orders/inventory/quantity)}"/>
</xsl:template>

</xsl:stylesheet>

请注意,我们假设每个客户在输入中都是唯一的 XML。