如何获得所有使用的名称空间的列表? [英] How to get a list of all used namespaces?

查看:56
本文介绍了如何获得所有使用的名称空间的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing an XSLT 1.0 stylesheet to transform multi-namespace XML documents to HTML. At some place in the result HTML I want to list all the namespaces, that occured in the document.

这可能吗?

我想到了类似的东西

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

但是我当然会得到数以百万计的重复.因此,我必须以某种方式过滤我已经打印的内容.

but of course I'd get gazillions of duplicates. So I'd have to filter somehow, what I already printed.

递归调用模板可以工作,但是我无法解决所有元素的问题.

Recursively calling templates would work, but I can't wrap my head around how to reach all elements.

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

Accessing //@xmlns:* directly doesn't work, because one can't access this via XPath (one isn't allowed to bind any prefix to the 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的输入样本):

Output (With Dimitre's input sample):

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

EDIT :也是该XPath表达式:

EDIT: Also this XPath expression:

//*/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

与两次通过转换相同.

EDIT 4: Same efficient as two pass transformation.

此样式表:

<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 ax实现(如TransforMiix)的XSLT处理器时,只能提取此样式表实际使用的命名空间:

EDIT 5: When you are dealing with a XSLT processor without namespace axe implementation (Like TransforMiix), you can only extract namespaces actually used with this stylesheet:

<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输出:

TransforMiix output:

mynamespace2

这篇关于如何获得所有使用的名称空间的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆