- 我正在嘗試創建一個 xml 檔案,該檔案為我提供如下預期的輸出,因此基本上我的樣式表基于以下幾個條件從 xml 中提取資料。
- 應該通過傳入 cusid 作為引數來獲取輸出,所以如果我傳入 cusid= a1234。(詳情請參考隨附的xml檔案)
- 預期輸出如下,它應該在單個根節點中,這意味著客戶如下:
<?xml version="1.0" encoding="UTF-8"?>
<customer name="Joe Malone"
state="AZ"
zip="45643"
orders="2"
number_items="8"/>
我正在使用 XSL:1.0
- 從上面的輸出中,訂單是訂單的數量,因此對于 cusid= a1234(來自 xml 檔案),我們有 2 個訂單,而 number_items 是該特定 cusid 的數量(訂單/訂單/庫存/數量)的總和。
- 因此,如果我傳遞另一個 cusid=z5678 作為引數,我應該獲得該 cusid 的相應詳細資訊
- 也附上我的嘗試,從中我可以獲取名稱、狀態和 zip,但它為給定的 cusid 顯示兩次,我不確定我的邏輯是否不正確(for 回圈)。
- 還附加了使計數和求和函式作業的嘗試,但沒有得到我想要的結果。我被困在這一點上。任何解決此問題的輸入都非常感謝。
這是我的 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>
uj5u.com熱心網友回復:
我相信你可以這樣做:
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 中都是唯一的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/379717.html
