XSL Analyze-String & # 8594; Matching-Substring for multiple variables

I was wondering if it is possible to use analyze-string and set several groups in RegEx, and then save all the corresponding groups in variables that will be used later.

So:

 <xsl:analyze-string regex="^Blah\s+(\d+)\s+Bloo\s+(\d+)\s+Blee" select="."> <xsl:matching-substring> <xsl:variable name="varX"> <xsl:value-of select="regex-group(1)"/> </xsl:variable> <xsl:variable name="varY"> <xsl:value-of select="regex-group(2)"/> </xsl:variable> </xsl:matching-substring> </xsl:analyze-string> 

Actually this does not work, but something like what I need, I know that I can transfer analyze-string to a variable, but it seems silly that for each group I have to process RegEx, and not very efficiently, I should be able to process the regex once and store all groups that will be used later.

Any ideas?

+6
source share
2 answers

Good

 <xsl:variable name="groups" as="element(group)*"> <xsl:analyze-string regex="^Blah\s+(\d+)\s+Bloo\s+(\d+)\s+Blee" select="."> <xsl:matching-substring> <group> <x><xsl:value-of select="regex-group(1)"/></x> <y><xsl:value-of select="regex-group(2)"/></y> </group> </xsl:matching-substring> </xsl:analyze-string> </xsl:variable> 

help? So you have a variable called groups , which is a sequence of group elements with captures.

+6
source

This conversion shows that xsl:analyze-string not required to obtain the desired results - there is a simpler and more general solution. :

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="*[matches(., '^Blah\s+(\d+)\s+Bloo\s+(\d+)\s+Blee')]"> <xsl:variable name="vTokens" select= "tokenize(replace(., '^Blah\s+(\d+)\s+Bloo\s+(\d+)\s+Blee', '$1 $2'), ' ')"/> <xsl:variable name="varX" select="$vTokens[1]"/> <xsl:variable name="varY" select="$vTokens[2]"/> <xsl:sequence select="$varX, $varY"/> </xsl:template> </xsl:stylesheet> 

when applied to this XML document :

 <t>Blah 123 Bloo 4567 Blee</t> 

that produces the desired, correct result :

 123 4567 

Here we do not rely on the knowledge of RegEx (it can be supplied as a parameter) and the string - we just replace the string with the separator string of the RegEx groups, which we then designate, and each element in the resulting sequence with tokenize() can be easily assigned the corresponding variable.

We do not need to search for the desired results, buried at a pace. tree - we just get them all in a sequence of results .

+5
source

Source: https://habr.com/ru/post/921561/


All Articles