我尝试创建 XSL 工作表总和和计数函数无法从嵌套的 xml 中获取结果


  1. 我正在尝试创建一个xml文件,该文件为我提供如下所示的预期输出,因此基本上,我的样式表基于以下几个条件从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. 从上面的输出中,orders是订单的计数,因此对于cusid=a1234(来自xml文件(,我们有2个订单,number_items是该特定cusid的数量(订单/订单/库存/数量(之和
  2. 因此,如果我传递另一个cusid=z5678作为参数,我应该得到该cusid的相应细节
  3. 附加我的尝试,我可以从中获得名称、状态和zip,但它为给定的cusid显示了两次,我不确定我的逻辑是否不正确(对于循环(
  4. 还附加了使count和sum函数工作的尝试,但没有获得所需的结果。我陷入了困境。任何解决这一问题的投入都将不胜感激

这是我的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中都是唯一的。

相关内容

  • 没有找到相关文章

最新更新