我有一个像这样的xml文件:
<?xml version="1.0"?>
<BOOKS>
<BOOK category="Book">
<TITLE image="./images/govnahDesign.jpg">Bible</TITLE>
<PRICE>0.00</PRICE>
</BOOK>
<BOOK category="Magazine">
<TITLE image="./images/govnahDesign.jpg">Auto Mall</TITLE>
<PRICE>10.00</PRICE>
</BOOK>
<BOOK category="Magazine">
<TITLE image="./images/govnahDesign.jpg">LyfeStyle</TITLE>
<PRICE>40.00</PRICE>
</BOOK>
</BOOKS>
我试图让它显示如下:
Category: Book
Bible
<img>
0.00
---------------------
Category: Magazine
Auto Mall
<img>
10.00
LyfeStyle
<img>
40.00
到目前为止我的XSL输出如下:
Category: Book
Bible
<img>
0.00
---------------------
Category: Magazine
Auto Mall
<img>
10.00
---------------------
Category: Magazine
LyfeStyle
<img>
40.00
---------------------
这是我的XSL文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="BOOKS/BOOK">
Category: <xsl:value-of select="@category"/>
<xsl:for-each select="@category">
<div class="item"><!--div has a float:left-->
<xsl:value-of select="../TITLE"/>
<img>
<xsl:attribute name="src">
<xsl:value-of select="../TITLE//@image"/>
</xsl:attribute>
</img>
Price: <xsl:value-of select="../PRICE"/><br />
<button id="view" onclick="javascript:viewProduct()">View</button>
</div>
<hr class="clear"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
任何人都可以指出我正确的方向。谢谢
答案 0 :(得分:0)
Govnah,
如果不更改XML,您可以选择以下路线:
http://www.jenitennison.com/xslt/grouping/muenchian.html
循环浏览您的图书,但只包含那些没有“跟随兄弟”且具有相同类别(即该类别中的第一个)的图书。然后在该循环内循环浏览与当前类别匹配的书籍。
你能这样修改你的XML吗?做你要求的事情并非不可能,但有时我发现将XML调整为更清晰的路线。
<?xml version="1.0"?>
<BOOKS>
<CATEGORIES>
<CATEGORY>Book</CATEGORY>
<CATEGORY>Magazine</CATEGORY>
</CATEGORIES>
<BOOK category="Book">
<TITLE image="./images/govnahDesign.jpg">Bible</TITLE>
<PRICE>0.00</PRICE>
</BOOK>
<BOOK category="Magazine">
<TITLE image="./images/govnahDesign.jpg">Auto Mall</TITLE>
<PRICE>10.00</PRICE>
</BOOK>
<BOOK category="Magazine">
<TITLE image="./images/govnahDesign.jpg">LyfeStyle</TITLE>
<PRICE>40.00</PRICE>
</BOOK>
</BOOKS>
答案 1 :(得分:0)
通常使用Meunchan方法实现分组。在SO中你会找到很多答案。在这里,您有一个可能的解决方案,只需对您的xsl进行一些小改动:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="bybookcategory" match="BOOKS/BOOK" use="@category"/>
<xsl:template match="BOOKS">
<xsl:for-each select="BOOK[generate-id()=generate-id(key('bybookcategory', @category)[1])]">
Category: <xsl:value-of select="@category"/>
<xsl:for-each select="key('bybookcategory', @category)">
<div class="item"><!--div has a float:left-->
<xsl:value-of select="TITLE"/>
<img>
<xsl:attribute name="src">
<xsl:value-of select="TITLE//@image"/>
</xsl:attribute>
</img>
Price: <xsl:value-of select="PRICE"/><br />
<button id="view" onclick="javascript:viewProduct()">View</button>
</div>
<hr class="clear"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>