我的XSLT文件中有以下代码:
<xsl:copy-of select="/root/Algemeen/foto/node()" />
Run Code Online (Sandbox Code Playgroud)
在XML文件中,节点/root/Algemeen/foto/包含HTML图像,例如:<img src ="somephoto.jpg"/>
我想要做的是为图像添加固定宽度.但以下不起作用:
<xsl:copy-of select="/root/Algemeen/foto/node()">
<xsl:attribute name="width">100</xsl:attribute>
</xsl:copy-of>
Run Code Online (Sandbox Code Playgroud)
Mad*_*sen 47
xsl:copy-of 执行所选节点的深层副本,但不提供更改它的机会.
您将需要使用xsl:copy,然后在其中添加其他节点. xsl:copy只复制节点和命名空间属性,但不复制常规属性和子节点,因此您需要确保apply-templates同时推送其他节点. xsl:copy没有@select,它适用于当前节点,因此无论您在何处应用<xsl:copy-of select="/root/Algemeen/foto/node()" />
,您都需要更改<xsl:apply-templates select="/root/Algemeen/foto/node()" />并将img逻辑移动到模板中.
像这样的东西:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<result>
<xsl:apply-templates select="/root/Algemeen/foto/img"/>
</result>
</xsl:template>
<!--specific template match for this img -->
<xsl:template match="/root/Algemeen/foto/img">
<xsl:copy>
<xsl:attribute name="width">100</xsl:attribute>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!--Identity template copies content forward -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)