我正在尝试使用 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>
输出:
<html lang="de" xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<div id="testid" class="testclass">hello world!</div>
</body>
</html>
注意:从匹配节点调用身份规则。
此样式表(推送样式):
<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>
关于xslt - 如何从一个 xhtml 文档中提取一个 div 部分到另一个 xhtml 文档中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5097063/