【发布时间】:2014-09-02 10:40:16
【问题描述】:
首先我会发布我的所有代码:
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://www.w3.org/1999/XSL/Transform">
<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>
如果你想看看,我的 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)>.
还有 Css,所以你们可以正常运行:)
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%;
}
好的,如果您将所有这些都发布在一个文件夹中并将它们全部命名为“test.(无论扩展名是什么)”。所以 test.xml、test.dtd 等等。
所以真正的问题是这里的这段代码:
<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>
它的意思是检查属性“单位”,然后根据它的值在实际重量的末尾附加一个 g、kg 或 ton。这部分工作得很好。问题是它使用第一个并将其应用于所有三个表。
因此,例如在我的 xml 中,如果第一个产品的重量、属性单位设置为 kg,那么所有表中的每个产品都设置为 kg。
希望我已经很好地解释了这一点,并且它实际上是有道理的。我把所有的代码都贴出来了,大家可以自己运行,看起来比解释清楚更容易。
谢谢,
乔尔
【问题讨论】:
-
刚刚意识到我认为问题出在 //weight,这是否会导致它搜索整个文档而不管父母是谁?不确定…………
-
提供一个完整的例子很好;提供一个 minimal 完整的工作示例会更好。对智者说一句话……
-
好点,以后我会领导你的智慧