将img标记添加到xslt生成的输出中

时间:2014-02-18 05:44:25

标签: c# html xml image xslt

我正在使用XSLT将XML转换为Html。我必须在生成的html文件中插入图像标记。我想从内容的“href”属性中添加其“src”属性的值。但是“src”属性是空白的。我有以下作为我的XML的一部分

 <body>
      <sec id="ch1.1">
        <title>INTRODUCTION 1</title>
        <p>The trends of increased functionality, improved performance, reduced size and increased complexity continue to evolve in the automotive electronics market. New system architectures are providing the performance and memory capability necessary to keep up with the hardware performance and software growth required by the automotive market trends. All of this technology growth implies a higher product cost and increased engineering effort required to develop these new products.</p>
        <p>
        <fig id="F22" position="float">
        <label>FIG. 2.7</label>
        <caption><p>Major components of internal combustion engines.</p></caption>
        <graphic content-type="figure" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="R-396_fig0021"/>
        </fig>
        </p>
        <p>In order to meet all of the technical and business objectives, corporations must manage project development cost by increasing the productivity and efficiency of engineering activities, reducing developmental spend rates, shortening product development times, and ensuring that the end objectives for product performance and delivery are met to schedule. Making the economic problem more complex, corporate globalization has moved at an unprecedented rate to capture and expand market share, better serve global customers and improve corporate efficiencies. This global movement has produced a significant amount of economic benefit to the corporate business metrics.</p>

我跟随xslt

    <xsl:for-each select="body/sec">
                <p>
                  <h1>
                    <xsl:value-of select="title"/>
                  </h1>
                  <br></br>
                  <xsl:for-each select="p">
                    <xsl:value-of select="text()"/>
                    <xsl:if test="fig">
                     <img alt="{fig/id}" src="{fig/graphic/content/@xlink:href}" style="position:{fig/@position}">


                </img>
    <p><xsl:value-of select="fig/caption/p"/></p>                        
</xsl:if>

                  </xsl:for-each>

我想要以下输出

<p>The trends of increased functionality, improved performance, reduced size and increased complexity continue to evolve in the automotive electronics market. New system architectures are providing the performance and memory capability necessary to keep up with the hardware performance and software growth required by the automotive market trends. All of this technology growth implies a higher product cost and increased engineering effort required to develop these new products.</p><p><img src="../images/R-396_fig0021" style="position:float" alt="Fig 2.7"/>Major components of internal combustion engines.</p><p>In order to meet all of the technical and business objectives, corporations must manage project development cost by increasing the productivity and efficiency of engineering activities, reducing developmental spend rates, shortening product development times, and ensuring that the end objectives for product performance and delivery are met to schedule. Making the economic problem more complex, corporate globalization has moved at an unprecedented rate to capture and expand market share, better serve global customers and improve corporate efficiencies. This global movement has produced a significant amount of economic benefit to the corporate business metrics.</p>

1 个答案:

答案 0 :(得分:1)

看看你是否可以将这个样式表改编成你的样式表。尽可能避免每次都xsl:for-each。使用模板匹配如下

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xlink="http://www.w3.org/1999/xlink" exclude-result-prefixes="xlink"
    version="1.0">

    <xsl:output method="html"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="sec|caption/p">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="sec/title">
        <h1><xsl:apply-templates/></h1>
    </xsl:template>

    <xsl:template match="fig">
        <img src="../images/{graphic/@xlink:href}" style="position:float" alt="{label}"/>
        <xsl:apply-templates select="caption/p"/>
    </xsl:template>

</xsl:stylesheet>