Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基本:使用XML和XSLT显示图像_Xml_Xslt - Fatal编程技术网

基本:使用XML和XSLT显示图像

基本:使用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"?>

已经在StackOverflow和其他网站上找到了这个答案

大多数问题都涉及我不使用的代码,我正在寻找一个更简单问题的答案。我想要并回答做这类事情的最基本、最普通的方法。这两个文件没有比我在这里发布的更多的内容。所有文件、xml、xsl和图像都位于同一目录中

我的XML代码

<?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,例如和以及一些可以使用的参数。