Reputation: 31
didn't know how to correctly name topic to specify my problem :) My XML database looks like this:
<data>
<prices>
<ROW>
<COLUMN NAME="product_id">00932932</COLUMN>
<COLUMN NAME="price">56</COLUMN>
... some other not irrelevant columns
</ROW>
... other rows with the same columns
</prices>
</data>
I'm trying to select the ones with price higher than 30 and colour them green. But it marks all of the prices as green, as if the condition was true to all of them. Condition looks like this:
<xsl:if test='/data/prices/ROW[COLUMN[@NAME="price"] > 30]' >
<span style="color: green"><xsl:value-of select='/data/prices/ROW[COLUMN[@NAME="EXPIRATION_DATE"]="" and COLUMN[@NAME="PRODUCT_ID"]=current()/COLUMN[@NAME="PRODUCT_ID"] ]/COLUMN[@NAME="PRICE"]'/></span>
</xsl:if>
<xsl:if test='/data/prices/ROW[COLUMN[@NAME="PRICE"] < 30]' >
<xsl:value-of select='/data/prices/ROW[COLUMN[@NAME="EXPIRATION_DATE"]="" and COLUMN[@NAME="PRODUCT_ID"]=current()/COLUMN[@NAME="PRODUCT_ID"] ]/COLUMN[@NAME="PRICE"]'/>
</xsl:if>
Any help would be appreciated :)
Upvotes: 1
Views: 887
Reputation: 1
Thanks Dimitre but it's still the same. All prices are green. I'm posting the whole code. I guess that would be better:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-public='-//W3C//DTD XHTML 1.0 Strict//EN'doctype-system='http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Zadanie 21</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<table style="border-style: solid">
<tr style="border-style: solid">
<td style="border-style: solid">ID</td>
<td style="border-style: solid">Produkt</td>
<td style="border-style: solid">Cena</td>
</tr>
<xsl:for-each select='/dane/produkty/ROW'>
<tr style="border-style: solid">
<td style="border-style: solid">
<xsl:value-of select='COLUMN[@NAME="ID_PRODUKTU"]'/>
</td>
<td style="border-style: solid">
<xsl:value-of select='COLUMN[@NAME="OPIS"]'/>
</td>
<td style="border-style: solid">
<xsl:if test='/dane/ceny/ROW[COLUMN[@NAME="CENA_CENNIKOWA"] > 30] ' >
<span style="color: green"><xsl:value-of select='/dane/ceny/ROW[COLUMN[@NAME="DATA_WYCOFANIA"]="" and COLUMN[@NAME="ID_PRODUKTU"]=current()/COLUMN[@NAME="ID_PRODUKTU"] ]/COLUMN[@NAME="CENA_CENNIKOWA"]'/></span>
</xsl:if>
<xsl:if test='/dane/ceny/ROW[COLUMN[@NAME="CENA_CENNIKOWA"] < 31]' >
<xsl:value-of select='/dane/ceny/ROW[COLUMN[@NAME="DATA_WYCOFANIA"]="" and COLUMN[@NAME="ID_PRODUKTU"]=current()/COLUMN[@NAME="ID_PRODUKTU"] ]/COLUMN[@NAME="CENA_CENNIKOWA"]'/>
</xsl:if>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
Upvotes: 0
Reputation: 243449
Answer to the reply-question by matyo35:
<xsl:if test='/dane/ceny/ROW[COLUMN[@NAME="CENA_CENNIKOWA"] ] ' > <span style="color: green"><xsl:value-of select='/dane/ceny/ROW[COLUMN[@NAME="DATA_WYCOFANIA"]="" and COLUMN[@NAME="ID_PRODUKTU"]=current()/COLUMN[@NAME="ID_PRODUKTU"] ] /COLUMN[@NAME="CENA_CENNIKOWA"]'/></span> </xsl:if>
The condition in the test
attributr is always satisfied if there is even a single Row
that has a child COLUMN[@NAME="CENA_CENNIKOWA"]
, because the expression is absolute -- it doesn't depend on the current node, matched by the template.
Correct way (I guess -- due to no XML document having been provided) of specifying the condition:
<xsl:if test=
'/dane/ceny/ROW[COLUMN[NAME="ID_PRODUKTU"]=current()/COLUMN[NAME="ID_PRODUKTU"]
and COLUMN[@NAME="CENA_CENNIKOWA"] > 30]'>
Upvotes: 0
Reputation: 1
My friend didn't show everything (let's call it that way). We have to write a script to (what my friend said before) colour prices higher than 30. This code is working. It shows everything what we need:
<xsl:for-each select='/dane/produkty/ROW'>
<tr style="border-style: solid">
<td style="border-style: solid">
<xsl:value-of select='COLUMN[@NAME="ID_PRODUKTU"]'/>
</td>
<td style="border-style: solid">
<xsl:value-of select='COLUMN[@NAME="OPIS"]'/>
</td>
<td style="border-style: solid">
<xsl:if test='/dane/ceny/ROW[COLUMN[@NAME="CENA_CENNIKOWA"] ] ' >
<span style="color: green"><xsl:value-of select='/dane/ceny/ROW[COLUMN[@NAME="DATA_WYCOFANIA"]="" and COLUMN[@NAME="ID_PRODUKTU"]=current()/COLUMN[@NAME="ID_PRODUKTU"] ]/COLUMN[@NAME="CENA_CENNIKOWA"]'/></span>
</xsl:if>
<!--<xsl:if test='/dane/ceny/ROW[COLUMN[@NAME="CENA_CENNIKOWA"] < "31"]' >
<xsl:value-of select='/dane/ceny/ROW[COLUMN[@NAME="DATA_WYCOFANIA"]="" and COLUMN[@NAME="ID_PRODUKTU"]=current()/COLUMN[@NAME="ID_PRODUKTU"] ]/COLUMN[@NAME="CENA_CENNIKOWA"]'/>
</xsl:if>-->
</td>
</tr>
</xsl:for-each>
This will colour every price but we have to use condition.
Upvotes: 0
Reputation: 70618
The problem is probably that your xsl:if is testing if any ROW has row has a price greater than 30, regardless of what your current context is (i.e regardless of what ROW you are currently positioned on). The / at the start of the xpath expression means you start the search right back at the document node, not the current node.
Assuming you are selecting all rows with a template, like so (or maybe you are doing an xsl:for-each
<xsl:apply-templates select="ROW">
Then, within your template that matches the ROW, you would code your xsl:if like so
<xsl:if test='COLUMN[@NAME="price"] > 30'>
Here is a small sample of XSLT to demonstrate
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/data/prices">
<table>
<xsl:apply-templates select="ROW" />
</table>
</xsl:template>
<xsl:template match="ROW">
<tr>
<xsl:if test="COLUMN[@NAME='price'] > 30">
<xsl:attribute name="style">background-color:green</xsl:attribute>
</xsl:if>
<td><xsl:value-of select="COLUMN[@NAME='product_id']" /></td>
<td><xsl:value-of select="COLUMN[@NAME='price']" /></td>
</tr>
</xsl:template>
</xsl:stylesheet>
Upvotes: 3
Reputation: 9863
Your XPath expression doesn't match the XML you're showing. The data
element isn't visible in the XML (I assume you omitted that for simplicity), but neither is the ROW
element. Also, you need to be aware of case-sensitivity - i.e you need column
and name
(lower-case) in your XPath (or vice versa). There must be something else wrong, too, because as given, the condition is false for all elements.
Upvotes: 0