I get the wrong output of my XSLT-file

Here should go questions about transforming XML with XSLT and FOP.
rina98
Posts: 1
Joined: Fri Jul 29, 2022 1:59 pm

I get the wrong output of my XSLT-file

Post by rina98 »

Hey, I am trying to calculate the percentage of elements in my data, but get the incorrect output. I suppose, it is something wrong with the path, so maybe someone can help me.
Here is the XML-file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
	<pc:cars
		xmlns:pc="some-url-for-pc"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="file-on-my-pc.xsd"
		version="0.1">

		<pc:car url="https://link"
			name="Seat Leon"
			price="11.700"
			year="2015">
			<pc:main_descriptions quantity_owner="2" 
				quantity_of_kilometer="84496">
</pc:main_descriptions>
</pc:car>
</pc:cars>
So my XSLT-file tried to calculate how many cars have the normal quantity of kilometres and how much percent have an excess of kilometres, but it doesn't work. Here is my XSLT-file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pc="some-url-for-pc"
exclude-result-prefixes="pc"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/pc:cars">

<xsl:variable name="AmountOfCars" select="count(//pc:car)"/>
<xsl:variable name="Standart" select="16000"/>
<xsl:variable name="high" select="count(pc:car[@quantity_of_kilometer &gt; $Standart])" />
<xsl:variable name="low" select="count(pc:car[@quantity_of_kilometer &lt; $Standart])" />
<html>
	<head>
		<title>The percentage of kilometres </title>
	</head>
	<body>
		<h1>The percentage of kilometres </h1>
		<result>
			<highResult>
				<xsl:value-of select="format-number($high div $AmountOfCars, '#%')" />
			</highResult>
			<lowResult>
				<xsl:value-of select="format-number($low div $AmountOfCars, '#%')" />
			</lowResult>
		</result>
	</body>
</html>
</xsl:template>
</xsl:stylesheet>
Martin Honnen
Posts: 97
Joined: Tue Aug 19, 2014 12:04 pm

Re: I get the wrong output of my XSLT-file

Post by Martin Honnen »

You don't have the right XPath expressions to select the attribute, should be

Code: Select all

<xsl:variable name="high" select="count(pc:car[pc:main_descriptions/@quantity_of_kilometer &gt; $Standart])" />
<xsl:variable name="low" select="count(pc:car[pc:main_descriptions/@quantity_of_kilometer &lt; $Standart])" />
instead.
rina98
Posts: 1
Joined: Fri Jul 29, 2022 1:59 pm

Re: I get the wrong output of my XSLT-file

Post by rina98 »

Martin Honnen wrote: Fri Jul 29, 2022 4:11 pm You don't have the right XPath expressions to select the attribute, should be

Code: Select all

<xsl:variable name="high" select="count(pc:car[pc:main_descriptions/@quantity_of_kilometer &gt; $Standart])" />
<xsl:variable name="low" select="count(pc:car[pc:main_descriptions/@quantity_of_kilometer &lt; $Standart])" />
instead.
Maybe you can also help me with this problem. I want to compare it with the normal state of kilometres by cars (so it equals 18000 for example). So I wanted to calculate it by every car in my data ( something like this: expectation = (2022-year) *18000) and then compare each car with this number. If a car has a kilometre higher than expected that it belongs to the first group with the high kilometres state, if not then the second group. The code is also below, but I have an error message: Cannot convert string "" to double. Maybe you can advise me on what am I doing wrong.

Code: Select all

<xsl:variable name="AmountOfCars" select="count(//pc:auto)"/>
<xsl:variable name="expectation" select="(2022-@year) * 18000" />
<xsl:variable name="high">
   <xsl:if test="count(pc:car[pc:main_descriptions/@quantity_of_kilometer &gt; $expectation])" />
</xsl:variable>

<xsl:variable name="low">
   <xsl:if test="count(pc:car[pc:main_descriptions/@quantity_of_kilometer &lt; $expectation])" />
</xsl:variable>
....
<html>
<body>
<result>
			<low_result>
				<xsl:value-of select="format-number($low div $AmountOfCars, '#%')" />
			</low_result>
			<high_result>
				<xsl:value-of select="format-number($high div $AmountOfCars, '#%')" />
			</high_result>
		</result>
	</body>
</html>
Last edited by rina98 on Sun Jul 31, 2022 12:26 pm, edited 1 time in total.
Post Reply