Skip to content

Commit

Permalink
transpile <xsl:attribute name>
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed Dec 5, 2024
1 parent 19a8626 commit 330591d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/Xsl/Node/AttributeName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

namespace Genkgo\Xsl\Xsl\Node;

use DOMElement;
use Genkgo\Xsl\Xpath\Compiler;
use Genkgo\Xsl\Xsl\ElementTransformerInterface;

final class AttributeName implements ElementTransformerInterface
{
/**
* @var Compiler
*/
private $xpathCompiler;

/**
* @param Compiler $compiler
*/
public function __construct(Compiler $compiler)
{
$this->xpathCompiler = $compiler;
}

/**
* @param DOMElement $element
* @return bool
*/
public function supports(DOMElement $element): bool
{
return $element->hasAttribute('name');
}

/**
* @param DOMElement $element
*/
public function transform(DOMElement $element): void
{
$element->setAttribute(
'name',
$this->xpathCompiler->compile(
$element->getAttribute('name'),
$element
)
);
}
}
2 changes: 2 additions & 0 deletions src/Xsl/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Genkgo\Xsl\Util\FetchNamespacesFromNode;
use Genkgo\Xsl\Xpath\Compiler;
use Genkgo\Xsl\Xsl\Node\AttributeExpandText;
use Genkgo\Xsl\Xsl\Node\AttributeName;
use Genkgo\Xsl\Xsl\Node\AttributeValueTemplates;
use Genkgo\Xsl\Xsl\Node\AttributeMatch;
use Genkgo\Xsl\Xsl\Node\AttributeSelect;
Expand Down Expand Up @@ -147,6 +148,7 @@ public static function newDefaultTransformer(
new AttributeExpandText($xpathCompiler),
new AttributeMatch($xpathCompiler),
new AttributeSelect($xpathCompiler),
new AttributeName($xpathCompiler),
new AttributeTest($xpathCompiler),
new ElementValueOf(),
new ElementAttribute(),
Expand Down
2 changes: 1 addition & 1 deletion test/Stubs/Xsl/ElementAttribute/attribute.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<xsl:template match="collection">
<xsl:for-each select="cd">
<cd>
<xsl:attribute name="attr" select="instrument" />
<xsl:attribute name="{lower-case('ATTR')}" select="instrument" />
</cd>
</xsl:for-each>
</xsl:template>
Expand Down

0 comments on commit 330591d

Please sign in to comment.