- 我正在尝试以下操作,通过状态代码传递参数,并根据状态和订单数量获取订单的详细信息。我尝试了以下代码,但无法使参数在xsl中工作
- 示例:我想将参数传递为getState=TX(其中getState是参数名称(,它应该只为我获取TX的相应数据,这同样适用于MO和CA。我仅限于Xsl 1.0。我也可以获取计数,但它打印在元素之外,我需要它,如下所示<状态名称=";TX";计数";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>
这是用于提取数据订单的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>
任何使代码变得更好的输入和建议都将不胜感激。
我相信在这里做的明智之举是:
- 使用键按状态选择数据;以及
- 将选定的数据放在一个变量中,这样您就可以使用单个选择来计算选定的数据并输出它
然后你可以简单地做:
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>
您想要这样的
<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>