传递参数变量 XSLT 1.0
passing parameter variable XSLT 1.0
我有类似以下的东西XML:(但有更多产品)
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name>Mango</name>
<type>fruit</type>
<imageurl>pic.jpeg</imageurl>
</product>
<product>
<name>banana</name>
<type>fruit</type>
<imageurl>pic3.jpeg</imageurl>
</product>
<product>
<name>duck</name>
<type>mammal</type>
<imageurl>pic2.jpeg</imageurl>
</product>
</products>
还有这个 XSL:(但有更多的元素和属性)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="typeSelected"/>
<xsl:template match="product/{$typeSelected}">
<xsl:element name="img">
<xsl:attribute name="class">juice</xsl:attribute>
<xsl:attribute name="src">
<xsl:value-of select="imageurl"/>
</xsl:attribute>
</xsl:element>
</div>
</xsl:template>
我正在使用外部 JavaScript 文件设置参数值,但我想仅对 <type>
与该参数值匹配的产品进行分组。显然,XSL 需要更改,并且阅读过我知道我不能在匹配语句中使用参数。感觉我尝试的事情不应该太难。我是否漏掉了一些明显的东西?
给定参数 fruit,我希望输出类似于:
<img class="juice" src="pic.jpeg"/>
<img class="juice" src="pic3.jpeg"/>
我会这样做:
XSLT 1.0
<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:param name="typeSelected"/>
<xsl:key name="product-by-type" match="product" use="type" />
<xsl:template match="/">
<root>
<xsl:for-each select="key('product-by-type', $typeSelected)">
<img class="juice" src="{imageurl}"/>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
结果(当 $typeSelected = "fruit" 时):
<?xml version="1.0" encoding="utf-8"?>
<root>
<img class="juice" src="pic.jpeg"/>
<img class="juice" src="pic3.jpeg"/>
</root>
注:
XML 文档必须有一个根元素;
class
属性的内容是硬编码的;我在你输入的任何地方都没有看到它;
鸭子不是哺乳动物。
我有类似以下的东西XML:(但有更多产品)
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name>Mango</name>
<type>fruit</type>
<imageurl>pic.jpeg</imageurl>
</product>
<product>
<name>banana</name>
<type>fruit</type>
<imageurl>pic3.jpeg</imageurl>
</product>
<product>
<name>duck</name>
<type>mammal</type>
<imageurl>pic2.jpeg</imageurl>
</product>
</products>
还有这个 XSL:(但有更多的元素和属性)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:param name="typeSelected"/>
<xsl:template match="product/{$typeSelected}">
<xsl:element name="img">
<xsl:attribute name="class">juice</xsl:attribute>
<xsl:attribute name="src">
<xsl:value-of select="imageurl"/>
</xsl:attribute>
</xsl:element>
</div>
</xsl:template>
我正在使用外部 JavaScript 文件设置参数值,但我想仅对 <type>
与该参数值匹配的产品进行分组。显然,XSL 需要更改,并且阅读过我知道我不能在匹配语句中使用参数。感觉我尝试的事情不应该太难。我是否漏掉了一些明显的东西?
给定参数 fruit,我希望输出类似于:
<img class="juice" src="pic.jpeg"/>
<img class="juice" src="pic3.jpeg"/>
我会这样做:
XSLT 1.0
<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:param name="typeSelected"/>
<xsl:key name="product-by-type" match="product" use="type" />
<xsl:template match="/">
<root>
<xsl:for-each select="key('product-by-type', $typeSelected)">
<img class="juice" src="{imageurl}"/>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
结果(当 $typeSelected = "fruit" 时):
<?xml version="1.0" encoding="utf-8"?>
<root>
<img class="juice" src="pic.jpeg"/>
<img class="juice" src="pic3.jpeg"/>
</root>
注:
XML 文档必须有一个根元素;
class
属性的内容是硬编码的;我在你输入的任何地方都没有看到它;鸭子不是哺乳动物。