XSLT 2.0: using a regular expression to extract all matching parts to an array

安稳与你 提交于 2019-12-12 05:25:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!