在使用 XSL 将 XHTML 转换为 XHTML 时,我遇到了 namespace 问题。考虑作为示例输入:
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Test</title></head>
<body>
<p>Remove this</p>
</body>
</html>
然后以下转换不起作用(例如,不删除
<p />
):<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()" name="copy">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<xsl:template match="p" />
</xsl:stylesheet>
但这个确实:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:template match="@*|node()" name="copy">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<xsl:template match="xhtml:p" />
</xsl:stylesheet>
我的问题和问题是:如何更改 XSLT 以便不必为所有 XHTML 元素添加前缀,并且仍然可以匹配它们?从我到目前为止所尝试的,添加一个默认命名空间,如
<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" />
没有做到这一点。谢谢你的帮助!
最佳答案
How can I change the XSLT so that I do not have to add prefixes to all the XHTML elements and it still get to match them?
这是可能的,但我建议定义命名空间并在引用此命名空间中的元素时使用前缀:
<xsl:template match="*[local-name()='p']" />
警告 :
只有保证没有两个元素具有相同的
local-name()
,这种技术才是安全的。但在不同的命名空间中。
关于xhtml - XHTML 的 XSL 转换中的命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12743331/