xml - 将相同标签、不同命名空间视为相同

标签 xml xslt xslt-1.0 xml-namespaces

我正在开发一个项目,其中有包含 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/

相关文章:

xml - 在 XPath XSL 中执行 "Group By"查询

php - PHP 中的 UTF-8 编码 xml

java - 如何在 XSLT 中迭代列表(例如 ArrayList)

xml - 使用 XSLT 重构 XML 节点

XSLT 选择具有给定值的另一个属性的节点的属性值

xml - 简单 XML 上每个循环的 XSLT

xslt - 在项目中声明全局变量并在xslt中使用它

java - Java 6 中对 xinclude 的默认支持?

xml - 如何使用 gokogiri (libxml2) 解析带有命名空间的 xml?

xml - 如何从 perl 中的 xml 中获取所需的元素?