你如何添加图像? [英] How do you add an image?

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

问题描述

情况:

我有一个包含图像信息的简单 XML 文档.我需要将其转换为 HTML.但是,我看不到打开标记的位置,当我使用下面的 XSL 代码时,它显示以下错误消息:

I have a simple XML document that contains image information. I need to transform it into HTML. However, I can't see where the open tag is and when I use the XSL code below, it shows the following error message:

当没有打开元素开始标记时,无法写入属性节点."

"Cannot write an attribute node when no element start tag is open."

XML 内容:

<root>
    <HeaderText>
        <HeaderText>Dan Testing</HeaderText>
    </HeaderText>
    <Image>
        <img width="100" height="100" alt="FPO lady" src="/uploadedImages/temp_photo_small.jpg"/>
    </Image>
    <BodyText>
        <p>This is a test of the body text<br  /></p>
    </BodyText>
    <ShowLinkArrow>false</ShowLinkArrow>
</root>

XSL 代码:

<xsl:stylesheet version="1.0" extension-element-prefixes="msxsl"
    exclude-result-prefixes="msxsl js dl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:js="urn:custom-javascript" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:dl="urn:datalist">
    <xsl:output method="xml" version="1.0" omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>
    <xsl:template match="/" xml:space="preserve">
        <img>
            <xsl:attribute name="width">
                100
            </xsl:attribute>
            <xsl:attribute name="height">
                100
            </xsl:attribute>
            <xsl:attribute name="class">
                CalloutRightPhoto
            </xsl:attribute>
            <xsl:attribute name="src">
                <xsl:copy-of select="/root/Image/node()"/>
            </xsl:attribute>
        </img>
    </xsl:template>
</xsl:stylesheet>

推荐答案

只是为了澄清这里的问题 - 错误在以下代码中:

Just to clarify the problem here - the error is in the following bit of code:

<xsl:attribute name="src">
    <xsl:copy-of select="/root/Image/node()"/>
</xsl:attribute>

指令 xsl:copy-of 获取一个节点或节点集并制作它的副本 - 输出一个节点或节点集.然而,属性不能包含节点,只能包含文本值,因此 xsl:value-of 将是一个可能的解决方案(因为它返回节点或节点集的文本值).

The instruction xsl:copy-of takes a node or node-set and makes a copy of it - outputting a node or node-set. However an attribute cannot contain a node, only a textual value, so xsl:value-of would be a possible solution (as this returns the textual value of a node or nodeset).

更短的解决方案(也许更优雅)如下:

A MUCH shorter solution (and perhaps more elegant) would be the following:

<img width="100" height="100" src="{/root/Image/node()}" class="CalloutRightPhoto"/>

在属性中使用 {} 称为属性值模板,可以包含任何 XPATH 表达式.

The use of the {} in the attribute is called an Attribute Value Template, and can contain any XPATH expression.

注意,这里可以使用与您在 xsl_copy-of 中使用的相同的 XPath,因为它知道在属性值模板中使用时采用文本值.

Note, the same XPath can be used here as you have used in the xsl_copy-of as it knows to take the textual value when used in a Attribute Value Template.

这篇关于你如何添加图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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