首先,我发布所有代码:
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%;
}
好的,如果你将所有这些发布到一个文件夹中,并将它们全部命名为&#34; test。(无论扩展名是什么)&#34;。所以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>
它的意图是检查属性&#39;单位&#39;并根据它的值,然后在实际重量的末尾附加g,kg或ton。这部分效果很好。问题是它使用第一个并将其应用于所有三个表。
因此,对于我的xml中的示例,如果第一个产品权重,属性单位设置为kg,则所有表中的每个产品都设置为kg。
希望我已经足够好地解释了这一点,它实际上是有道理的。我发布了所有代码,所以你们可以为自己运行它们,似乎更容易尝试解释它。
谢谢,
乔尔
答案 0 :(得分:1)
weight
是product
的孩子,因此当您处于product
的上下文中时,正确引用当前产品的权重为{{1}或者只是./weight
- 而不是weight
,它从根开始。
与您的问题无关,但您不应该为每个类别重复相同的代码。
还有关于如何做的任何建议......
这是一些精简之后的例子:
//weight
可以通过循环一个类别列表来进一步做 - 可以是样式表中保存的静态列表,也可以是单独的文档,或者是通过从处理过的XML中提取唯一类别而得到的动态列表。