Skip to content

Commit

Permalink
fix invalid current-grouping-key when called in new for-each
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed Nov 1, 2022
1 parent e36afe9 commit d75572f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Xsl/Functions/CurrentGroupingKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ public function serialize(Lexer $lexer, DOMNode $currentElement): array
}

if ($currentElement->localName === 'sort' && $currentElement->namespaceURI === XslTransformations::URI) {
$resultTokens = ['current()'];
$lexer->seek($lexer->key() + 2);
return ['current()'];
} else {
if ($xslForEach instanceof DOMElement) {
$resultTokens = ['current()'];
$groupId = $xslForEach->getAttribute('group-id');
} else {
throw new \UnexpectedValueException('Expecting DOMElement');
}
}

$lexer->seek($lexer->key() + 2);
return $resultTokens;
return ['$current-grouping-key-' . $groupId];
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Xsl/Node/ElementForEachGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function transform(DOMElement $element): void
$document = $element->ownerDocument;

$xslForEach = $this->createForEachStatement($element, $groupId);
$groupingKeyVariable = $this->createGroupingKeyVariable($element, $groupId);
$unGroupedVariable = $this->createUnGroupedVariable($element, $groupId);
$groupVariable = $this->createGroupVariable($element, $groupId);

Expand All @@ -61,6 +62,7 @@ public function transform(DOMElement $element): void
}
}

$xslForEach->appendChild($groupingKeyVariable);
$xslForEach->appendChild($groupVariable);

while ($element->childNodes->length > 0) {
Expand Down Expand Up @@ -124,6 +126,19 @@ private function createGroupVariable(DOMElement $element, string $groupId): DOME
return $variable;
}

/**
* @param DOMElement $element
* @param string $groupId
* @return DOMElement
*/
private function createGroupingKeyVariable(DOMElement $element, string $groupId): DOMElement
{
$variable = $element->ownerDocument->createElementNS(XslTransformations::URI, 'xsl:variable');
$variable->setAttribute('name', 'current-grouping-key-' . $groupId);
$variable->setAttribute('select', 'current()');
return $variable;
}

/**
* @param DOMElement $element
* @param string $groupId
Expand Down
14 changes: 14 additions & 0 deletions test/Integration/Xsl/ForEachGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ public function testByCurrentGroupingKey(): void
$this->assertEquals('ComposerGenkgo', \trim($processor->transformToXml($data)));
}

public function testByCurrentGroupingKeyInNewForEach(): void
{
$styleSheet = new DOMDocument();
$styleSheet->load('Stubs/Xsl/ForEachGroup/group-by-current-grouping-key-in-new-for-each.xsl');

$processor = new XsltProcessor(new NullCache());
$processor->importStyleSheet($styleSheet);

$data = new DOMDocument();
$data->load('Stubs/packages.xml');

$this->assertEquals('ComposerGenkgoGenkgoGenkgo', \trim($processor->transformToXml($data)));
}

public function testByCurrentGroupingKeySort(): void
{
$styleSheet = new DOMDocument();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output omit-xml-declaration="yes" />

<xsl:template match="packages">

<xsl:for-each-group select="package" group-by="author">
<xsl:for-each select="current-group()">
<xsl:value-of select="current-grouping-key()" />
</xsl:for-each>
</xsl:for-each-group>

</xsl:template>

</xsl:stylesheet>

0 comments on commit d75572f

Please sign in to comment.