select应用于所有元素,而不仅仅是我想要的元素



首先,我将发布我所有的代码:

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>&#36;</span>
        <xsl:value-of select="price"/>
    </td>
    <td>
        <xsl:value-of select="creation_date/day"/>
        <span>&#47;</span>
        <xsl:value-of select="creation_date/month"/>
        <span>&#47;</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>&#32;Kg</span>
            </xsl:when>
            <xsl:otherwise>
                <xsl:if test="//weight > 1">
                <span>&#32;Tonnes</span>
                </xsl:if>
                <xsl:if test="//weight &lt;= 1">
                <span>&#32;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>&#36;</span>
        <xsl:value-of select="price"/>
    </td>
    <td>
        <xsl:value-of select="creation_date/day"/>
        <span>&#47;</span>
        <xsl:value-of select="creation_date/month"/>
        <span>&#47;</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>&#32;Kg</span>
            </xsl:when>
            <xsl:otherwise>
                <xsl:if test="//weight > 1">
                <span>&#32;Tonnes</span>
                </xsl:if>
                <xsl:if test="//weight &lt;= 1">
                <span>&#32;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>&#36;</span>
        <xsl:value-of select="price"/>
    </td>
    <td>
        <xsl:value-of select="creation_date/day"/>
        <span>&#47;</span>
        <xsl:value-of select="creation_date/month"/>
        <span>&#47;</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>&#32;Kg</span>
            </xsl:when>
            <xsl:otherwise>
                <xsl:if test="//weight > 1">
                <span>&#32;Tonnes</span>
                </xsl:if>
                <xsl:if test="//weight &lt;= 1">
                <span>&#32;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。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>&#32;Kg</span>
        </xsl:when>
        <xsl:otherwise>
            <xsl:if test="//weight > 1">
            <span>&#32;Tonnes</span>
            </xsl:if>
            <xsl:if test="//weight &lt;= 1">
            <span>&#32;Ton</span>
            </xsl:if>
        </xsl:otherwise>
    </xsl:choose>
</td>

它的意思是检查属性"单位",并根据它的值,然后附加一个g, kg或吨到实际重量的末尾。这部分工作得相当好。问题是它使用第一个表并将其应用于所有三个表。

因此,例如,在我的xml中,如果第一个产品的重量,属性单位设置为kg,那么所有表中的每个产品都设置为kg。

希望我已经解释得足够好,它实际上是有意义的。我把所有的代码都贴出来了,这样你们就可以自己运行了,这似乎比解释清楚要容易得多。

谢谢,乔尔

weightproduct的子元素,因此当您处于product的上下文中时,对当前产品重量的正确引用是./weight或简单的weight -而不是从根开始的//weight

与你的问题无关,但你不应该为每个类别重复相同的代码。


编辑:

有什么建议吗....

下面是一个简化后的示例:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/catalog">
    <html>
        <link rel="stylesheet" type="text/css" href="test.css" />
        <xsl:variable name="thead">
            <tr>
                <th>Title</th>
                <th>Price</th>
                <th>Creation Date</th>
                <th>Weight</th>
                <th>Color</th>
                <th>Description</th>
            </tr>
        </xsl:variable>
        <p>Artworks</p>
        <table>
            <xsl:copy-of select="$thead"/>
            <xsl:apply-templates select="product[@category='art']">
                <xsl:sort select="@id" data-type="number"/>
            </xsl:apply-templates>
        </table>
        <p>Ovenware</p>
        <table>
            <xsl:copy-of select="$thead"/>
            <xsl:apply-templates select="product[@category='ovenware']">
                <xsl:sort select="@id" data-type="number"/>
            </xsl:apply-templates>
        </table>
        <p>Dinner Sets</p>
        <table>
            <xsl:copy-of select="$thead"/>
            <xsl:apply-templates select="product[@category='dinner_set']">
                <xsl:sort select="@id" data-type="number"/>
            </xsl:apply-templates>
        </table>
    </html>
</xsl:template>
<xsl:template match="product">          
    <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="format-number(price, '$0')"/></td>
        <td>
            <xsl:value-of select="concat(creation_date/day, '/', creation_date/month, '/', creation_date/year)"/></td>
        <td>
            <xsl:value-of select="weight"/>
            <xsl:choose>
                <xsl:when test="weight/@unit = 'gram'">g</xsl:when>
                <xsl:when test="weight/@unit = 'kilogram'"> Kg</xsl:when>
                <xsl:otherwise>
                    <xsl:text> Ton</xsl:text>
                    <xsl:if test="weight > 1">nes</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:template>
</xsl:stylesheet>

还可以通过循环一个类别列表来进一步实现这一点—可以是样式表或单独文档中保存的静态列表,也可以是通过从处理过的XML中提取唯一类别而派生的动态列表。

相关内容

  • 没有找到相关文章

最新更新