>我有一个XMl文件,我需要在其中提取一些信息并汇总值。在 XSL 文件中,我不确定是否需要 for-each 循环。我认为 XSL 将解释我正在尝试做什么。
下面是 XML 文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="uppgift3.xsl"?>
<autoads>
<ad>
<type>1</type>
<name>Honda</name>
<model>XL 1000 V</model>
<regyear>2001</regyear>
<price>129900</price>
<adtext>2001 Honda XL 1000 V, 8.900 km. h?g vindruta. Pris 129.900kr,-. </adtext>
<addate>20020115</addate>
<volume>1000</volume>
<category></category>
</ad>
<ad>
<type>2</type>
<name>Nissan</name>
<model>Almera 1.4S</model>
<regyear>1997</regyear>
<price>119000</price>
<adtext>1997 Nissan Almera 1.4S, 5 d?rrar, met, 70.000 km. el.spegel/f?nster, galv. kaross, c.l?s, startsp?rr, airbag, nedf?llb. baks. ABS, ute temp. R/CD, alarm, d.fte, v.s?ten, s/v-hj. EU-godk. full service, servo. Pris 119.000 kr,-. </adtext>
<addate>20020118</addate>
<volume>0</volume>
<category>5 d?rrrar</category>
</ad>
<ad>
<type>2</type>
<name>Mercedes-Benz</name>
<model>C220 Elegance</model>
<regyear>1995</regyear>
<price>209000</price>
<adtext>1995 Mercedes-Benz C220 Elegance, 4 d?rrar, 88.000 km. skinn, klima/automatic, cruise, el.spegel/f?nster, alu.f?lgar, c.l?s, airbag, antispinn, ABS, ute temp, radio, s/v-hjul, servo, creme skinn. automat. Pris 209.000 kr,-. </adtext>
<addate>20020118</addate>
<volume>0</volume>
<category>4 d?rrar</category>
</ad>
<ad>
<type>2</type>
<name>Audi</name>
<model>S8</model>
<regyear>2000</regyear>
<price>850000</price>
<adtext>2000 Audi S8, 4 d?rrar, 40.000 km. 4x4, Tiptronic-aut., klimataut., ABS, el.f?nster/speglar/s?ten, soltak, c.l?s, servo, airbag, startsp?rr, antispinn, cruise., alu., trnred., sort skinn, mitttarmst?d., sportss?ten, stereo, alarm, s/v-hjul, dragkrok, 17"+18"alu. Pris 850.000 kr,-. </adtext>
<addate>20020118</addate>
<volume>0</volume>
<category>4 d?rrar</category>
</ad>
<ad>
<type>1</type>
<name>Yamaha</name>
<model>Thunderace 1000</model>
<regyear>2000</regyear>
<price>130000</price>
<adtext>2000 Yamaha Thunderace 1000, 11.500 km. R?d/Silver,tankv?ska medf?ljer. 146hk Pris kan diskuteras vid snabb affär Pris 130.000 kr,-. </adtext>
<addate>20020116</addate>
<volume>1000</volume>
<category></category>
</ad>
<ad>
<type>2</type>
<name>Audi</name>
<model>A4 1.6</model>
<regyear>2000</regyear>
<price>260000</price>
<adtext>2000 Audi A4 1.6, 4 d?rrar, 78.000 km. el.f?nster, ABS, alu. airbag, , klima, ESP, navig. plus, TV, evt. inbyte, power box (+30 HK) medf?ljer. Pris 260.000 kr,-. </adtext>
<addate>20020117</addate>
<volume>0</volume>
<category>4 d?rrar</category>
</ad>
<ad>
<type>2</type>
<name>Jeep</name>
<model>Grand Cherokee 2.5TD</model>
<regyear>0</regyear>
<price>359000</price>
<adtext>0 Jeep Grand Cherokee 2.5TD, Stv., 38.000 km. A/C, el.f?mster/spegel, 8xalu., c.l?s, airbag, R/CD, s/v-hjul, servo. Pris 359.000 kr,-. </adtext>
</ad>
<ad>
<type>1</type>
<name>Suzuki</name>
<model>TL 1000 R</model>
<regyear>1998</regyear>
<price>110000</price>
<adtext>1998 Suzuki TL 1000 R, 8400 km. Pris 110.000 kr,-. </adtext>
<addate>20020116</addate>
<volume>1000</volume>
<category></category>
</ad>
<ad>
<type>2</type>
<name>Volkswagen</name>
<model>Golf 1.8 Pink Floyd</model>
<regyear>1994</regyear>
<price>85000</price>
<adtext>1994 Volkswagen Golf 1.8 Pink Floyd, 4 d?rrar, 135.600 km. Eu-godk?nd, airbag, el.taklucka, just ratt, metallic, radio/CD/Kass, s/v-hjul, servicebok, servo, stereo. Fullst?ndig service, 1 ?gare, 6 h?gtalare. Pris 85.000 kr,-. </adtext>
<addate>20020115</addate>
<volume>0</volume>
<category>4 d?rrar</category>
</ad>
<ad>
<type>2</type>
<name>Volvo</name>
<model>850 2.0 LT</model>
<regyear>1997</regyear>
<price>245490</price>
<adtext>1997 Volvo 850 2.0 LT, Stv., 122785 km. halvskinn, el.spegel/f?nster/soltak, fj.c.l?s, st.sp?rr, airbag, just.ratt, rails, nedf.baks闁剁€岆 m.armst?d, ABS, rad/kass, alarm, dragkrok, s/v-hjul, f.glass, met, garanti, servo. Pris 245.490 kr,-. </adtext>
</ad>
<ad>
<type>2</type>
<name>Audi</name>
<model>A3 1.6 Ambition</model>
<regyear>1998</regyear>
<price>180000</price>
<adtext>1998 Audi A3 1.6 Ambition, 3 d?rrar, 88.000 km. s/v-hj. el.f?nster, ABS, f.glass, alu. c.l?s, servo, airbag, rostfri, stilig bil. Pris 180.000 kr,-. </adtext>
<addate>20020117</addate>
<volume>0</volume>
<category>3 d?rrar</category>
</ad>
<ad>
<type>1</type>
<name>BMW</name>
<model>K 1200 RS</model>
<regyear>2000</regyear>
<price>195000</price>
<adtext>2000 BMW K 1200 RS, 4.000 km. sidov?skor, bagageften. Pris 195.000 kr,-. </adtext>
<addate>20020116</addate>
<volume>1200</volume>
<category></category>
</ad>
<ad>
<type>2</type>
<name>Suzuki</name>
<model>Baleno 1.6 GLX</model>
<regyear>1999</regyear>
<price>175000</price>
<adtext>1999 Suzuki Baleno 1.6 GLX, Stv., 49.500 km. A/C, el.f?nster/speil, c.l?s, airbag, rails, ABS, stereo, , servo. Pris 175.000 kr,-. </adtext>
</ad>
<ad>
<type>1</type>
<name>Suzuki</name>
<model>LS 650 Savage</model>
<regyear>1987</regyear>
<price>29000</price>
<adtext>1987 Suzuki LS 650 Savage, r?d. Pris 29.000 kr,-. </adtext>
<addate>20020116</addate>
<volume>650</volume>
<category></category>
</ad>
<ad>
<type>1</type>
<name>Suzuki</name>
<model>GS 500 E</model>
<regyear>1993</regyear>
<price>31900</price>
<adtext>1993 Suzuki GS 500 E. Pris 31.900,-. </adtext>
<addate>20020117</addate>
<volume>500</volume>
<category></category>
</ad>
<ad>
<type>2</type>
<name>BMW</name>
<model>520 i</model>
<regyear>2000</regyear>
<price>387000</price>
<adtext>2000 BMW 520 i, Stv., 52.600 km. Skinn, klima, m.f.ratt, el.spegel/f?nster, alu.f?lgar, c.l?s, airbag x 6, antispinn, rails, trnt, ABS, rad/cd, alarm, dragkrok, met, servo, Pris 387.000 kr,-. </adtext>
</ad>
<ad>
<type>1</type>
<name>Triumph</name>
<model>Daytona T 595</model>
<regyear>1997</regyear>
<price>115000</price>
<adtext>1997 Triumph Daytona T 595, 13.000 km. som ny, karbon anl?gg, nya d?ck, extra tank. Pris 115.000 kr,-. </adtext>
<addate>20020115</addate>
<volume>955</volume>
<category></category>
</ad>
<ad>
<type>2</type>
<name>Mitsubishi</name>
<model>Pajero 2.5 TD</model>
<regyear>1987</regyear>
<price>59000</price>
<adtext>1987 Mitsubishi Pajero 2.5 TD, Stv. 4x4, dragkrok, servo, mycket utr., ingen rost. Pris 59.000 kr,-. </adtext>
</ad>
</autoads>
这是我的 XSL 文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Fordon till salu</h2>
<div>
<xsl:for-each select="autoads/ad">
<xsl:sort select="regyear" order="ascending" />
<div style="margin:15px;">
<xsl:choose>
<xsl:when test="regyear < 1999">
<span style="margin-right:10px;font-weight:bold;float:left;">Total amount of cars before 1999: </span>
<div><xsl:value-of select="sum(//name)" /></div>
<span style="margin-right:5px;font-weight:bold;float:left;">Total amount of cars with automatic gear: </span>
<div><xsl:value-of select="model" /></div>
<span style="margin-right:5px;font-weight:bold;float:left;">Average price for cars before 1999: </span>
<div><xsl:value-of select="sum(//price[regyear < 1999])" /></div>
</xsl:when>
<xsl:otherwise>
<span style="margin-right:10px;font-weight:bold;float:left;">Total amount of cars from 1999 and onwards: </span>
<div><xsl:value-of select="name" /></div>
<span style="margin-right:5px;font-weight:bold;float:left;">Total amount of cars with automatic gear: </span>
<div><xsl:value-of select="model" /></div>
<span style="margin-right:5px;font-weight:bold;float:left;">Average price for cars from 1999 and onwards: </span>
<div><xsl:value-of select="regyear" /></div>
</xsl:otherwise>
</xsl:choose>
</div>
</xsl:for-each>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我已经编辑了我的答案以显示预期结果。感谢您的投入。因此,有了XML文件和XSL文件,我希望结果是:
Total amount of cars before 1999:
Total amount of cars with automatic gear:
Average price for cars before 1999:
Total amount of cars from 1999 and onwards:
Total amount of cars with automatic gear:
Average price for cars from 1999 and onwards:
上述结果尚未计算。再次Tnx!
我正在尝试从XML文件中提供的广告文本中提取信息.
也就是说,我试图提取广告文本中带有"自动机"工作的汽车数量。
对于 1999 <结果应为>= 1999,结果应为 1。
结果应为>
<h2 style="margin:0">Antal bilar med automat växellåda: <xsl:value-of select="count(/autoads/ad[regyear < 1999][type = 2]/adtext[contains(., 'automat')])" /></h2>
我怀疑你实际上得到了一个错误,因为这行
<xsl:value-of select="sum(//name)" />
name
包含一个字符串,而不是一个数字,添加你不能加单词!
如果您只需要总值(例如总和或计数),则不需要xsl:for-each
否则您将在 XML 中获取每辆车的总计。
例如,要获得 1999 年之前的汽车总数,您只需在任何循环之外使用 count
函数
<xsl:value-of select="count(ad[regyear < 1999])" />
(顺便说一下,这假设您当前位于autoads
元素上。
要获得 1999 年之前配备自动齿轮的汽车数量,您将这样做(假设 2 type
是自动的......
<xsl:value-of select="count(ad[regyear < 1999][type = 2])" />
为了获得平均价格,你会这样做。
<xsl:value-of select="sum(ad[regyear < 1999]/price) div count(ad[regyear < 1999])" />
当然,如果你想从1999年开始对汽车做同样的事情,那将是类似的,但你最终会有很多代码重复。因此,您可以使用命名模板来计算总数,并将您希望计为参数的汽车传入。
试试这个 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />
<xsl:template match="/autoads">
<html>
<body>
<h2>Fordon till salu</h2>
<xsl:call-template name="totals">
<xsl:with-param name="ads" select="ad[regyear < 1999]" />
</xsl:call-template>
<xsl:call-template name="totals">
<xsl:with-param name="ads" select="ad[regyear >= 1999]" />
</xsl:call-template>
</body>
</html>
</xsl:template>
<xsl:template name="totals">
<xsl:param name="ads" />
<div style="margin:15px;">
<span style="margin-right:10px;font-weight:bold;float:left;">Total amount of cars before 1999: </span>
<div><xsl:value-of select="count($ads)" /></div>
<span style="margin-right:5px;font-weight:bold;float:left;">Total amount of cars with automatic gear: </span>
<div><xsl:value-of select="count($ads[type = 2])" /></div>
<span style="margin-right:5px;font-weight:bold;float:left;">Average price for cars before 1999: </span>
<div><xsl:value-of select="sum($ads/price) div count($ads)" /></div>
</div>
</xsl:template>
</xsl:stylesheet>