这是我第一次使用 XSLT 和 XML。所以我要问的是,如何仅显示具有相同 product_type 的特定产品,例如“主板”、“风扇”等?也许像这样product_type == "fan"
,我不确定。我的计划是为每个 product_type 创建一个不同的 XSLT。
目前,我只能显示 XML 中的每个产品。XML 文件如下所示
<?xml version="1.0" standalone="yes"?>
<products>
<product>
<Id>7</Id>
<product_name>Test</product_name>
<product_price>123</product_price>
<product_type>MOTHER BOARD</product_type>
<product_description>123123</product_description>
<product_image>gpu_white.png</product_image>
</product>
<product>
<Id>14</Id>
<product_name>wwww</product_name>
<product_price>1321</product_price>
<product_type>FAN</product_type>
<product_description>qwewqeqw</product_description>
<product_image>hd_white.png</product_image>
</product>
<product>
<Id>22</Id>
<product_name>321123</product_name>
<product_price>321</product_price>
<product_type>MEMORY</product_type>
<product_description>Lorem ipsum dolor sit amet</product_description>
<product_image>fan_white.png</product_image>
</product>
</products>
和 XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<div class="main">
<xsl:for-each select="products/product">
<div class="box">
<h3 style="color:white">
<xsl:value-of select="product_name"/>
</h3>
<xsl:element name="img">
<xsl:attribute name="src">
product_image/<xsl:value-of select="product_image"/>
</xsl:attribute>
<xsl:attribute name="width">
100px
</xsl:attribute>
<xsl:attribute name="height">
100px
</xsl:attribute>
</xsl:element>
<p style="color:white">
RM <xsl:value-of select="format-number(product_price,'0.00')" />
</p>
<p style="font-size:9pt;">
<xsl:value-of select="product_description"/>
</p>
<xsl:element name="button">
<xsl:attribute name="class">btn btn-danger my-cart-btn</xsl:attribute>
<xsl:attribute name="data-id">
<xsl:value-of select="Id"/>
</xsl:attribute>
<xsl:attribute name="data-name">
<xsl:value-of select="product_name"/>
</xsl:attribute>
<xsl:attribute name="data-summary">
<xsl:value-of select="product_description"/>
</xsl:attribute>
<xsl:attribute name="data-price">
<xsl:value-of select="product_price"/>
</xsl:attribute>
<xsl:attribute name="data-quantity">1</xsl:attribute>
<xsl:attribute name="data-image">
product_image/<xsl:value-of select="product_image"/>
</xsl:attribute>
Add to Cart <span class="glyphicon glyphicon-shopping-cart"></span>
</xsl:element>
</div>
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>
这样做非常简单。只需将您的条件添加到方括号中的选择表达式中,即可表示条件
<xsl:for-each select="products/product[product_type = 'FAN']">
(请记住它是区分大小写的)
更好的是,使用参数,而不是为每个产品类型使用单独的样式表......
<xsl:param name="prodType" select="'FAN'"/>
<xsl:template match="/">
<div class="main">
<xsl:for-each select="products/product[product_type = $prodType]">
将参数传递给 XSLT 的方式取决于您使用的 XSLT 处理器以及用于调用它的平台。
请注意,您可以通过删除xsl:element
和xsl:attribute
标签并直接写出元素和属性来大大简化 XSLT ,利用属性值模板,您希望属性的值基于表达式的值。
试试这个 XSLT....
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:param name="prodType" select="'FAN'"/>
<xsl:template match="/">
<div class="main">
<xsl:for-each select="products/product[product_type = $prodType]">
<div class="box">
<h3 style="color:white">
<xsl:value-of select="product_name"/>
</h3>
<img src="product_image/{product_image}" width="100px" height="100px"></img>
<p style="color:white">
RM <xsl:value-of select="format-number(product_price,'0.00')" />
</p>
<p style="font-size:9pt;">
<xsl:value-of select="product_description"/>
</p>
<button class="btn btn-danger my-cart-btn"
data-id="{Id}"
data-name="{product_name}"
data-summary="{product_description}"
data-price="{product_price}"
data-quantity="1"
data-image="product_image/{product_image}">
Add to Cart <span class="glyphicon glyphicon-shopping-cart"></span>
</button>
</div>
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>
属性中的花括号表示属性值模板,表示要评估的表达式,而不是字面上的输出。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句