替换XML文档中的属性值

时间:2018-12-26 21:30:13

标签: xml attributes xslt-2.0

我下面有XML

    <?xml version="1.0" encoding="UTF-8"?>
    <Wrapper>
        <DynamicEnrichment>
            <outpath>/opt/oracle/archive</outpath>
        </DynamicEnrichment>
        <ImagedDocuments sequence="11" beginTime="2018-12-03T16:03:11.7237883-06:00" endTime="2018-12-03T16:03:11.7237883-06:00">
            <Document type="Secure - New Business Reg 60 Disclosure Form" path="\\prdausrvs01\Transfer\Onbase\OUT\EnterprisePrint\b726e5d73692463da29bd9183d6c3b6e_AV001220207.TIF" fileName="REG60DISC"/>
            <Document type="Secure - New Business Reg 60 Disclosure Form1" path="\\prdausrvs01\Transfer\Onbase\OUT\EnterprisePrint\b726e5d73692463da29bd9183d6c3b6e_AV001220204.TIF" fileName="REG60DISC1"/>
        </ImagedDocuments>
    </Wrapper>

我想要做的是将值“ \ prdausrvs01 \ Transfer \ Onbase \ OUT \ EnterprisePrint \”替换为“ / opt / oracle / archive /”

我在xslt以下尝试了此操作,但没有得到正确的结果。

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:param name="search" select="substring-before(/Wrapper/ImagedDocuments/Document[1]/@path,'EnterprisePrint')"/>
        <xsl:param name="replace" select="/Wrapper/DynamicEnrichment/outpath"/> 
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="@*">
            <xsl:text>
                <xsl:value-of select="."/>
            </xsl:text>
            <xsl:analyze-string select="." regex="{$search}">           
                <xsl:matching-substring>
                    <xsl:value-of select="$replace"/>
                </xsl:matching-substring>
                <xsl:non-matching-substring>
                    <xsl:value-of select="."/>
                </xsl:non-matching-substring>
            </xsl:analyze-string>
        </xsl:template>
    </xsl:stylesheet>

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

searchreplace字符串还原为其裸露的基本字符串,并将substring...放入模板中。

因此,如下修改xsl:analyze-string模板:

<xsl:param name="search" select="'EnterprisePrint\\'"/>
<xsl:param name="replace" select="'/Wrapper/DynamicEnrichment/outpath/'"/>

...

<xsl:template match="@path">
    <xsl:attribute name="path">
        <xsl:analyze-string select="." regex="(.*){$search}(.*)">           
            <xsl:matching-substring>
                <xsl:value-of select="concat($replace,regex-group(2))"/>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:attribute>
</xsl:template>

您还可以选择使用xsl:choose而不是xsl:analyze-string(请注意搜索字符串中的斜杠要少一个):

<xsl:param name="search" select="'EnterprisePrint\'"/>
<xsl:param name="replace" select="'/Wrapper/DynamicEnrichment/outpath/'"/>

...

<xsl:template match="@path">
    <xsl:variable name="str" select="substring-before(.,$search)" />
    <xsl:attribute name="path">
        <xsl:choose>
            <xsl:when test="$str=''">
                <xsl:value-of select="." />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat($replace,substring-after(.,$search))" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:attribute>
</xsl:template>

两个版本都有相同的输出。