xml – XSLT:如何获取所有使用的命名空间的列表

我正在编写一个XSLT 1.0样式表,将多命名空间 XML文档转换为HTML.在结果HTML的某个地方,我想列出文档中出现的所有命名空间.

这可能吗

我想到了像

<xsl:for-each select="//*|//@*">
  <xsl:value-of select="namespace-uri(.)" />
</xsl:for-each>

但是我当然会收到重复的数据.所以我必须过滤,我已经打印了.

递归调用模板可以工作,但是我不能围绕如何覆盖所有元素.

访问// @ xmlns:*直接不起作用,因为无法通过XPath访问(不允许将任何前缀绑定到xmlns:namespace).

另一个没有扩展功能:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="*">
        <xsl:param name="pNamespaces" select="'&#xA;'"/>
        <xsl:variable name="vNamespaces">
            <xsl:variable name="vMyNamespaces">
                <xsl:value-of select="$pNamespaces"/>
                <xsl:for-each select="namespace::*
                                        [not(contains(
                                                 $pNamespaces,concat('&#xA;',.,'&#xA;')))]">
                    <xsl:value-of select="concat(.,'&#xA;')"/>
                </xsl:for-each>
            </xsl:variable>
            <xsl:variable name="vChildsNamespaces">
                <xsl:apply-templates select="*[1]">
                    <xsl:with-param name="pNamespaces"
                                        select="$vMyNamespaces"/>
                </xsl:apply-templates>
            </xsl:variable>
            <xsl:value-of select="concat(substring($vMyNamespaces,1 div not(*)),substring($vChildsNamespaces,1 div boolean(*)))"/>
        </xsl:variable>
        <xsl:variable name="vFollowNamespaces">
            <xsl:apply-templates select="following-sibling::*[1]">
                <xsl:with-param name="pNamespaces" select="$vNamespaces"/>
            </xsl:apply-templates>
        </xsl:variable>
        <xsl:value-of
             select="concat(substring($vNamespaces,1 div not(following-sibling::*)),substring($vFollowNamespaces,1 div boolean(following-sibling::*)))"/>
    </xsl:template>
</xsl:stylesheet>

输出(使用Dimitre的输入样本):

http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3

编辑:此XPath表达式:

//*/namespace::*[not(. = ../../namespace::*|preceding::*/namespace::*)]

作为证明,此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:for-each select="//*/namespace::*
                                     [not(. = ../../namespace::*|
                                              preceding::*/namespace::*)]">
            <xsl:value-of select="concat(.,'&#xA;')"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

输出:

http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3

编辑4:与双程转换相同的效率.

这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:key name="kElemByNSURI"
             match="*[namespace::*[not(. = ../../namespace::*)]]"
              use="namespace::*[not(. = ../../namespace::*)]"/>
    <xsl:template match="/">
        <xsl:for-each select=
            "//namespace::*[not(. = ../../namespace::*)]
                           [count(..|key('kElemByNSURI',.)[1])=1]">
            <xsl:value-of select="concat(.,'&#xA;')"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

输出:

http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3

编辑5:当您处理没有命名空间ax实现的XSLT处理器(像TransforMiix)时,您只能提取与此样式表实际使用的命名空间:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:key name="kElemByNSURI" match="*|@*" use="namespace-uri()"/>
    <xsl:template match="/">
        <xsl:for-each select=
            "(//*|//@*)[namespace-uri()!='']
                       [count(.|key('kElemByNSURI',namespace-uri())[1])=1]">
            <xsl:value-of select="concat(namespace-uri(),'&#xA;')"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

TransforMix输出:

mynamespace2

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇