以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XML基础 』  (http://bbs.xml.org.cn/list.asp?boardid=1)
----  [求助]怎么样在XML文件中存放图片?请大虾指点指点!!  (http://bbs.xml.org.cn/dispbbs.asp?boardid=1&rootid=&id=7345)


--  作者:lqs820130
--  发布时间:5/10/2004 4:49:00 PM

--  [求助]怎么样在XML文件中存放图片?请大虾指点指点!!
最近在看《XMLT从入门到精通》(中国铁路出版社)的这本书时看到一个展示图片的例子。
一、源文件
   图片.XML:
  <?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="图文.xsl"?>
<story>
<description>这是第一段文字</description>
<image>image1.jpg</image>
<description>这是第二段文字</description>
<image>image2.jpg</image>
<description>这是第三段文字</description>
<image>image3.jpg</image>
<description>这是第四段文字</description>
<image>image4.jpg</image>
<description>这是第五段文字</description>
<image>image5.jpg</image>
<description>这是第六段文字</description>
<image>image6.jpg</image>
</story>

XSLT文件:
图片.XSL:
  <?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet
 version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns="http://www.w3.org/TR/REC-html40">
<xsl:output method="html" indent="yes" encoding="GB2312" />

<xsl:variable name="images" select="/story/image" />

<xsl:template match="story">
<html>
  <head><title>图文</title></head>
  <body>
    <table border="2" bordercolor="black">
      <xsl:apply-templates select="description" />
    </table>
  </body>
</html>
</xsl:template>

<xsl:template match="description">
  <xsl:variable name="position" select="position()" />
  <xsl:variable name="image" select="$image[$position]" />
    <xsl:choose>
       <xsl:when test="$position mod 2 =1 ">
          <tr>
             <td><img scr="{$image}" /></td>
             <td bgcolor="#eeeeee"><xsl:value-of select="." /></td>
          </tr>
       </xsl:when>
       <xsl:otherwise>
          <tr>
            <td bgcolor="#eeeeee"><xsl:value-of select="." /></td>
            <td><img scr="{$image}" /></td>
          </tr>
       </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

出来了这个问题:无法解析到变量或参数 'image'的引用。变量或参数可能没有被定义,或它可能不在范围内。
这个XML文件到底怎么改哦,麻烦高手指点一下。谢谢


--  作者:forwar
--  发布时间:5/10/2004 5:56:00 PM

--  
1.文件名“图文.xsl”是中文的,会不会出问题?
2.有没有出现:“无法解析到变量或参数 'story'的引用。变量或参数可能没有被定义,或它可能不在范围内” 这种情况?如果没有说明可以解析story,那么说明xsl文件已经可以被xml文件调用,但是'image'有问题,
<xsl:variable name="images" select="/story/image" />    name="images" 而xml里是<image>问题会不会出在这上面?
3.图片是不是放在/story/image/下?

这是我感觉出问题的地方,不知对不对,如果说的不对也不要b4我啊!大家探讨一下。


--  作者:jwings
--  发布时间:5/10/2004 11:12:00 PM

--  
如下是我修改你原文件的文件,错误只是没有统一,要么使用images,要么使用image,特别是xsl里面$image[$position],一定要小心
photo.xml
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="photo.xsl"?>
<story>
 <description>这是第一段文字</description>
 <images>images1.jpg</images>
 <description>这是第二段文字</description>
 <images>images2.jpg</images>
 <description>这是第三段文字</description>
 <images>images3.jpg</images>
 <description>这是第四段文字</description>
 <images>images4.jpg</images>
 <description>这是第五段文字</description>
 <images>images5.jpg</images>
 <description>这是第六段文字</description>
 <images>images6.jpg</images>
</story>
photo.xsl
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/REC-html40">
<xsl:output method="html" indent="yes" encoding="GB2312" />

<xsl:variable name="images" select="/story/images" />

<xsl:template match="story">
<html>
  <head><title>图文</title></head>
  <body>
    <table border="2" bordercolor="black">
      <xsl:apply-templates select="description" />
    </table>
  </body>
</html>
</xsl:template>

<xsl:template match="description">
  <xsl:variable name="position" select="position()" />
  <xsl:variable name="images" select="$images[$position]" />
    <xsl:choose>
       <xsl:when test="$position mod 2 =1 ">
          <tr>
             <td><img scr="{$images}" /></td>
             <td bgcolor="#eeeeee"><xsl:value-of select="." /></td>
          </tr>
       </xsl:when>
       <xsl:otherwise>
          <tr>
            <td bgcolor="#eeeeee"><xsl:value-of select="." /></td>
            <td><img scr="{$images}" /></td>
          </tr>
       </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>
可以显示,只要有图片的话。^_^


--  作者:lqs820130
--  发布时间:5/13/2004 3:31:00 PM

--  
按照jwings大哥帮忙改的,页面总算可以显示了,但是图片部分的图片还是没给显示出来,后来我在论坛中找到一个关于XML做的菜单的例子找到了需要更改的方法,只需要改动XSL文件,改后如下:
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/REC-html40">
<xsl:output method="html" indent="yes" encoding="GB2312" />

<xsl:variable name="images" select="/story/images" />

<xsl:template match="story">
<html>
  <head><title>图文</title></head>
  <body>
    <table border="2" bordercolor="black">
      <xsl:apply-templates select="description" />
    </table>
  </body>
</html>
</xsl:template>

<xsl:template match="description">
  <xsl:variable name="position" select="position()" />
  <xsl:variable name="images" select="$images[$position]" />
    <xsl:choose>
       <xsl:when test="$position mod 2 =1 ">
          <tr>
             <td valign="middle"><img border="0" width="300" height="240">
            <xsl:attribute name="SRC">
              <xsl:value-of select="$images"/>
            </xsl:attribute></img></td>
             <td bgcolor="#eeeeee"><xsl:value-of select="." /></td>
          </tr>
       </xsl:when>
       <xsl:otherwise>
          <tr>
            <td bgcolor="#eeeeee"><xsl:value-of select="." /></td>
            <td valign="middle"><img border="0" width="300" height="240">
            <xsl:attribute name="SRC">
              <xsl:value-of select="$images"/>
            </xsl:attribute></img></td>
          </tr>
       </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

发上来仅仅给其他还不会整图片的新人一点点帮助,更jwings大哥改的那个XSL只在
"><img border="0" width="300" height="240">
            <xsl:attribute name="SRC">
              <xsl:value-of select="$images"/>
            </xsl:attribute></img>
这一块不同,同样的在页面中添加视频也一样的~


--  作者:jwings
--  发布时间:5/13/2004 6:01:00 PM

--  
呵呵,我只是菜鸟一只,怪不好意思的。大家共同努力努力
--  作者:caitijian
--  发布时间:2/16/2006 3:06:00 PM

--  
<tr>
             <td><img scr="{$images}" /></td>
             <td bgcolor="#eeeeee"><xsl:value-of select="." /></td>
          </tr>
将以上的代码修改为:
<tr>
             <td><img SRC="{$images}" /></td>
             <td bgcolor="#eeeeee"><xsl:value-of select="." /></td>
          </tr>
才能显示,注意不是scr而是大写的SRC.
--  作者:gjcn
--  发布时间:2/18/2006 12:27:00 PM

--  
大家看看還需要什麼或者還要問什麼?
我看我有沒有或知道的,謝謝!
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
94.055ms