Subject: | Support for <xsl:value-of select="elem[@attr='value']"/> |
The attached patch adds support for:
<xsl:value-of select="elem[@attr='value']"/>
<xsl:value-of select="//elem[@attr='value']"/>
This patch builds on the 'indent' patch previously submitted for
http://rt.cpan.org/Ticket/Display.html?id=18476
This patch may partially resolve problems reported in
http://rt.cpan.org/Ticket/Display.html?id=16312
Subject: | elem_by_attr.patch |
--- XSLT.pm.indent 2006-03-31 09:20:12.000000000 -0500
+++ XSLT.pm.elem 2006-03-31 11:29:16.000000000 -0500
@@ -2731,6 +2731,13 @@
return &__attribute__( $self, $1, $path, $node, $silent );
}
+ elsif ($path =~ s/^\/descendant\-or\-self\:\:node\(\)\/(child\:\:|)(\*|[\w\.\:\-]+)\[\s*attribute\:\:(\S+?)\s*=[\s'"]*(\S+?)[\s'"]*\]//)
+ {
+ # //elem[@attr=value] #
+ $self->debug(qq{Getting deep element '$2' with attribute '$3' equal to '$4' ("$path")});
+ $self->_outdent();
+ return &__element_by_attribute__( $self, $2, $3, $4, $path, $node, $silent, "deep" );
+ }
elsif ( $path =~
s/^\/descendant\-or\-self\:\:node\(\)\/(child\:\:|)(\*|[\w\.\:\-]+)\[(\S+?)\]//
)
@@ -2752,6 +2759,13 @@
return &__element__( $self, $1, $path, $node, $silent, "deep" );
}
+ elsif ($path =~ s/^\/(child\:\:|)(\*|[\w\.\:\-]+)\[\s*attribute\:\:(\S+?)\s*=[\s'"]*(\S+?)[\s'"]*\]//)
+ {
+ # /elem[@attr=value] #
+ $self->debug(qq{Getting element '$2' with attribute '$3' equal to '$4' ("$path")});
+ $self->_outdent();
+ return &__element_by_attribute__( $self, $2, $3, $4, $path, $node, $silent );
+ }
elsif ( $path =~ s/^\/(child\:\:|)(\*|[\w\.\:\-]+)\[(\S+?)\]// )
{
@@ -2876,6 +2890,30 @@
return $node;
}
+sub __element_by_attribute__
+{
+ my ( $self, $element, $attr, $value, $path, $node, $silent, $deep ) = @_;
+ $deep ||= ""; # False #
+
+ my @list;
+ foreach my $elem ($node->getElementsByTagName($element, $deep)) {
+ if ($elem->getAttribute($attr) eq $value) {
+ push(@list, $elem);
+ }
+ }
+
+ $self->_indent();
+ if (@list) {
+ $node = &__get_node_set__($self, $path, \@list, $silent);
+ } else {
+ $self->debug("Failed");
+ $node = [];
+ }
+ $self->_outdent();
+
+ return $node;
+}
+
sub __element__
{
my ( $self, $element, $path, $node, $silent, $deep ) = @_;
@@ -3947,6 +3985,10 @@
<xsl:value-of select="//elem"/>
+<xsl:value-of select="elem[@attr='value']"/>
+
+<xsl:value-of select="//elem[@attr='value']"/>
+
<xsl:value-of select="elem[n]"/>
<xsl:value-of select="//elem[n]"/>