问题描述
如何将参数应用于xsl:sort
元素中的select
和order
属性?我想用这样的东西用PHP进行动态处理:
How do I apply a parameter to a select
and order
attribute in a xsl:sort
element? I'ld like to do this dynamic with PHP with something like this:
$xsl = new XSLTProcessor();
$xslDoc = new DOMDocument();
$xslDoc->load( $this->_xslFilePath );
$xsl->importStyleSheet( $xslDoc );
$xsl->setParameter( '', 'sortBy', 'viewCount' );
$xsl->setParameter( '', 'order', 'descending' );
但是我现在首先必须如何使它工作.我尝试了以下操作,但它给了我一个编译错误":订单的无效值$ order". $sortBy
似乎也不做任何事情:
But I'ld first have to now how to get this to work. I tried the following, but it gives me a 'compilation error' : 'invalid value $order for order'. $sortBy
doesn't seem to do anything either:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="sortBy" select="viewCount"/>
<xsl:param name="order" select="descending"/>
<xsl:template match="/">
<media>
<xsl:for-each select="media/medium">
<xsl:sort select="$sortBy" order="$order"/>
// <someoutput>
</xsl:for-each>
</media>
</xsl:template>
</xsl:stylesheet>
推荐答案
您已经接近正确的解决方案,但是存在一些问题:
You are close to the correct solution, but there are a few issues:
-
<xsl:param name="sortBy" select="viewCount"/>
这将$sortBy
参数定义为当前节点(文档节点)的viewCount
子级的值.因为顶部元素未命名为viewCount
,所以如此定义的$sortBy
参数根本没有值.
<xsl:param name="sortBy" select="viewCount"/>
This defines the$sortBy
parameter as the value of theviewCount
child of the current node (the document node). Because the top element is not namedviewCount
, the$sortBy
parameter so defined has no value at all.
<xsl:param name="order" select="descending"/>
同上.
<xsl:sort select="$sortBy" order="$order"/>
即使上面的问题1.和2.已解决,该xslt指令仍然有问题.它将order
属性的值指定为文字字符串'$order'
-而不是参数$order
的值.在XSLT中执行此操作的方法是使用AVT(属性值模板).每当我们希望在属性值中指定要将特定字符串作为XPath表达式求值时,则此字符串必须用花括号括起来.
<xsl:sort select="$sortBy" order="$order"/>
Even if issues 1. and 2. above are fixed, this xslt instruction is still problematic. It specifies the value of the order
attribute as the literal string '$order'
-- not as the value of the parameter $order
. The way to do this in XSLT is by using AVT (Attribute Value Template). Whenever we want a to specify that within an attribute value we want a particular string to be evaluated as an XPath expression, then this string must be surrounded by curly braces.
因此,order
属性应指定为:order = '{$order}'
.
So, the order
attribute should be specified as: order = '{$order}'
.
不幸的是,AVT不能用于select
属性(XSLT规范的另一条规则).
Unfortunately, AVTs cannot be used for the select
attribute (another rule from the XSLT spec).
指定select
属性值的方法有些棘手:
The way to specify the value of the select
attribute is a little-bit more tricky:
select='*[name()=$sortBy]'
表示:按子元素排序,该子元素的名称与变量$sortBy
的值相同.
select='*[name()=$sortBy]'
This says: sort by the child element, whose name is the same as the value of the variable $sortBy
.
将所有内容汇总在一起,以下是经过校正的转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="sortBy" select="'viewCount'"/>
<xsl:param name="order" select="'descending'"/>
<xsl:template match="/">
<media>
<xsl:for-each select="media/medium">
<xsl:sort select="*[name()=$sortBy]" order="{$order}"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</media>
</xsl:template>
</xsl:stylesheet>
此转换应用于以下XML文档:
<media>
<medium>
<viewCount>2</viewCount>
</medium>
<medium>
<viewCount>1</viewCount>
</medium>
<medium>
<viewCount>5</viewCount>
</medium>
</media>
产生了正确的结果:
<media>
<medium>
<viewCount>5</viewCount>
</medium>
<medium>
<viewCount>2</viewCount>
</medium>
<medium>
<viewCount>1</viewCount>
</medium>
</media>
这篇关于XSLT:在xls:sort属性中使用参数(动态排序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!