使用 xslt 在分类布局中转换 xml

发布于 2024-11-07 08:59:48 字数 1984 浏览 1 评论 0原文

我有一个像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云醉月微眠 2024-11-14 08:59:48

分组通常通过 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>

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:

<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>
も星光 2024-11-14 08:59:48

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>

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.

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文