2

我是 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>

目前我在文件中都有它只会显示最后一个模板结果而不是两个模板。

任何帮助将不胜感激。

谢谢

4

1 回答 1

0

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

通常,将分支从显式分支指令(xsl:if 等)转移到模板匹配模式的想法。还将循环控制从 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>   
于 2012-10-18T01:17:13.407 回答