user3799826 user3799826 - 6 months ago 15
HTML Question

Xsl transformation, remove/move image tag

I have and XHtml document in this format. How can I use XSL to move all the img tags inside the images div.



<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Title</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div class="article">
<div class="docTitles">
<h1 class="title">doc title</h1>
</div>
<div class="section">
<h2 class="generic">subtitle</h2>
<p>
<img alt="Image" src="images/{D11F8168-E415-498A-BC1C-6FEF3514B120}.JPG" width="200" />paragraph</p>
</div>
</div>
<div id="images"></div>
</body>

</html>





Here is the output i am looking for, all the image tags moved inside the images div



<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Title</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<div class="article">
<div class="docTitles">
<h1 class="title">doc title</h1>
</div>
<div class="section">
<h2 class="generic">subtitle</h2>
<p>paragraph</p>
</div>
</div>
<div id="images">
<img alt="Image" src="images/{D11F8168-E415-498A-BC1C-6FEF3514B120}.JPG" width="200" />
</div>
</body>

</html>




Answer

Try:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="x:img"/>

<xsl:template match="x:div[@id='images']">
    <xsl:copy>
        <xsl:copy-of select="//x:img"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>