XSLT to modify attribute value and add new element

会有一股神秘感。 提交于 2019-12-12 01:15:41

问题


I have below 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>

What I want to do is replace the value "\prdausrvs01\Transfer\Onbase\OUT\EnterprisePrint\" with "/opt/oracle/archive/"

I tried this with below xslt but this is outputting only the new element. Existing attribute is not changed.

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:param name="search" select="'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="@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> 
        <xsl:template match="/">
            <xsl:element name='DynamicEnrichment'>
                <xsl:attribute name='type'>
                    <xsl:text>TiffPathModifier</xsl:text>
                </xsl:attribute>
                <xsl:element name='PreditorEnvironment'>
                    <xsl:text>Changed TiffPaths if existed</xsl:text>
                </xsl:element>          
            </xsl:element>
        </xsl:template> 
    </xsl:stylesheet>

Any help would be much appreciated

来源:https://stackoverflow.com/questions/53948731/xslt-to-modify-attribute-value-and-add-new-element

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