- 我正在嘗試通過狀態代碼傳遞引數并根據狀態和訂單數量獲取訂單的詳細資訊我嘗試了以下代碼但我無法使引數在 xsl 中作業。
- 示例我想將引數作為 getState=TX (其中 getState 是引數名稱)傳遞,它應該只為我獲取 TX 的相應資料,這同樣適用于 MO 和 CA。我僅限于 Xsl 1.0 我也能得到計數但它列印在元素之外我需要它像這樣<state name="TX" count "2">
- 狀態必須作為僅表示 TX,CA,MO 的代碼傳遞(作為引數而不是全名) java -jar saxon-he-10.5.jar -xsl:walmart.xsl -s:orders.xml -o: testoutput.xml getState=TX -> 任何人都可以確認這是在生成輸出檔案時應該如何傳遞嗎?
- 輸出我目前的樣子,它按日期的降序排序。任何人都可以請教如何實作上述目標(計數和引數)。
當前輸出檔案如下所示
<?xml version="1.0" encoding="UTF-8"?>
<details>
<state name="TX">count:2
<order date="2021-08-15"
item="onions"
address="amway avenue"
phone="(432) 666-7890"
inventory="9976454"/>
<order date="2021-10-11"
item="cereal"
address="34 main st"
phone="(212) 566-7670"
inventory="0247556"/>
</state>
<state name="CA">count:2
<order date="2021-02-19"
item="eggs"
address="Audobon st"
phone="(232) 456-3211"
inventory="0244559"/>
<order date="2021-05-13"
item="brocolli"
address="47 apartment"
phone="(444) 564-3433"
inventory="3434654"/>
</state>
<state name="MO">count:2
<order date="2021-02-27"
item="fries"
address="paseo blvd"
phone="(309) 123-5644"
inventory="2245526"/>
<order date="2021-12-04"
item="juice"
address="locust st"
phone="(309) 566-5555"
inventory="2245556"/>
</state>
</details>
walmart.xsl 這是我試過的 xsl 檔案
<?xml version="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="getState" select="response/walmart"/>
<xsl:template match="/">
<xsl:element name="details">
<state name="TX">
count:<xsl:value-of select= "count(response/walmart[state = 'TX'])"/>
<xsl:apply-templates select="response/walmart[state = 'TX']">
<xsl:sort select="order_date" order="ascending"/>
</xsl:apply-templates>
</state>
<state name="CA">
count: <xsl:value-of select= "count(response/walmart[state = 'CA'])"/>
<xsl:apply-templates select="response/walmart[state = 'CA']">
<xsl:sort select="order_date" order="ascending"/>
</xsl:apply-templates>
</state>
<state name="MO">
count:<xsl:value-of select= "count(response/walmart[state = 'MO'])"/>
<xsl:apply-templates select="response/walmart[state = 'MO']">
<xsl:sort select="order_date" order="ascending"/>
</xsl:apply-templates>
</state>
</xsl:element>
</xsl:template>
<xsl:template match="walmart">
<order date="{order_date}" item="{item}" address="{walmart_address}" phone ="{walmart_contact_phone}" inventory="{inventory_number}" >
</order>
</xsl:template>
</xsl:stylesheet>
這是拉取資料orders.xml的xml檔案
<?xml version="1.0"?>
<response>
<walmart>
<order_date>2021-10-11</order_date>
<item>cereal</item>
<state>TX</state>
<walmart_address>34 main st</walmart_address>
<walmart_contact_phone>(212) 566-7670</walmart_contact_phone>
<inventory_number>0247556</inventory_number>
</walmart>
<walmart>
<order_date>2021-05-13</order_date>
<item>brocolli</item>
<state>CA</state>
<walmart_address>47 apartment</walmart_address>
<walmart_contact_phone>(444) 564-3433</walmart_contact_phone>
<inventory_number>3434654</inventory_number>
</walmart>
<walmart>
<order_date>2021-08-15</order_date>
<item>onions</item>
<state>TX</state>
<walmart_address>amway avenue</walmart_address>
<walmart_contact_phone>(432) 666-7890</walmart_contact_phone>
<inventory_number>9976454</inventory_number>
</walmart>
<walmart>
<order_date>2021-02-19</order_date>
<item>eggs</item>
<state>CA</state>
<walmart_address>Audobon st</walmart_address>
<walmart_contact_phone>(232) 456-3211</walmart_contact_phone>
<inventory_number>0244559</inventory_number>
</walmart>
<walmart>
<order_date>2021-12-04</order_date>
<item>juice</item>
<state>MO</state>
<walmart_address>locust st</walmart_address>
<walmart_contact_phone>(309) 566-5555</walmart_contact_phone>
<inventory_number>2245556</inventory_number>
</walmart>
<walmart>
<order_date>2021-02-27</order_date>
<item>fries</item>
<state>MO</state>
<walmart_address>paseo blvd</walmart_address>
<walmart_contact_phone>(309) 123-5644</walmart_contact_phone>
<inventory_number>2245526</inventory_number>
</walmart>
</response>
任何使代碼更好的輸入和建議都非常感謝。
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="getState"/>
<xsl:key name="data-by-state" match="walmart" use="state" />
<xsl:template match="/response">
<xsl:variable name="state-data" select="key('data-by-state', $getState)" />
<details>
<state name="{$getState}" count="{count($state-data)}">
<xsl:for-each select="$state-data">
<xsl:sort select="order_date"/>
<order date="{order_date}" item="{item}" address="{walmart_address}" phone ="{walmart_contact_phone}" inventory="{inventory_number}" />
</xsl:for-each>
</state>
</details>
</xsl:template>
</xsl:stylesheet>
當使用引數呼叫此樣式表時getState="TX",結果將是:
<?xml version="1.0" encoding="UTF-8"?>
<details>
<state name="TX" count="2">
<order date="2021-08-15" item="onions" address="amway avenue" phone="(432) 666-7890" inventory="9976454"/>
<order date="2021-10-11" item="cereal" address="34 main st" phone="(212) 566-7670" inventory="0247556"/>
</state>
</details>
uj5u.com熱心網友回復:
你想要這樣的東西
<xsl:template match="/">
<details>
<xsl:variable name="selection"
select="response/walmart[state=$getState]"/>
<state name="{$getState}" count="{count($selection)}">
<xsl:apply-templates select="$selection">
<xsl:sort select="order_date" order="ascending"/>
</xsl:apply-templates>
</state>
</details>
</xsl:template>
<xsl:template match="walmart">
<order date="{order_date}"
item="{item}"
address="{walmart_address}"
phone="{walmart_contact_phone}"
inventory="{inventory_number}" />
<order>
</xsl:template>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/378876.html
下一篇:根元素的MarshalXML
