是否有可能从href链接和图号序列中提取图像名称

时间:2017-02-08 10:41:10

标签: xml xslt

我的输入代码

<?xml version="1.0" encoding="UTF-8"?>
<task>
<info class="- topic/itemgroup task/info ">
<fig id="status-wf-xsd" class="- topic/fig ">
<title class="- topic/title ">Status</title>
<image placement="inline" class="- topic/image " href="ram/raju/alias/code1_Oct31.jpg"></image>
</fig>
</info>
<info class="- topic/itemgroup task/info ">
<fig id="status-wf-xsd" class="- topic/fig ">
<title class="- topic/title ">Gnerator</title>
<image placement="inline" class="- topic/image " href="ragava/raju/alias/code1_Oct32.jpg"></image>
</fig>
</info>
</task>

xsl我用过

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="task">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="info">
<div>
<span class="figcap"><span class="enumeration fig-enumeration">Figure 1. </span>
<xsl:value-of select="//fig/title"/>
</span>
<div class="figbody">
<img xmlns="" src="{substring-after($code, '/alias/')}"/>
<xsl:value-of select="//image"/>
</div>  
<xsl:apply-templates/>
</div>    
</xsl:template>
<xsl:variable name="code" select="preceding-sibling::fig/image/@href"/>
<xsl:template match="image">
</xsl:template>
<xsl:template match="fig/title">
</xsl:template>
<xsl:template match="fig">
<div class="fignone">
<xsl:apply-templates/>
</div>    
</xsl:template>
</xsl:stylesheet>

输出我正在

<html>
<body>
<div>
<span class="figcap"><span class="enumeration fig-enumeration">Figure 1. </span>Status</span>
<div class="figbody"><img src=""/></div>
<div class="fignone">
</div>
</div>
<div>
<span class="figcap"><span class="enumeration fig-enumeration">Figure 1. </span>Gnerator</span>
<div class="figbody"><img src=""/></div>
<div class="fignone">
</div>
</div>
</body>
</html>

但是我希望我输出图号序列和fig cap里面的figtone然后img标签为xmlns =“”src =“code1_Oct32.jpg”

<html>
<body>
<div class="fignone" id="installinfo__status-wf-jar">
<div class="figbody"> <img xmlns="" src="img/m1_Oct31.png" /> </div>
<span class="figcap"><span class="enumeration fig-enumeration">Figure 1. </span>Status</span></div>
<div>
<div class="fignone">
<div class="figbody"><img src="img/code1_Nov1.jpg"/></div>
<span class="figcap"><span class="enumeration fig-enumeration">Figure 2. </span>Gnerator</span>
</div>
</div>
</body>
</html>

请帮助我。

先谢谢

1 个答案:

答案 0 :(得分:0)

您当前的变量code位于任何模板之外,因此它与您匹配的info无关(我说info元素,因为您当前的代码创建了img标记位于匹配info)的模板中。您在匹配image的模板之前将其放置的事实与此无关。

您需要做的是在匹配info的模板中移动变量声明,这也不需要preceding-sibling

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="task">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="info">
        <div>
            <span class="figcap">
                <span class="enumeration fig-enumeration">Figure <xsl:number />. </span>
                <xsl:value-of select="fig/title"/>
            </span>
            <div class="figbody">
                <xsl:variable name="code" select="fig/image/@href"/>
                <img xmlns="" src="img/{substring-after($code, '/alias/')}"/>
            </div>  
            <xsl:apply-templates/>
        </div>    
    </xsl:template>

    <xsl:template match="image" />

    <xsl:template match="fig/title" />

    <xsl:template match="fig">
        <div class="fignone">
            <xsl:apply-templates/>
        </div>    
    </xsl:template>
</xsl:stylesheet>

请注意使用xsl:number进行编号。