/** * @param DOMElement $element * @param $groupId * @return DOMElement */ private function createForEachStatement(DOMElement $element, $groupId) { $select = $element->getAttribute('select'); $groupBy = $element->getAttribute('group-by'); $namespaces = FetchNamespacesFromNode::fetch($element); $addGroupingItem = (new FunctionBuilder('php:function'))->addArgument(PhpCallback::class . '::call')->addArgument(XslTransformations::URI . ':group-by')->addArgument($groupId)->addExpression('.')->addExpression('generate-id(.)')->addArgument(base64_encode($groupBy))->addArgument(base64_encode(json_encode($namespaces))); $updatedSelect = $select . '[' . $addGroupingItem->build() . ']'; $createGroupFunction = (new FunctionBuilder('php:function'))->addArgument(PhpCallback::class . '::call')->addArgument(XslTransformations::URI . ':group-iterate')->addArgument($groupId)->addExpression($updatedSelect); $xslForEach = $element->ownerDocument->createElementNS(XslTransformations::URI, 'xsl:for-each'); $xslForEach->setAttribute('select', $createGroupFunction->build() . '//xsl:group'); $xslForEach->setAttribute('group-id', $groupId); return $xslForEach; }
/** * @param Lexer $lexer * @param DOMNode $currentElement * @return string[] */ private function createFunctionTokens(Lexer $lexer, DOMNode $currentElement) { $token = $lexer->current(); $documentElement = $currentElement->ownerDocument->documentElement; $namespaces = FetchNamespacesFromNode::fetch($documentElement); $functionName = $this->convertTokenToFunctionName($token, $namespaces); if ($this->functions->has($functionName)) { $function = $this->functions->get($functionName); if ($function instanceof ReplaceFunctionInterface) { return $function->replace($lexer, $currentElement); } } return [$token]; }
/** * @param DOMDocument $document */ public function transform(DOMDocument $document) { $root = $document->documentElement; $namespaces = FetchNamespacesFromNode::fetch($document->documentElement); $xslPrefix = array_search(XslTransformations::URI, $namespaces); $excludePrefixes = preg_split('/\\s/', $root->getAttribute('exclude-result-prefixes')); $excludePrefixes[] = 'php'; $excludePrefixes[] = 'xs'; if (in_array('#all', $excludePrefixes) === true || $this->config->shouldExcludeResultPrefixes()) { $excludePrefixes = array_merge($excludePrefixes, array_keys($namespaces)); $excludePrefixes = array_filter($excludePrefixes, function ($prefix) { return $prefix !== '#all'; }); } $excludePrefixes = array_unique($excludePrefixes); $root->setAttribute('xmlns:php', 'http://php.net/xsl'); $root->setAttribute('xmlns:xs', XmlSchema::URI); $root->setAttribute('exclude-result-prefixes', implode(' ', $excludePrefixes)); $this->transformElements($document, $xslPrefix); $this->transformAttributes($document, $xslPrefix); }
/** * @param $elements * @return DOMDocument */ public static function inScopePrefixes($elements) { Assert::assertArray($elements); $listOfPrefixes = []; foreach ($elements as $element) { $listOfPrefixes = array_merge($listOfPrefixes, array_keys(FetchNamespacesFromNode::fetch($element))); } return XsSequence::fromArray($listOfPrefixes); }