开发者

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

开发者 https://www.devze.com 2022-12-24 01:28 出处:网络
I have a HTML page (fully valid) that is post-processed by XSLT.开发者_开发百科 Let\'s say the relevant section of code looks like this:

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消