是否可以对xml元素的属性进行查找和替换?我想要更改href指向的目录:
发自:
<image href="./views/screenshots/page1.png">至
<image href="screenshots/page1.png"> 以及来自:
<image href="./screenshots/page2.png">至
<image href="screenshots/page2.png">因此,通过去掉属于所有图像标记的href的所有"./“,但只删除图像标记。此外,如果first文件夹没有命名为"screenshots“,则将其删除。有没有一种简单的方法可以一气呵成呢?
发布于 2010-10-15 07:09:11
此转换:
<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>在此XML文档上应用时的
<t>
<image href="./views/screenshots/page1.png"/>
<image href="./screenshots/page2.png"/>
<load href="./xxx.yyy"/>
<image href="ZZZ/screenshots/page1.png"/>
</t>生成所需的结果
<t>
<image href="screenshots/page1.png"/>
<image href="screenshots/page2.png"/>
<load href="./xxx.yyy"/>
<image href="ZZZ/screenshots/page1.png"/>
</t>注意到
身份规则的
image元素的属性是以字符串modified.href "./" 或字符串 templates).https://stackoverflow.com/questions/3938208
复制相似问题