0

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.

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"

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

EDIT:: 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

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>

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

However, I need to take into account <p class="exclude">

Here is a more complicated example with <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>

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>

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.

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

4

2 回答 2

1

Here's another XSLT 1.0 stylesheet option.

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

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 Input

<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 (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>

Output

<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>
于 2012-06-15T04:11:57.273 回答
0

for each p you are processing, check...

<xsl:if test="count(preceding::p[not(@class='exclude')])=4">
   render your html here
</xsl:if>

Edit:

Addtionally to do with your further question:

<xsl:apply-tempates select="p[not(@class='exlude')]"/>

<xsl:template match="p">
   <p>
     <xsl:if test="count(preceding::p[not(@class='exclude')])=4">
      render your html here
   </xsl:if>
   </p>
</xsl:template>

if you don't want all the other p's you can put the count in the match.

EDIT:

I have looked at your updated question, and I am not sure how it works, as you have an unbalanced template (you have a closing p but not an opening one?) - I assume this is a typo. Also you say your p is within body, but you are matching on content/p. Where did the content tag come from.

Anyway, with the corrections, assuming your original template was:

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

and it worked, just change it to

just change it to be

<xsl:template match="p[count(preceding::p[not(@class='exclude')])=4]">
    <p>content include<xsl:apply-templates /></p> 
</xsl:template> 

that would give you the right thing

于 2012-06-14T15:29:46.000 回答