我正在尝试使用 xslt 将一个 xhtml 文档中的 div 部分提取到另一个 xhtml 文档中。然而,我没有成功。相反,xslt 转换产生了有线输出。假设要转换以下xhtml文档:
<?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>
xslt 应提取 ID 为“testid”的 div 部分并将其写入新的 xhtml 文档,该文档应如下所示:
<?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>
我的 xslt 代码如下所示:
<?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>
实际上输出如下:
<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>
为了获得正确的结果,我需要在 xslt 中更改哪些内容?任何帮助表示赞赏。谢谢。
这个样式表(拉式):
<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>
通过此输入:
<?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:从匹配节点调用身份规则。
这个样式表(推送样式):
<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>
和“砖”模板:
<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)