Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
Sorting XSLT2.0中的xml排序问题有什么想法吗?_Sorting_Count_Xslt 2.0 - Fatal编程技术网

Sorting XSLT2.0中的xml排序问题有什么想法吗?

Sorting XSLT2.0中的xml排序问题有什么想法吗?,sorting,count,xslt-2.0,Sorting,Count,Xslt 2.0,您好,我正在尝试按属性为“id”的元素“answer”的出现次数对xml进行排序,并获取简单摘要。 <person id="1"> <answer id="A"/> <answer id="B"/> </person> <person id="2"> <answer id="A"/> <answer id="C"/> </person> <person id="3"> <

您好,我正在尝试按属性为“id”的元素“answer”的出现次数对xml进行排序,并获取简单摘要。

<person id="1">
 <answer id="A"/>
 <answer id="B"/>
</person>

<person id="2">
 <answer id="A"/>
 <answer id="C"/>
</person>

<person id="3">
 <answer id="C"/>
</person>


我只需要输出摘要文本:

<xsl:for-each select="distinct-values(/person/answer)">
 <xsl:sort select="count(/person/answer)" data-type="number"/>
 <xsl:value-of select="./@id"/> = 
 <xsl:value-of select="count(/person/answer[@id=./@id])"/> time(s)
</xsl:for-each>
A=2次
C=2次
B=1次(s)

我在XSLT2.0中尝试过:

<xsl:for-each select="distinct-values(/person/answer)">
 <xsl:sort select="count(/person/answer)" data-type="number"/>
 <xsl:value-of select="./@id"/> = 
 <xsl:value-of select="count(/person/answer[@id=./@id])"/> time(s)
</xsl:for-each>

= 
时间(s)
但它不起作用:
在XMLSpy 2008中:
“XPath 2.0表达式中的错误不是节点项”

在撒克逊9中:
XPTY0020:前导“/”无法选择包含上下文项的树的根节点:上下文项是原子值

未能编译样式表。检测到1个错误。

我将对每组中的项目进行分组和计数:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:for-each-group select="//person/answer" group-by="@id">
      <xsl:sort select="count(current-group())" order="descending"/>
      <xsl:value-of select="concat(current-grouping-key(), ' = ', count(current-group()), ' time(s).&#10;')"/>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>
A = 2 time(s).
C = 2 time(s).
B = 1 time(s).