Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 最后一个XSL排序指定值_Xslt_Xslt 1.0 - Fatal编程技术网

Xslt 最后一个XSL排序指定值

Xslt 最后一个XSL排序指定值,xslt,xslt-1.0,Xslt,Xslt 1.0,是否可以按如下方式对节点进行排序: 示例XML <record> <id>0</id> <sku>0</sku> <name>Title</name> <prop>456</prop> <number>99</number> </record> 如果我应用此模板 <xsl:stylesheet version="1.

是否可以按如下方式对节点进行排序:

示例XML

<record>
   <id>0</id>
   <sku>0</sku>
   <name>Title</name>
   <prop>456</prop>
   <number>99</number>
</record>
如果我应用此模板

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
     <xsl:template match="record/*">
     <xsl:param select="." name="value"/>
        <div>
           <xsl:value-of select="concat(local-name(), ' - ', $value)"/>
         </div>
     </xsl:template>
</xsl:stylesheet>
输出:

<div>id - 0</div>
<div>sku - 0</div>
<div>name - Title</div>
<div>prop - 456</div>
<div>number - 99</div>
但是,我希望最后输出所有0值,如下所示:

<div>name - Title</div>
<div>prop - 456</div>
<div>number - 99</div>
<div>id - 0</div>
<div>sku - 0</div>

这可以通过对问题进行排序来实现吗?

在我看来,问题根本不是任何排序。 你甚至没有写你提到的具体值是多少 这就是标题。它是记录子元素的任意序列

请尝试执行以下脚本:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />

  <xsl:template match="record">
    <xsl:copy>
      <xsl:apply-templates select="name, prop, number, id, sku"/>  
    </xsl:copy>
  </xsl:template>

  <xsl:template match="record/*">
    <div><xsl:value-of select="concat(local-name(), ' - ', .)"/></div>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
  </xsl:template>
</xsl:transform>
并加上:

xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
转换标记


但是我仍然看不到任何可以称为特定值的内容。

使用XSLT-1.0可以很容易地实现这一点。只需在xsl:apply templates上使用谓词,检查内容是否为零:

 <xsl:template match="record/*">
    <div>
       <xsl:value-of select="concat(local-name(), ' - ', .)"/>
     </div>
 </xsl:template>

 <xsl:template match="/record">
    <xsl:apply-templates select="*[normalize-space(.) != '0']" />
    <xsl:apply-templates select="*[normalize-space(.)  = '0']" />
 </xsl:template>

这不会对输出进行排序,而是按照您希望的方式对其进行分组。xsl:param是不必要的。

在XSLT中可以进行定制排序,但首先需要定义该排序的逻辑。这里的分类规则是什么?@Utkanos啊,我错了。我希望在所有其他值之后输出所有零值。抱歉错过了!
 <xsl:template match="record/*">
    <div>
       <xsl:value-of select="concat(local-name(), ' - ', .)"/>
     </div>
 </xsl:template>

 <xsl:template match="/record">
    <xsl:apply-templates select="*[normalize-space(.) != '0']" />
    <xsl:apply-templates select="*[normalize-space(.)  = '0']" />
 </xsl:template>