[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Re: efficiently extracting a capture group from a
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.")
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|