regex - XSL using matches and regular expressions -
i have simple for-each statement matches nodes using regular expression. looking inside nodes matches of 4 or more digits.
<xsl:for-each select="//methods/body[matches(., '= \d{4}')]"> <p><xsl:value-of select="." /></p> </xsl:for-each> i trying value of matched value - i.e. if nodea contains "xxxx yyyy 12345 zzzz" , nodeb contains "aaa bb 88555 cccc", print values 12345 , 88555.
the code above print entire current node matches.
furthermore, need distinct values. e.g. xxx 12345, yyy 56789, zzz 12345 need result 12345 , 56789.
is matching pattern best way accomplish this? got initial (correct) set of nodes quickly, stalled.
for test, matches() works well. pulling out values, xsl:analyze-string better.
example...
xml input
<doc> <x>xxxx yyyy 12345 zzzz</x> <x>aaa bb 88555 cccc</x> <x>xxxx yyyy 12345 zzzz aaa bb 88555 cccc</x> </doc> xslt 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="text"/> <xsl:strip-space elements="*"/> <xsl:template match="/*"> <xsl:variable name="values"> <xsl:for-each select="x[matches(.,'\d{4}')]"> <xsl:analyze-string select="." regex="\d{{4}}"> <xsl:matching-substring> <xsl:sequence select="."/> </xsl:matching-substring> </xsl:analyze-string> </xsl:for-each> </xsl:variable> <xsl:value-of select="distinct-values(tokenize($values,'\s'))" separator=", "/> </xsl:template> </xsl:stylesheet> output
1234, 8855
Comments
Post a Comment