xml - 如何获取所有使用的命名空间的列表?

标签 xml list xslt namespaces

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

这可能吗?

我想过类似的事情

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

但当然我会得到无数的重复。所以我必须以某种方式过滤我已经打印的内容。

递归调用模板会起作用,但我无法理解如何访问所有元素。

访问 //@xmlns:*直接不起作用,因为不能通过 XPath 访问它(不允许将任何前缀绑定(bind)到 xmlns: 命名空间)。

最佳答案

另一个没有扩展功能:

<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:当您处理没有 namespace 的 XSLT 处理器时ax 实现(如 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>

TransforMiix 输出:
mynamespace2

关于xml - 如何获取所有使用的命名空间的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4149843/

相关文章:

java - 使用 XSLT 漂亮地打印 XML/XHTML,而不会损坏 namespace 信息

python - 使用 Python 的维基百科

Python:按位列表操作

c# - 如何对类型列表集合进行排序

list - takeWhile 测试嵌套列表的长度

java - 使用通过 xslt 从 xml 转换为文本文件时抛出异常

c# - XSL 转换 XML->XML 仅输出文本?

java - XML 的注解 @JsonDeserialize 的等价物

xml - 使用 Apache Flink 将数据推送到 S3

java - 处理多个 XSLT 并将结果传递给另一个 XSLT 以在 Java 中进行转换