Xslt 如何将一个xhtml文档中的div节提取到另一个xhtml文档中
我试图使用xslt将一个div节从一个xhtml文档提取到另一个xhtml文档中。然而,我没有成功。相反,xslt转换产生了有线输出。假设要转换以下xhtml文档:Xslt 如何将一个xhtml文档中的div节提取到另一个xhtml文档中,xslt,html,copy,Xslt,Html,Copy,我试图使用xslt将一个div节从一个xhtml文档提取到另一个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> <
<?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>
<?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代码如下所示:
<?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>
<?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>
实际输出如下:
<?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>
<?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中更改什么?感谢您的帮助。谢谢。此样式表(拉式样式):
通过此输入:
<?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:copy-of select="//xhtml:div[@id='testid']"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
此转换:
<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>
应用于提供的XML文档时:
<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>
<?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>
标题测试
一些废话
你好,世界!
其他一些废话
测试段落
生成所需的正确结果:
<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>
<?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>
你好,世界!
我刚才注意到了xhtml名称空间的问题+好问题,+1。您的主要问题是没有在XSLT代码中定义XHTML名称空间。请参阅我的答案,以获得完整、简短和简单的解决方案。:)