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 开发者_如何学JAVAdocument 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 stylesheet (pull style):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="xhtml">
<xsl:template match="text()"/>
<xsl:template match="xhtml:div[@id='testid']">
<html lang="de">
<head></head>
<body>
<xsl:call-template name="identity"/>
</body>
</html>
</xsl:template>
<xsl:template match="node()|@*" mode="identity" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="identity"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
With this input:
<?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>
Output:
<html lang="de" xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<div id="testid" class="testclass">hello world!</div>
</body>
</html>
Note: Invoque identity rule from matching node.
This stylesheet (push style):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="xhtml">
<xsl:template match="/">
<html lang="de">
<head></head>
<body>
<xsl:apply-templates select="//xhtml:div[@id='testid']"/>
</body>
</html>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And "brick" template:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="xhtml">
<xsl:template match="/">
<html lang="de">
<head></head>
<body>
<xsl:copy-of select="//xhtml:div[@id='testid']"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="x">
<xsl:output indent="yes" encoding="iso-8859-1"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" lang="de">
<head/>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="x:div[@id='testid']">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
when applied on the provided XML document:
<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>
produces the wanted, correct result:
<?xml version="1.0" encoding="iso-8859-1"?>
<html lang="de" xmlns="http://www.w3.org/1999/xhtml">
<head />
<body>
<div id="testid" class="testclass">
hello world!
</div>
</body>
</html>
精彩评论