问题
How can I collect all parts of a string matching a regular expression pattern into an array?
<xsl:variable name="matches" select="function('abc_Xza_Y_Sswq', '_[A-Z]')"/>
returning
('_X', '_Y', '_S')
回答1:
There are no arrays in XSLT/XPath 2.0, you could however write a function that uses analyze-string
to return a sequence of strings:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf">
<xsl:function name="mf:extract" as="xs:string*">
<xsl:param name="input" as="xs:string"/>
<xsl:param name="pattern" as="xs:string"/>
<xsl:analyze-string select="$input" regex="{$pattern}">
<xsl:matching-substring>
<xsl:sequence select="."/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:function>
<xsl:template match="/">
<xsl:variable name="matches" select="mf:extract('abc_Xza_Y_Sswq', '_[A-Z]')"/>
<xsl:value-of select="$matches" separator=", "/>
</xsl:template>
</xsl:transform>
来源:https://stackoverflow.com/questions/34968469/xslt-2-0-using-a-regular-expression-to-extract-all-matching-parts-to-an-array