XSLT comparing/sorting using a param passed from PHP
I'm having an issue doing conditional tests and sorts using params passed into the xsl file from PHP. I can do a match based on testing a string value, and can sort using an explicit name, but cannot get the variables to work in doing either. Here is the XSL and a fragment of the relevant XML.
<Products xmlns:b="http://www.viator.com/affiliateXML2/base" xmlns="http://www.viator.com/vap">
<Product>
<ProductName>Washington DC Guided Day Tour</ProductName>
<ProductCategories>
<ProductCategory>
<Group>Tours & Sightseeing</Group>
<Category>Full-day Tours</Category>
<Subcategory>City Tour</Subcategory>
</ProductCategory>
<ProductCategory>
<Group>Tours & Sightseeing</Group>
<Category>Bus & Minivan Tours</Category>
<Subcategory>City Coach Tour</Subcategory>
</ProductCategory>
</ProductCategories>
</Product>
</Products>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:v="http://www.viator.com/vap">
<xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8"/>
<xsl:param name="sortBy"/>
<xsl:param name="catGroup"/>
<xsl:template match="v:Products">
<xsl:for-each select="v:Product/v:ProductCategories/v:ProductCategory">
<xsl:if test="string(v:Group) = $catGroup">
<xsl:apply-templates select="../../.">
<xsl:sort select="$sortBy" order="ascending"/>
</xsl:apply-templates>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="v:Product">
<xsl:variable name="name" select="v:ProductName" />
<xsl:variable name="thumb" select="v:ProductImage/v:ThumbnailURL" />
<xsl:variable name="url" select="v:ProductURLs/v:ProductURL" />
<xsl:variable name="rating" select="v:ProductStarRating/v:AvgRating" />
<div class='rest_block'>
<ul>
<li class='name'><xsl:value-of select="v:ProductName"/></li>
<img class='stars' src='/images/global/hotels/{$rating}stars.png' alt='User Rated {$rating} Stars' />
<li><ul class='top'>
<li class='inline'><em>Category : </em>
<xsl:for-each select="v:ProductCategories/v:ProductCategory">
<xsl:value-of select="v:Group"/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each></li>
<li class='inline'><em>ID: </em><xsl:value-of select="v:ProductCode"/></li>
</ul></li>
<li cl开发者_如何学编程ass='blurb'><img class='h_thumb' src='{$thumb}' alt='{$name}' />
<em>Description:</em><xsl:value-of select="v:ProductText"/></li>
<li class='low_rate' id='tour'>$<xsl:value-of select="v:Pricing/v:PriceUSD"/><span>Per person rate</span><a class='sel_hotel' href='{$url}'>Select Tour</a></li>
<li><ul class='info'>
<li class='inline'><em>Commences: </em><xsl:value-of select="v:Commences"/></li><br/>
<li class='inline'><em>Duration: </em><xsl:value-of select="v:Duration"/></li>
</ul></li>
</ul>
</div>
</xsl:template>
</xsl:stylesheet>
This is my first real experience with XSLT and I can't get params to work how I am used to variables working, even after having read multiple well done answers on this site. I know the params are being passed and that they have the value I want as I replaced another variable reference. I need help on referencing the params in the correct way. Any help is appreciated, thanks.
UPDATE: @Alejandro: I didn't include those as they're being changed dynamically and don't really have a default.
<xsl:param name="sortBy" select="v:Rank"/>
(defaults to rank sorting anyways that's how the XML is formatted)
<xsl:param name="catGroup" select="'Day Trips & Excursions'" />
(this is a string value)
this is how I'm passing them with PHP:
$processor->setParameter('', 'sortBy', 'v:'.$_GET['sort']);
$processor->setParameter('', 'catGroup', 'Day Trips & Excursions');
IF $sortBy param it's an element QName, you could do:
<xsl:sort select="*[name() = $sortBy]" order="ascending"/>
EDIT: This is the correct declaration for $sortBy (it must be a string):
<xsl:param name="sortBy" select="'v:Rank'"/>
精彩评论