Firstly I'll post ALL my code:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "test.dtd">
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<catalog>
<product category="art" id="id_001">
<title>Blue Sculpture</title>
<price currency="AUS">2000</price>
<creation_date>
<day>11</day>
<month>08</month>
<year format="yyyy">2014</year>
</creation_date>
<weight unit="kilogram">2</weight>
<color>Green</color>
<description>A beutiful Green Sculpture</description>
</product>
<product category="ovenware" id="id_002">
<title>Red Pie Dish</title>
<price currency="AUS">400</price>
<creation_date>
<day>5</day>
<month>11</month>
<year format="yyyy">2013</year>
</creation_date>
<weight unit="kilogram">5</weight>
<color>Red</color>
<description>Versatile Pie Dish!</description>
</product>
<product category="dinner_set" id="id_003">
<title>Blue Sculpture</title>
<price currency="AUS">2000</price>
<creation_date>
<day>11</day>
<month>08</month>
<year format="yyyy">2014</year>
</creation_date>
<weight unit="ton">2</weight>
<color>Green</color>
<description>A beutiful Green Sculpture</description>
</product>
</catalog>
XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR">
<xsl:template match="/">
<html>
<link rel="stylesheet" type="text/css" href="test.css" />
<p>Artworks</p>
<table>
<tr>
<th>Title</th>
<th>Price</th>
<th>Creation Date</th>
<th>Weight</th>
<th>Color</th>
<th>Description</th>
</tr>
<xsl:for-each select="catalog/product[@category='art']">
<xsl:sort select="@id" data-type="number"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td>
<span>$</span>
<xsl:value-of select="price"/>
</td>
<td>
<xsl:value-of select="creation_date/day"/>
<span>/</span>
<xsl:value-of select="creation_date/month"/>
<span>/</span>
<xsl:value-of select="creation_date/year"/>
</td>
<td><xsl:value-of select="weight"/>
<xsl:choose>
<xsl:when test="//weight[@unit = 'gram']">
<span>g</span>
</xsl:when>
<xsl:when test="//weight[@unit = 'kilogram']">
<span> Kg</span>
</xsl:when>
<xsl:otherwise>
<xsl:if test="//weight > 1">
<span> Tonnes</span>
</xsl:if>
<xsl:if test="//weight <= 1">
<span> Ton</span>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</td>
<td><xsl:value-of select="color"/></td>
<td class="description"><xsl:value-of select="description"/></td>
</tr>
</xsl:for-each>
</table>
<p>Ovenware</p>
<table>
<tr>
<th>Title</th>
<th>Price</th>
<th>Creation Date</th>
<th>Weight</th>
<th>Color</th>
<th>Description</th>
</tr>
<xsl:for-each select="catalog/product[@category='ovenware']">
<xsl:sort select="id"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td>
<span>$</span>
<xsl:value-of select="price"/>
</td>
<td>
<xsl:value-of select="creation_date/day"/>
<span>/</span>
<xsl:value-of select="creation_date/month"/>
<span>/</span>
<xsl:value-of select="creation_date/year"/>
</td>
<td><xsl:value-of select="weight"/>
<xsl:choose>
<xsl:when test="//weight[@unit = 'gram']">
<span>g</span>
</xsl:when>
<xsl:when test="//weight[@unit = 'kilogram']">
<span> Kg</span>
</xsl:when>
<xsl:otherwise>
<xsl:if test="//weight > 1">
<span> Tonnes</span>
</xsl:if>
<xsl:if test="//weight <= 1">
<span> Ton</span>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</td>
<td><xsl:value-of select="color"/></td>
<td class="description"><xsl:value-of select="description"/></td>
</tr>
</xsl:for-each>
</table>
<p>Dinner Set's</p>
<table>
<tr>
<th>Title</th>
<th>Price</th>
<th>Creation Date</th>
<th>Weight</th>
<th>Color</th>
<th>Description</th>
</tr>
<xsl:for-each select="catalog/product[@category='dinner_set']">
<xsl:sort select="id"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td>
<span>$</span>
<xsl:value-of select="price"/>
</td>
<td>
<xsl:value-of select="creation_date/day"/>
<span>/</span>
<xsl:value-of select="creation_date/month"/>
<span>/</span>
<xsl:value-of select="creation_date/year"/>
</td>
<td><xsl:value-of select="weight"/>
<xsl:choose>
<xsl:when test="//weight[@unit = 'gram']">
<span>g</span>
</xsl:when>
<xsl:when test="//weight[@unit = 'kilogram']">
<span> Kg</span>
</xsl:when>
<xsl:otherwise>
<xsl:if test="//weight > 1">
<span> Tonnes</span>
</xsl:if>
<xsl:if test="//weight <= 1">
<span> Ton</span>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</td>
<td><xsl:value-of select="color"/></td>
<td class="description"><xsl:value-of select="description"/></td>
</tr>
</xsl:for-each>
</table>
</html>
</xsl:template>
</xsl:stylesheet>
And just incase you want to have a look, my DTD:
<!ELEMENT catalog (product+)>
<!ELEMENT product (title?, price, creation_date?, weight?, color, description?)>
<!ELEMENT creation_date (day, month, year)>
<!ATTLIST product id ID #REQUIRED>
<!ATTLIST product category (art|dinner_set|ovenware) "art">
<!ATTLIST price currency (AUS|USA) "AUS">
<!ATTLIST weight unit (gram|kilogram|ton) "gram">
<!ATTLIST year format (yy|yyyy) "yy">
<!ELEMENT id (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT day (#PCDATA)>
<!ELEMENT month (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT weight (#PCDATA)>
<!ELEMENT color (#PCDATA)>
<!ELEMENT description (#PCDATA)>.
And the Css, so you guys can run this properly :)
body{
background-color: #fff;
}
table{
border-collapse:collapse;
border-spacing:0;
padding: 10px;
width: 100%;
overflow: hidden;
}
td{
font-family:Arial, sans-serif;
font-size:14px;
padding:10px 5px;
overflow:hidden;
word-break:normal;
min-width: 80px;
text-align: center;
background-color: #E0E5DF;
border-bottom: 2px solid #f6f6f6;
border-top: 2px solid #f6f6f6;
}
th{
font-family:Arial, sans-serif;
font-size:14px;
font-weight:normal;
padding:10px 5px;
overflow:hidden;
word-break:normal;
border-top: 4px solid #32517F;
border-bottom: 2px solid #f6f6f6;
background-color: #64A2FF;
}
tr:hover td{
background-color: #81BDF7;
}
.description{
width: 20%;
}
Okay so if you post all these in a folder and name them all "test.(whatever the extension is)". So test.xml, test.dtd ect.
So the real problem is this bit of code here:
<td><xsl:value-of select="weight"/>
<xsl:choose>
<xsl:when test="//weight[@unit = 'gram']">
<span>g</span>
</xsl:when>
<xsl:when test="//weight[@unit = 'kilogram']">
<span> Kg</span>
</xsl:when>
<xsl:otherwise>
<xsl:if test="//weight > 1">
<span> Tonnes</span>
</xsl:if>
<xsl:if test="//weight <= 1">
<span> Ton</span>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</td>
What it's meant to do is check the attribute 'unit', and depending on it's value is then append a g, kg or ton to the end of the actual weight. This part works rather well. The problem is that it uses the first and applies it to all three tables.
So for exampl in my xml if the first products weight, attribute unit is set to kg, then every product in all the tables is set to kg.
Hope i have explained this well enough and it actually makes sense. I posted all the code so you guys could run it for yourselves, seemed easier that trying to explain it exzactly.
Thanks,
Joel
Tiada ulasan:
Catat Ulasan