Problem in xml sorting in XSLT 2.0 of any ideas?

Hello, I'm trying to sort my xml by the number of occurrences of the "response" element with the attribute "id" and get just a summary.

<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>

I want just the summary text in the output:

A = 2 times (s)
C = 2 times (s)
B = 1 times (s)

In XSLT 2.0, I tried:

<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>

but it does not work:
in XMLSpy 2008:
"Error in XPath 2.0 expression Not node element"

in Saxon 9:
XPTY0020: Leading '/' cannot select the root of the tree node containing the context element: the context element is an atomic value
. The style sheet could not be compiled. 1 error detected.

+3
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>

,

<root>
<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>
</root>

A = 2 time(s).
C = 2 time(s).
B = 1 time(s).
+4

Source: https://habr.com/ru/post/1731880/


All Articles