For the (contrived) HTML example below:
<div>
<p>lorem <a href="lorem.html" target="_blank">ipsum</a></p>
<a href="foo.html" target="top">foo</a>
<p><img src="foo.jpg" class="bar"/></p>
<img src="bar.jpg" class="bar"/>
</div>
I'm trying to write an XSLT 1.0 transform which:
- whitelists top-level
<p>
- whitelists
href
attribute for<a>
- whitelists
src
attribute for<img>
- wraps top-level
<a>
and<img>
in<p>...</p>
Ideally this would be done is a way that allows adding more elements and attributes.
Expected output:
<div>
<p>lorem <a href="lorem.html">ipsum</a></p>
<p><a href="foo.html">foo</a></p>
<p><img src="foo.jpg"/></p>
<p><img src="bar.jpg"/></p>
</div>
The following XSLT 2.0 works thanks to <xsl:next-match>
:
Fiddle: https://xsltfiddle.liberty-development.net/6r5Gh3p:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/div">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- whitelist <p> as top-level element -->
<xsl:template match="/div/p">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- coerce top-level <img> and <a> as children <p> -->
<xsl:template match="/div/img|/div/a">
<p><xsl:next-match/></p>
</xsl:template>
<!-- whitelist href attribute for <a> -->
<xsl:template match="a">
<xsl:copy>
<xsl:copy-of select="@href"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- whitelist src attribute for <img> -->
<xsl:template match="img">
<xsl:copy>
<xsl:copy-of select="@src"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
In XSLT 1.0 there is no <next-match>
and using the template below only matches once, so <a>
and <img>
do get wrapped in <p>
but their attributes don't get whitelisted:
Fiddle: https://xsltfiddle.liberty-development.net/94rmq6r
<xsl:template match="/div/img|/div/a">
<p>
<xsl:copy><xsl:apply-templates/></xsl:copy>
</p>
</xsl:template>
Output:
<div>
<p>lorem <a href="lorem.html">ipsum</a></p>
<p><a>foo</a></p>
<p><img src="foo.jpg"/></p>
<p><img/></p>
</div>
How can this be accomplished in XSLT 1.0?