I'm trying to extract a div section from an xhtml document into another xhtml document by using xslt. However, I did not succeed. Rather, the xslt transformation resulted in a wired output. Suppose the following xhtml document to transform:
<?xml version="1.0" encoding="iso-8859-1"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="de">
<head>
<title>title test</title>
</head>
<body>
some blabla
<div>
<div id="testid" class="testclass">
hello world!
</div>
</div>
some other blabla <p/>
test paragraph<p/>
</body>
</html>
The xslt should extract the div section with the id "testid" and write it into a new xhtml document which should look as follows:
<?xml version="1.0" encoding="iso-8859-1"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="de">
<head>
<title>title test</title>
</head>
<body>
<div id="testid" class="testclass">
hello world!
</div>
</body>
</html>
My xslt code looks as follows:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" lang="de">
<head>
</head>
<body>
<xsl:apply-templates select="node()|text()"/>
</body>
</html>
</xsl:template>
<xsl:template match="*">
<xsl:if test="div[@id='testid']">
<xsl:copy-of select="*"/>
</xsl:if>
<xsl:apply-templates select="node()|@*" />
</xsl:template>
</xsl:stylesheet>
The output actually is as follows:
<html xmlns="http://www.w3.org/1999/xhtml" lang="de">
<head/>
<body>de
title test
some blabla
testidtestclass
hello world!
some other blabla
test paragraph
</body>
</html>
What do I need to change in the xslt in order to get the correct results? Any help is appreciated. Thanks.
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
This stylesheet (pull style):
With this input:
Output:
Note: Invoque identity rule from matching node.
This stylesheet (push style):
And "brick" template: