I have a simple XML document that contains image information. I need to transform it to HTML -- simple, right? However, when I use the XSL below, it blows up with the error "Cannot write an attribute node when no element start tag is open." I can't see where the open tag is -- any ideas?
我有一个包含图像信息的简单XML文档。我需要把它转换成HTML,很简单,对吧?但是,当我使用下面的XSL时,它会出现错误“在没有打开元素开始标记时不能写入属性节点”。我看不清标签在哪里有什么想法吗?
XML:
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:
<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>
5 个解决方案
#1
26
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).
指令xsl:copy-of获取一个节点或节点集并对其进行复制——输出一个节点或节点集。但是属性不能包含节点,只能包含文本值,因此xsl:value-of可能是一种解决方案(因为它返回节点或节点的文本值)。
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.
在属性中使用{}称为属性值模板,可以包含任何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.
注意,这里可以使用与在xsl_copy中使用相同的XPath,因为它知道在属性值模板中使用时采用文本值。
#2
4
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
吗?看起来您正在尝试将整个图像/img节点复制到属性@src
#3
4
In order to add attributes, XSL wants
为了添加属性,XSL需要
<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.
虽然,是的,如果只是按原样复制元素,则不需要这些。
#4
3
Never mind -- I'm an idiot. I just needed <xsl:value-of select="/root/Image/node()"/>
没关系,我是个白痴。我只需要
#5
0
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
即没有{},而是给出直接的图像路径
#1
26
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).
指令xsl:copy-of获取一个节点或节点集并对其进行复制——输出一个节点或节点集。但是属性不能包含节点,只能包含文本值,因此xsl:value-of可能是一种解决方案(因为它返回节点或节点的文本值)。
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.
在属性中使用{}称为属性值模板,可以包含任何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.
注意,这里可以使用与在xsl_copy中使用相同的XPath,因为它知道在属性值模板中使用时采用文本值。
#2
4
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
吗?看起来您正在尝试将整个图像/img节点复制到属性@src
#3
4
In order to add attributes, XSL wants
为了添加属性,XSL需要
<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.
虽然,是的,如果只是按原样复制元素,则不需要这些。
#4
3
Never mind -- I'm an idiot. I just needed <xsl:value-of select="/root/Image/node()"/>
没关系,我是个白痴。我只需要
#5
0
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
即没有{},而是给出直接的图像路径