如何添加图像?

发布于 2024-07-04 00:19:32 字数 1768 浏览 6 评论 0原文

情况:

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

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

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>

Situation:

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

方圜几里 2024-07-11 00:19:32

没关系——我是个白痴。 我只需要

Never mind -- I'm an idiot. I just needed
<xsl:value-of select="/root/Image/node()"/>

坦然微笑 2024-07-11 00:19:32

另一种可以尝试的选项是直接的

<img width="100" height="100" src="/root/Image/image.jpeg" class="CalloutRightPhoto"/>

,即不带 {},而是给出直接图像路径

The other option to try is a straightforward

<img width="100" height="100" src="/root/Image/image.jpeg" class="CalloutRightPhoto"/>

i.e. without {} but instead giving the direct image path

余罪 2024-07-11 00:19:32

为了添加属性,XSL 需要的

<xsl:element name="img">
     (attributes)
</xsl:element>

不仅仅是“

<img>
     (attributes)
</img>

虽然,是的,如果您只是按原样复制元素,则不需要任何这些。”

In order to add attributes, XSL wants

<xsl:element name="img">
     (attributes)
</xsl:element>

instead of just

<img>
     (attributes)
</img>

Although, yes, if you're just copying the element as-is, you don't need any of that.

ぃ双果 2024-07-11 00:19:32

不应该是:

<xsl:value-of select="/root/Image/img/@src"/>

? 看起来您正在尝试将整个 Image/img 节点复制到属性 @src

Shouldn't that be:

<xsl:value-of select="/root/Image/img/@src"/>

? It looks like you are trying to copy the entire Image/img node to the attribute @src

待"谢繁草 2024-07-11 00:19:32

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

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

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

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

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

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

请注意,此处可以使用与 xsl_copy-of 中使用的相同的 XPath,因为它知道在属性值模板中使用时获取文本值。

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>

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文