我有一个XML文件,它有一个元素<matimage>的@url属性。目前在@url属性中有一个特定的图像名称,比如triangle.png。我想要应用XSLT并修改这个URL,使其类似于assets/images/triangle.png。
我尝试了以下XSLT:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" />
<!-- Copy everything -->
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="@type[parent::matimage]">
<xsl:attribute name="uri">
<xsl:value-of select="NEW_VALUE"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>作为第一步,我尝试用新值替换旧值,但似乎行不通。请告诉我如何在@url属性的现有值前添加或追加新值。
下面是示例XML:
<material>
<matimage url="triangle.png">
Some text
</matimage>
</material>所需输出:
<material>
<matimage url="assets/images/triangle.png">
Some text
</matimage>
</material>发布于 2013-02-18 19:22:33
您正在尝试实现的解决方案可能是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<!-- Identity template : copy elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- Match all the attributes url within matimage elements -->
<xsl:template match="matimage/@url">
<xsl:attribute name="url">
<!-- Use concat to prepend the value to the current value -->
<xsl:value-of select="concat('assets/images/', .)" />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/14934844
复制相似问题