Using XSLT to process an HTML page and move a DIV from one place to another

I have an HTML page (fully valid) that is after XSLT processing.

Say the corresponding section of code is as follows:

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

XSLT should convert it like this:

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

Any ideas? I am stuck.

Change . Indicates that <div id="content">and are <div id="announcement">separated by another code.

+3
source share
1 answer

If the div announcementimmediately follows the div content, 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 enough to not touch on any other sections that may be in your document. If your situation allows, you can make it simpler / less specific to increase readability.

+2

Source: https://habr.com/ru/post/1737727/


All Articles