XSLT:在xls:sort属性中使用参数(动态排序) [英] XSLT: use parameters in xls:sort attributes (dynamic sorting)

查看:82
本文介绍了XSLT:在xls:sort属性中使用参数(动态排序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将参数应用于xsl:sort元素中的selectorder属性?我想用这样的东西用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:

  1. <xsl:param name="sortBy" select="viewCount"/> 这将$sortBy参数定义为当前节点(文档节点)的viewCount子级的值.因为顶部元素未命名为viewCount,所以如此定义的$sortBy参数根本没有值.

  1. <xsl:param name="sortBy" select="viewCount"/> This defines the $sortBy parameter as the value of the viewCount child of the current node (the document node). Because the top element is not named viewCount, 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属性中使用参数(动态排序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆