This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="image/@href[starts-with(.,'./screenshots/')]">
<xsl:attribute name="href">
<xsl:value-of select="substring(.,3)"/>
</xsl:attribute>
</xsl:template>
<xsl:template match=
"image/@href
[starts-with(.,'./')
and not(starts-with(substring(.,3), 'screenshots/'))
]">
<xsl:attribute name="href">
<xsl:value-of select="substring-after(substring(.,3),'/')"/>
</xsl:attribute>
</xsl:template>
<xsl:template priority="10"
match="image/@href[starts-with(.,'./views/')]">
<xsl:attribute name="href">
<xsl:value-of select="substring(.,9)"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document:
<t>
<image href="./views/screenshots/page1.png"/>
<image href="./screenshots/page2.png"/>
<load href="./xxx.yyy"/>
<image href="ZZZ/screenshots/page1.png"/>
</t>
produces the wanted result:
<t>
<image href="screenshots/page1.png"/>
<image href="screenshots/page2.png"/>
<load href="./xxx.yyy"/>
<image href="ZZZ/screenshots/page1.png"/>
</t>
Do note:
The use and overriding of the identity rule. This is the most fundamental and most powerful XSLT design pattern.
Only href
attributes of image
elements are modified.
Only href
attributes that start with the string "./"
or the string "./{something-different-than-screenshots}/"
are processed in a special way (by separate templates).
All other nodes are only processed by the identity template.
This is a pure "push style" solution.