我有以下 xml
<?xml version="1.0" encoding="UTF-8"?>
<content>
<artwork classification="12" href="1.jpg"/>
<artwork classification="10" href="2.jpg"/>
</content>
应用 xsl 时
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink"
>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@href">
<xsl:attribute name="xlink:href">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
它产生
<?xml version="1.0" encoding="UTF-8"?>
<content>
<artwork classification="12" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="1.jpg"/>
<artwork classification="10" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="2.jpg"/>
</content>
而我需要
<?xml version="1.0" encoding="UTF-8"?>
<content xmlns:xlink="http://www.w3.org/1999/xlink">
<artwork classification="12" xlink:href="1.jpg"/>
<artwork classification="10" xlink:href="2.jpg"/>
</content>
我应该如何修改我的xsl以获得我需要的结果?
我使用 xalan XSLT 处理器。
最佳答案
您只需匹配要为其声明 namespace 的元素即可。处理器将为您应用命名空间。
XSLT 1.0 在 MSXSL 4.0 下测试(也在 Saxon-HE 9.2.1.1J 下作为 XSLT 2.0 进行测试)强>)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink"
>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="content">
<content>
<xsl:apply-templates select="@*|node()"/>
</content>
</xsl:template>
<xsl:template match="@href">
<xsl:attribute name="xlink:href">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
关于xslt - xsl 属性命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6184006/