简体   繁体   中英

Using XSLT how do I add element around text before and after element in mixed content?

This is the structure of my XML-document:

<body><p>Some text <em>before</em> image<img src="" width="" height=""/>some text <b>after</b> image</p></body>

After processing it should look like this:

<body><p>Some text <em>before</em> image</p><img src="" width="" height=""/><p>some text <b>after</b> image</p></body>

How can I add the p elements to the text before and after an img element?

For XSLT 2.0:

<xsl:template match="p[img]">
  <xsl:for-each-group select="node()" group-adjacent="boolean(self::img)">
    <xsl:choose>
      <xsl:when test="self::img">
        <xsl:copy-of select="."/>
      </xsl:when>
      <xsl:otherwise>
        <p><xsl:copy-of select="current-group()"/></p>
      </
    </
  </
</

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM