xslt 如何将属性添加到副本 [英] xslt how to add attributes to copy-of

查看:25
本文介绍了xslt 如何将属性添加到副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 XSLT 文件中有以下代码:

I have the following piece of code in my XSLT file:

<xsl:copy-of select="/root/Algemeen/foto/node()" />

在 XML 文件中,节点 /root/Algemeen/foto/ 包含一个 HTML 图像,例如:<img src="somephoto.jpg"/>

In the XML file the node /root/Algemeen/foto/ holds an HTML image, for example: <img src="somephoto.jpg" />

我想做的是为图像添加固定宽度.但以下不起作用:

What I would like to do is to add a fixed width to the image. But the following doesn't work:

<xsl:copy-of select="/root/Algemeen/foto/node()">
    <xsl:attribute name="width">100</xsl:attribute>
</xsl:copy-of>

推荐答案

xsl:copy-of 执行所选节点的深层复制,但不提供更改它的机会.

xsl:copy-of performs a deep copy of the selected node, but doesn't provide an opportunity to alter it.

您需要使用 xsl:copy,然后在其中添加其他节点.xsl:copy 只复制节点和命名空间属性,而不复制常规属性和子节点,因此您需要确保apply-templates 推送其他节点也通过.xsl:copy 没有 @select,它适用于当前节点,所以无论你在哪里应用 <xsl:copy-of select="/root/Algemeen/foto/node()"/>,您需要更改为 并移动 img 逻辑到模板中.

You will want to use xsl:copy and then add additional nodes inside. xsl:copy just copies the node and namespace attributes, but not the regular attributes and child nodes, so you will want to ensure that you apply-templates to push the other nodes through as well. xsl:copy does not have a @select, it works on the current node, so wherever you were applying the <xsl:copy-of select="/root/Algemeen/foto/node()" /> , you will need to change to <xsl:apply-templates select="/root/Algemeen/foto/node()" /> and move the img logic into a template.

像这样:

<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>

这篇关于xslt 如何将属性添加到副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆