Re: [xsl] Re: efficiently extracting a capture group from analyze-string()

Subject: Re: [xsl] Re: efficiently extracting a capture group from analyze-string()
From: "Graydon graydon@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 4 Jun 2022 17:58:46 -0000
On Sat, Jun 04, 2022 at 05:22:16PM -0000, Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx scripsit:
> The tricky part is, I need an empty sequence if the input string does not
> comply with pattern.
[snip]
> And although this example processes a filename, I'd like to find best
> generalized way to obtain a capture group (or nothing) for any match scenario.
> In perl, I can assign a variable to the result of a regex match:
>
>   my $file = 'path/my-sometype.xml';
>
>   my ($type) = ($file =~ m{my-(\w+)});

This is hard because perl is hiding a conditional in =~ and there's no
analagous XPath mechanism.

<xsl:variable name="adjust" as="xs:string" select="replace($file,'^.*my-([^\.]*).xml$','$1')" />

<xsl:variable name="type" as="xs:string?" select="if ($file eq $adjust) then '' else $adjust" />

Will do it in the same sort of conceptual pattern perl is using.

<xsl:variable name="type" as="xs:string?"
select="analyze-string($file,'my-(\w+)')/descendant::fn:group[@nr eq
'1']/string()" />

and adding fn to the namespaces declared for the stylesheet is probably
more natural for XPath.


-- 
Graydon Saunders  | graydonish@xxxxxxxxx
^fs oferiode, pisses swa mfg.
-- Deor  ("That passed, so may this.")

Current Thread