2017-02-07 70 views
0

我的XML是像下面的图像:显示使用XML和XSLT

<?xml version="1.0" encoding="ISO-8859-1"?>  
<chapter id="ch01"> 
    <sect1> 
     <title>Wattage</title> 
     <para>Paragraph1</para> 
     <para>Paragraph2</para> 
     <para><figure> 
       <caption> 
        <para> 
        <i>Sample image caption</i></para> 
       </caption> 
       <img src="myimagepath\cover_front.jpg"/> 
      </figure> 
     </para> 
    </sect1> 
</chapter> 
我有在我的渲染使用XSLT我的XML的HTML网页上显示的图像问题

(通过C#aspx页面)。

我的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="/"> 
     <html> 
      <body> 
       <h2>My Book</h2> 
       <xsl:apply-templates select="chapter/sect1" /> 
      </body> 
     </html> 
    </xsl:template> 
    <xsl:template match="chapter/sect1"> 
     <xsl:apply-templates select="title" /> 
     <xsl:apply-templates select="para/figure" /> 
     <br /> 
    </xsl:template> 
    <xsl:template match="title"> 
     <b><span style="font-size=22px;"> 
       <xsl:value-of select="." /> 
      </span> 
     </b> 
     <br /> 
    </xsl:template> 
    <xsl:template match="para/figure"> 
     <xsl:element name="img"> 
      <xsl:attribute name="src"> 
       <xsl:value-of select="." /> 
      </xsl:attribute> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

我的图像使用上面XSLT不显示。任何人都可以请帮忙。我是XSLT新手。

+1

你在做变换服务器端或客户端吗?图像路径是相对于客户端看到的URL还是XSLT文件? –

+0

你确定你的XML结构合理吗?你故意这样做: Paragraph1 ? – Rob

+0

正如@Rob所建议的那样,我通过添加两个结束标记''完成了输入XML。请注意,如果检查你的答案。 – zx485

回答

1

你正在渲染para/figure的点并不完全符合你的想法,即选择“。”的点。图片来源其实应该呈现的是:

<caption><para><i>Sample image caption</i></para></caption> 
<img src="myimagepath\cover_front.jpg"/> 

尝试改变这个模板:到:

<xsl:template match="para/figure"> 
    <img src="{img/@src}" /> 
</xsl:template> 

(这是从内存的工作),所以:

  • 对/图有两个子元素的标题和img
  • 所以我们想要用一个属性“src”(又是附带的)输出一个标签“img”(名字相同是偶然的),我们希望th e src是当前节点的img元素的src属性(@ ==属性)。大括号使魔术可以将内联值放入要呈现的标记中。
+0

非常感谢你,Murph。 现在在我的aspx页面显示图像。 – Awadesh

+0

对于Rowland问题,我正在通过XML .Net控件进行服务器端XSLT转换。我的图片路径是相对于网址的。我的问题是当我的图像路径与XSLT文件相关时,代码是什么?你能解释一下这个概念吗?非常感谢。 – Awadesh

+0

我的XML中有多个para标签用于一个父标签。 – Awadesh