匹配动态xslt模板 [英] matching a dynamic xslt template

查看:102
本文介绍了匹配动态xslt模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个xslt问题,我似乎无法解决.这是我第二次尝试问这个问题,请不要将其标记为重复.

OK so I have an xslt issue that I cannot seem to solve. This is my second attempt at asking the question please do not mark as duplicate.

我的问题是我想在文档的第四个<p>标记之后包含一些HTML.问题是可能存在具有属性class="exclude"

My issue is that I would like to include some HTML after the fourth <p> tag in my document. The issue is that there can be <p> tags with the attribute class="exclude"

如果<p>标记具有该类,则我不想渲染该段落.同时,无论<p class="excludes"有多少

If the <p> tag has that class, I would like to not render that paragraph. At the same time, I would still like to render my included html after the 4th paragraph, regardless of the number of <p class="excludes" there are

: 这是我以前的xsl.这会将我包含的html插入第四段.

: Here is the xsl I had previously. This would slot my included html into the 4th paragraph.

<xsl:template match="content/p[position() = 4]">
     content include<xsl:apply-templates /></p>
</xsl:template> 

以下是一些示例输入/输出

Here are some example inputs / outputs

首先是一个基本示例,其中没有<p class="excludes">我的输入是:

First is the base example where there are no <p class="excludes"> My input is:

<body>
<p></p>
<p></p>
<p></p>
<p></p>
include new HTML here
<p></p>
...
</body>

在该示例中,html在通过我的xslt之后的第4段之后很好地呈现.

In that example, the html renders nicely after the 4th paragraph after passing through my xslt.

但是,我需要考虑<p class="exclude">

这是<p class="exclude" being used >

<body>
<p class="exclude"></p> (should not be rendering on page)
<p></p>
<p></p>
<p></p>
<p></p>
include new HTML here
<p></p>
...
</body>

请注意,在此示例中,包含的HTML出现在第5 *段之后,因为第一段不应在页面上呈现,但是同时我希望在第4个呈现段之后包含呈现的HTML.让我们再看一个更复杂的示例.

Notice that in this example, the included HTML is appearing after the 5th* paragraph because the first paragraph should not be rendering on the page, but at the same time I want the rendered HTML to be included after the 4th rendered paragraph. Lets look at one more example that is more complicated.

<body>
<p class="exclude"></p> (this should not be rendering on page)
<p></p>
<p class="exclude"></p> (this should not be rendering on page)
<p class="exclude"></p> ...
<p></p>
<p class="exclude"></p> ...
<p></p>
<p class="exclude"></p> ...
<p class="exclude"></p> ...
<p class="exclude"></p> ...
<p></p>
new HTML include here
<p></p>
...
<body>
<p class="exclude"></p>

现在,在我的最后一个示例中,您可以看到已添加多个<p class="excludes",是的,我仍然希望新的html内容在第4个呈现的段落之后呈现,或者在本示例中是在所有段落中的第11个总数.

Now, in my final example you can see that multiple <p class="excludes" have been added and yes i still want the new html content to render after the 4th rendered paragraph, or in this example the 11th total over all paragraph.

如果有人有任何xslt可以帮助我实现这一目标,那就太好了.请记住,我正在使用xslt 1.0预先感谢

If anyone has any xslt that could help me achieve this that would be great. Please keep in mind I am using xslt 1.0 Thanks in advance

推荐答案

这是另一个XSLT 1.0样式表选项.

Here's another XSLT 1.0 stylesheet option.

在这一部分中,我尝试考虑了注释中提到的p元素的正常处理".

In this one I tried to take into account the "normal processing" of the p elements that was mentioned in the comments.

在示例中,我添加了一条处理指令,只是为了说明已处理了p.可以根据您在XSLT中的操作来替换或删除它.

In the example, I'm adding a processing instruction just to show that a p was processed. This can be replaced or removed depending on what you're doing in your XSLT.

XML输入

<body>
    <p class="exclude">delete1</p>
    <p>keep1</p>
    <p class="exclude">delete2</p>
    <p class="exclude">delete3</p>
    <p>keep2</p>
    <p class="exclude">delete4</p>
    <p>keep3</p>
    <p class="exclude">delete5</p>
    <p class="exclude">delete6</p>
    <p class="exclude">delete7</p>
    <p>keep4</p>
    <p>keep5</p>
    <p class="exclude">delete8</p>
</body>

XSLT 1.0 (已通过Xalan和Saxon 6.5.5测试)

XSLT 1.0 (Tested with Xalan and Saxon 6.5.5)

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

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

    <!--Strip p if class attribute value is exclude.-->
    <xsl:template match="p[@class='exclude']"/>

    <!--Special handling of the 4th non-exclude p. Call the "normal" template to 
        handle any other p processing and then output the additional HTML.-->
    <xsl:template match="p[not(@class='exclude')][count(preceding::p[not(@class='exclude')])+1=4]">
        <xsl:call-template name="normal"/>      
        <b>ADDITIONAL HTML HERE</b>
    </xsl:template>

    <!--This is the "normal" processing of "p". For the example, just adding a PI.-->
    <xsl:template match="p" name="normal">
        <xsl:copy>
            <xsl:processing-instruction name="processed">normally</xsl:processing-instruction>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>     
    </xsl:template>

</xsl:stylesheet>

输出

<body>
   <p><?processed normally?>keep1</p>
   <p><?processed normally?>keep2</p>
   <p><?processed normally?>keep3</p>
   <p><?processed normally?>keep4</p>
   <b>ADDITIONAL HTML HERE</b>
   <p><?processed normally?>keep5</p>
</body>

这篇关于匹配动态xslt模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆