我正在开发一个项目,其中有包含 html 子节点的 xml 文件;有些具有 xmlns="http://www.w3.org/1999/xhtml"
属性,有些则没有。 (在前面的过程中,脚本检查 html 部分的格式是否正确,并在需要时使用 htmltidy 来清理它们)
是否有这样的 xml 文件:
<?xml version="1.0"?>
<root>
<html>
<h1>html with no namespace</h1>
<p>regular html.</p>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<h1>xhtml with namespace declaration</h1>
<p>this has a ns, so all tags under it share that ns.</p>
</html>
</root>
和这样的样式表:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<newroot>
<xsl:apply-templates/>
</newroot>
</xsl:template>
<xsl:template match="h1">
<newhead>
<xsl:apply-templates/>
</newhead>
</xsl:template>
<xsl:template match="p">
<newpara>
<xsl:apply-templates/>
</newpara>
</xsl:template>
</xsl:stylesheet>
将两个 html 集视为相同? 谢谢, 基点
最佳答案
对于像这样的简单匹配模式,最清晰的方法可能是在样式表中声明 html 命名空间
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:h="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="h">
然后在模板模式中列出两个替代方案
<xsl:template match="h1 | h:h1">
关于xml - 将相同标签、不同命名空间视为相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28485570/