使用 xslt 在分类布局中转换 xml
我有一个像这样的 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>
任何人都可以指出我正确的方向。谢谢
I have an xml file like so:
<?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>
And i'm trying to let it display like so:
Category: Book
Bible
<img>
0.00
---------------------
Category: Magazine
Auto Mall
<img>
10.00
LyfeStyle
<img>
40.00
The XSL i have so far is outputting like so:
Category: Book
Bible
<img>
0.00
---------------------
Category: Magazine
Auto Mall
<img>
10.00
---------------------
Category: Magazine
LyfeStyle
<img>
40.00
---------------------
Here is my XSL file:
<?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>
Can anyone please point me in the right direction. Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
分组通常通过 Meunchan 方法来实现。在SO中你会找到很多关于的答案。这里你有一个可能的解决方案,只需对你的 xsl 做一点小小的改变:
Grouping is usually achieved with Meunchan method. In SO you will find a lot of answers about. Here you have one possible solution which just make a small change to your xsl:
Govnah,
在不更改 XML 的情况下,您可以采用如下路线:
http://www. jenitennison.com/xslt/grouping/muenchian.html
循环浏览您的书籍,但仅浏览那些没有“以下兄弟”且具有相同类别的书籍(即该类别的第一本)。然后在该循环中,循环遍历与当前类别匹配的书籍。
你能像这样修改你的XML吗?完成您所要求的操作并非不可能,但有时我发现调整 XML 是一种更简洁的方法。
Govnah,
Without changing the XML, you can go a route like this:
http://www.jenitennison.com/xslt/grouping/muenchian.html
Loop through your books but only the ones without a "following-sibling" that has the same category (i.e. the first of that category). Then inside that loop, loop through the books that match the current category.
Can you modify your XML like so? It is not impossible to do what you're asking but sometimes I find tweaking the XML to be a cleaner route.