XSLT多个模板,但只从最后的结果中获取

时间:2012-10-17 22:52:13

标签: xml xslt

我是XML和XSLT的新手,有两个不同的模板,但我得到的输出来自最后的模板。

第一个模板输出创建html文本,其中data-video-single为false,即值0和第二个模板输出html文本,其中两个div,即左边一个,右边第二个。

我需要将两个文本连接在一起作为单个html输出。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>

    <xsl:key name="groups" match="/videos/video" use="data-video-group"/>

    <xsl:template match="/videos">
            <xsl:apply-templates mode="single" select="video[generate-id() = generate-id(key('groups', data-video-group)[1]) and data-video-single=0]" />
    </xsl:template>

    <xsl:template match="video" mode="single">

                <h2>
                    <xsl:value-of select="data-video-group"/>
                </h2>

                <xsl:for-each select="key('groups', data-video-group)">
                    <xsl:if test="data-video-single = '0'">
                        <div class="feature">
                            <p class="featurePic">
                                <img data-video-id="{data-video-id}" data-video-url="{data-video-url}" alt=""></img>
                            </p>
                            <h3>
                                <xsl:value-of select="data-video-heading"/>
                            </h3>
                            <p>
                                <xsl:value-of select="data-video-content"/>
                            </p>
                        </div>
                    </xsl:if>
                </xsl:for-each>
    </xsl:template>

    <xsl:template match="/videos">
        <xsl:apply-templates mode="nonSingle" select="video[generate-id() = generate-id(key('groups', data-video-group)[1]) and data-video-single=1]" />                
    </xsl:template> 

    <xsl:template match="video" mode="nonSingle">

        <xsl:if test="position() mod 2 != 0">
            <div class="featureLeft">
                <h2>
                    <xsl:value-of select="data-video-group"/>
                </h2>

                <xsl:for-each select="key('groups', data-video-group)">
                    <xsl:if test="data-video-single = '1'">
                        <div class="feature">
                            <p class="featurePic">
                                <img data-video-id="{data-video-id}" data-video-url="{data-video-url}" alt=""></img>
                            </p>
                            <h3>
                                <xsl:value-of select="data-video-heading"/>
                            </h3>
                            <p>
                                <xsl:value-of select="data-video-content"/>
                            </p>
                        </div>
                    </xsl:if>
                </xsl:for-each>
            </div>            
        </xsl:if>

        <xsl:if test="position() mod 2 = 0">
            <div class="featureRight">
                <h2>
                    <xsl:value-of select="data-video-group"/>
                </h2>

                <xsl:for-each select="key('groups', data-video-group)">
                    <xsl:if test="data-video-single = '1'">
                        <div class="feature">
                            <p class="featurePic">
                                <img data-video-id="{data-video-id}" data-video-url="{data-video-url}" alt=""></img>
                            </p>
                            <h3>
                                <xsl:value-of select="data-video-heading"/>
                            </h3>
                            <p>
                                <xsl:value-of select="data-video-content"/>
                            </p>
                        </div>
                    </xsl:if>

                </xsl:for-each>
            </div>                        
        </xsl:if>

    </xsl:template>

</xsl:stylesheet>

这是源xml数据。

<?xml version="1.0" encoding="utf-8" ?> 
<?xml-stylesheet type="text/xsl" href="/UserControls/IRISNewRelease/Data/newrelease.xslt"?>
<videos>
    <video>
        <data-video-id>1</data-video-id>
        <data-video-single>0</data-video-single>
        <data-video-group>ABC GROUP</data-video-group>
        <data-video-groupNote></data-video-groupNote>
        <data-video-url>VIMEO</data-video-url>
        <data-video-heading>Video Heading 1</data-video-heading>
        <data-video-content>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae lacus fermentum vehicula.</data-video-content>
    </video>
    <video>
        <data-video-id>2</data-video-id>
        <data-video-single>0</data-video-single>
        <data-video-group>ABC GROUP</data-video-group>
        <data-video-url>VIMEO</data-video-url>
        <data-video-heading>Video Heading 2</data-video-heading>
        <data-video-content>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae lacus fermentum vehicula.</data-video-content>
    </video>
    <video>
        <data-video-id>3</data-video-id>
        <data-video-single>0</data-video-single>
        <data-video-group>Company Secretarial</data-video-group>
        <data-video-url>VIMEO</data-video-url>
        <data-video-heading>Video Heading 3</data-video-heading>
        <data-video-content>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae lacus fermentum vehicula.</data-video-content>
    </video>
    <video>
        <data-video-id>4</data-video-id>
        <data-video-single>0</data-video-single>
        <data-video-group>Business Tax</data-video-group>
        <data-video-url>VIMEO</data-video-url>
        <data-video-heading>Video Heading 4</data-video-heading>
        <data-video-content>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae lacus fermentum vehicula.</data-video-content>
    </video>
    <video>
        <data-video-id>5</data-video-id>
        <data-video-single>0</data-video-single>
        <data-video-group>Personal Tax</data-video-group>
        <data-video-url>VIMEO</data-video-url>
        <data-video-heading>Video Heading 5</data-video-heading>
        <data-video-content>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae lacus fermentum vehicula.</data-video-content>
    </video>
    <video>
        <data-video-id>6</data-video-id>
        <data-video-single>1</data-video-single>
        <data-video-group>Personal Tax</data-video-group>
        <data-video-groupNote>Integration with IRIS Personal Tax</data-video-groupNote>
        <data-video-url>VIMEO</data-video-url>
        <data-video-heading>Video Heading 6</data-video-heading>
        <data-video-content>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae lacus fermentum vehicula.</data-video-content>
    </video>
</videos>

预期结果如下......

以下结果来自第一个模板,即mode = single,如果您评论第二个模板并运行它将正常工作。

<h2>
    Personal Tax</h2>
<div class="feature">
    <p class="featurePic">
        <img data-video-id="50214413" data-video-url="VIMEO" alt=""></p>
    <h3>
        Simplified Client Data Entry</h3>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae
        lacus fermentum vehicula.</p>
</div>
<div class="feature">
    <p class="featurePic">
        <img data-video-id="50214135" data-video-url="VIMEO" alt=""></p>
    <h3>
        IRIS Personal Tax and Payroll Integration</h3>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae
        lacus fermentum vehicula.</p>
</div>

这是第二个模板的结果,即mode = nonSingle,如果你评论第一个模板并运行此结果将正常工作。

<div class="featureLeft">
    <h2>
        Accounts Production</h2>
    <div class="feature">
        <p class="featurePic">
            <img data-video-id="50220365" data-video-url="VIMEO" alt=""></p>
        <h3>
            Companies House iXBRL e-filing</h3>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae
            lacus fermentum vehicula.</p>
    </div>
    <div class="feature">
        <p class="featurePic">
            <img data-video-id="50220516" data-video-url="VIMEO" alt=""></p>
        <h3>
            IRIS Accounts Production supports companies limited by guarantee</h3>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae
            lacus fermentum vehicula.</p>
    </div>
</div>
<div class="featureRight">
    <h2>
        Company Secretarial</h2>
    <div class="feature">
        <p class="featurePic">
            <img data-video-id="50220436" data-video-url="VIMEO" alt=""></p>
        <h3>
            Company name change for Companies House</h3>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae
            lacus fermentum vehicula.</p>
    </div>
</div>
<div class="featureLeft">
    <h2>
        Business Tax</h2>
    <div class="feature">
        <p class="featurePic">
            <img data-video-id="50214325" data-video-url="VIMEO" alt=""></p>
        <h3>
            IRIS Business Tax Client Account Overview</h3>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo tellus vitae
            lacus fermentum vehicula.</p>
    </div>
</div>

目前我在文件中同时显示最后一个模板结果,而不是两个模板。

任何帮助都将受到高度赞赏。

由于

1 个答案:

答案 0 :(得分:0)

我无法为您提供合适的解决方案,因为您忘记提供示例输入文档和预期的输出文档。但是,我可以给你一个解决方案的一般模式。您需要根据需要进行调整,并完成最后一个模板的序列构造函数。

通常,将分支从显式分支指令(xsl:if etc)移动到模板匹配模式的想法。同时将循环控制从xsl:for-each移动到模板处理(xsl:apply-templates)。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:key name="groups" match="/videos/video" use="data-video-group"/>

<xsl:template match="/videos">
  <xsl:apply-templates select="video[generate-id() = generate-id(key('groups', data-video-group)[1])]" mode="head"/>
</xsl:template>

<xsl:template match="video" mode="head">
  <h2><xsl:value-of select="data-video-group" /></h2>
  <xsl:apply-templates select="key('groups', data-video-group)" />
</xsl:template>

<xsl:template match="video[data-video-single=0]">
  <!-- Content for "data-video-single = False" case. -->
  <div class="feature">
    <p class="featurePic">
      <img data-video-id="{data-video-id}" data-video-url="{data-video-url}" alt=""></img>
    </p>
    <h3><xsl:value-of select="data-video-heading" /></h3>
    <p><xsl:value-of select="data-video-content" /></p>
  </div>
</xsl:template>

<xsl:template match="video[data-video-single!=0]">
  <!-- Your content for "data-video-single = True" case goes here! -->
</xsl:template>

</xsl:stylesheet>