XSL使用替换和匹配来更新值

时间:2019-01-22 03:22:43

标签: xslt xpath

我正在尝试使用已更改数据模型的旧表单,因此我要用新模型替换对旧模型的任何引用。我目前使用功能 matches 告诉我是否应该对当前字符串执行替换,然后使用 replace 函数用新的替换值,问题是 matches 中使用的正则表达式与 replace 中的正则表达式不兼容。

 <xsl:template
    match="//*[contains(@*:default,'instance(''document'')/')
    mode="pass">

    <xsl:variable
        name="regex"
        as="element()*">
        <regex>instance('document')/doc_type/description</regex>
        <regex>anotherRegex</regex>
    </xsl:variable>

    <xsl:variable
        name="replacement"
        as="element()*">
        <replacement>xxf:get-request-parameter('documentDesc')</replacement>
        <replacement>replacedRegex</replacement>
    </xsl:variable>

    <xsl:element name="{name()}">
        <xsl:for-each select="@*">
            <xsl:choose>
                <xsl:when test="name() = ('xxf:default')">
                    <xsl:attribute name="xxf:default">
                        <xsl:analyze-string
                        regex="{concat('(',$regex[1],'|',$regex[2],')')}"
                        select=".">
                            <xsl:matching-substring>
                                <xsl:if test="matches(.,$regex[1])">
                                    <xsl:value-of select="replace(.,$regex[1],$replacement[1])" />
                                </xsl:if>
                                <xsl:if test="matches(.,$regex[2])">
                                    <xsl:value-of select="replace(.,$regex[2],$replacement[2])" />
                                </xsl:if>
                            </xsl:matching-substring>
                            <xsl:non-matching-substring>
                                <xsl:value-of select="." />
                            </xsl:non-matching-substring>
                        </xsl:analyze-string>
                    </xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:attribute name="{local-name()}"><xsl:value-of select="." /></xsl:attribute>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:element>
</xsl:template>

当前XML:

<xf:bind id="clinic-bind"
    name="clinic"
    xxf:default="instance('document')/doc_type/description"
    type="xf:string"/>

我想把它变成什么:

<xf:bind id="clinic-bind"
    name="clinic"
    xxf:default="xxf:get-request-parameter('documentDesc')"
    type="xf:string"/>

1 个答案:

答案 0 :(得分:0)

所以问题基本上是我必须转义正则表达式变量中的'()'字符。

所以我结束了

<regex>instance\('document'\)/doc_type/description</regex>

在正则表达式部分。

相关问题