如何正确使用 XSL 和 count() 中的参数从 xml 获取数据
how to use Parameter in XSL and count() correctly to get data from xml
- 我正在尝试通过状态代码传递参数并获取基于状态的订单的详细信息和订单数量我尝试了以下代码但我无法使参数工作在 xsl 中。
- 示例我想传递参数为getState=TX(其中getState是参数名称),它应该只给我TX的相应数据,这同样适用于MO和CA。我仅限于 Xsl 1.0,我也能够获得计数,但它在我需要的元素之外打印,就像这样
- 状态必须作为代码传递,仅表示 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 文件 orders.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>
非常感谢任何改进代码的意见和建议。
我认为在这里做的明智之举是:
- 使用 key 到 select 各州的数据;和
- 将 selected 数据放入一个变量中,这样您就可以使用单个 selection 计算 selected 数据并输出它。
那么你可以简单地做:
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>
- 我正在尝试通过状态代码传递参数并获取基于状态的订单的详细信息和订单数量我尝试了以下代码但我无法使参数工作在 xsl 中。
- 示例我想传递参数为getState=TX(其中getState是参数名称),它应该只给我TX的相应数据,这同样适用于MO和CA。我仅限于 Xsl 1.0,我也能够获得计数,但它在我需要的元素之外打印,就像这样
- 状态必须作为代码传递,仅表示 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 文件 orders.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>
非常感谢任何改进代码的意见和建议。
我认为在这里做的明智之举是:
- 使用 key 到 select 各州的数据;和
- 将 selected 数据放入一个变量中,这样您就可以使用单个 selection 计算 selected 数据并输出它。
那么你可以简单地做:
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>