tags:

views:

40

answers:

1

I have a HTML page (fully valid) that is post-processed by XSLT.

Let's say the relevant section of code looks like this:

<div id="content"> ... </div>
...
<div id="announcement"> ... </div>

The XSLT needs to transform it to look like this:

<div id="content"> <div id="announcement"> ... </div> ... </div>

Any ideas? I'm stuck.

Edit: Indicated that <div id="content"> and <div id="announcement"> are separated by other code.

+2  A: 

If the announcement div directly follows the content div, use:

<xsl:template match="div[@id='content' and following-sibling::div[1][@id='announcement']">
  <!-- copy the content div and its attributes -->
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <!-- now make a copy of the directly following announcement div -->
    <xsl:copy-of select="following-sibling::div[1][@id='announcement']" />
    <!-- process the rest of the contents -->
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>

<!-- the empty template mutes an announcement div that follows a content div -->
<xsl:template match="div[@id='announcement' and preceding-sibling::div[1][@id='content']" />

The above is specific enough not to touch any other divs that might be in your document. If your situation allows, you can make it simpler/less specific to increase readability.

Tomalak
Unfortunately, it doesn't directly follow the content div; however, it should always be in the same place (relative to the content div). Perhaps I can modify your suggested code? Thank you!
Dave Mulder
@Dave: Sure, should not be too hard. The code I supplied takes the first following div if it has the id "announcement". You can reverse it `following-sibling::div[@id='announcement'][1]` or work with the `following::` axis and see where that gets you.
Tomalak