基本:使用XML和XSLT显示图像
已经在StackOverflow和其他网站上找到了这个答案 大多数问题都涉及我不使用的代码,我正在寻找一个更简单问题的答案。我想要并回答做这类事情的最基本、最普通的方法。这两个文件没有比我在这里发布的更多的内容。所有文件、xml、xsl和图像都位于同一目录中 我的XML代码基本:使用XML和XSLT显示图像,xml,xslt,Xml,Xslt,已经在StackOverflow和其他网站上找到了这个答案 大多数问题都涉及我不使用的代码,我正在寻找一个更简单问题的答案。我想要并回答做这类事情的最基本、最普通的方法。这两个文件没有比我在这里发布的更多的内容。所有文件、xml、xsl和图像都位于同一目录中 我的XML代码 <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="image_experiment.xsl"?>
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="image_experiment.xsl"?>
<root>
<my_content>
<p>IMAGE FOR TESTING XML ONLY</p>
<image>
sizes_1-6.gif
</image>
</my_content>
</root>
仅用于测试XML的图像
大小_1-6.gif
我的XSL文件
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<xsl:template match="image">
<img src="{@image}"/>
<xsl:apply-templates/>
</xsl:template>
</xsl:template>
</xsl:stylesheet>
您的XSLT有两种错误:
- 禁止嵌套模板
- XPath表达式与所需的节点不匹配,因为
选择
(属性节点)而不是正确的image/@image
(文本值节点)李>image/text()
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="image">
<img src="{normalize-space(text())}"/> <!-- remove leading and trailing spaces as well -->
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
正如您所看到的,我还在
img
-标记周围添加了一个最小的HTML。这是我的最终解决方案,效果很好-除了它在图像旁边打印图像文件的名称。仍在研究如何预防这种情况
示例XML标记
<image-center>
size_cat_1-6.jpg
</image-center>
<image-left>
size_cat_7-10.gif
</image-left>
<image-right>
size_cat_8-10.gif
</image-right>
尺寸为1-6.jpg
大小\u类别\u 7-10.gif
大小\u目录\u 8-10.gif
在浏览器中显示实际图像的XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="image-center">
<img src="{.}"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="image-left">
<img src="{.}"/>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="image-right">
<img src="{.}"/>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
那么你的问题是什么?虽然这很有效,但这并不是我想要的。它确实显示了我的图像,这给了我继续学习的信心和前进的方向。我已经了解到,我需要更多地了解xslt,例如和以及一些可以使用的参数。