XSLT修改属性值并添加新元素

时间:2018-12-27 17:26:41

标签: xml 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="'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>

任何帮助将不胜感激

0 个答案:

没有答案