Xml 如何获取所有已使用名称空间的列表?
我正在编写一个XSLT1.0样式表,用于将多命名空间XML文档转换为HTML。在结果HTML的某个地方,我想列出文档中出现的所有名称空间 这可能吗 我想到了类似的事情Xml 如何获取所有已使用名称空间的列表?,xml,list,xslt,namespaces,Xml,List,Xslt,Namespaces,我正在编写一个XSLT1.0样式表,用于将多命名空间XML文档转换为HTML。在结果HTML的某个地方,我想列出文档中出现的所有名称空间 这可能吗 我想到了类似的事情 <xsl:for-each select="//*|//@*"> <xsl:value-of select="namespace-uri(.)" /> </xsl:for-each> 但我当然会得到无数的复制品。所以我必须以某种方式过滤我已经打印的内容 递归调用模板是可行的,但我无法理
<xsl:for-each select="//*|//@*">
<xsl:value-of select="namespace-uri(.)" />
</xsl:for-each>
但我当然会得到无数的复制品。所以我必须以某种方式过滤我已经打印的内容
递归调用模板是可行的,但我无法理解如何访问所有元素
直接访问
/@xmlns:
不起作用,因为不能通过XPath访问它(不允许将任何前缀绑定到xmlns:
名称空间)。此转换:
<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::*)]">
<xsl:value-of select="concat(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<authors xmlns:user="mynamespace">
<?ttt This is a PI ?>
<author xmlns:user2="mynamespace2">
<name idd="VH">Victor Hugo</name>
<user2:name idd="VH">Victor Hugo</user2:name>
<nationality xmlns:user3="mynamespace3">French</nationality>
</author>
</authors>
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output method="text"/>
<xsl:key name="kNSbyURI" match="n" use="."/>
<xsl:template match="/">
<xsl:variable name="vrtfNS">
<xsl:for-each select=
"//namespace::*[not(. = ../../namespace::*)]">
<n><xsl:value-of select="."/></n>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="vNS" select="ext:node-set($vrtfNS)/*"/>
<xsl:for-each select=
"$vNS[generate-id()
=
generate-id(key('kNSbyURI',.)[1])
]">
<xsl:value-of select="concat(., '
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
更新:
<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::*)]">
<xsl:value-of select="concat(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<authors xmlns:user="mynamespace">
<?ttt This is a PI ?>
<author xmlns:user2="mynamespace2">
<name idd="VH">Victor Hugo</name>
<user2:name idd="VH">Victor Hugo</user2:name>
<nationality xmlns:user3="mynamespace3">French</nationality>
</author>
</authors>
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output method="text"/>
<xsl:key name="kNSbyURI" match="n" use="."/>
<xsl:template match="/">
<xsl:variable name="vrtfNS">
<xsl:for-each select=
"//namespace::*[not(. = ../../namespace::*)]">
<n><xsl:value-of select="."/></n>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="vNS" select="ext:node-set($vrtfNS)/*"/>
<xsl:for-each select=
"$vNS[generate-id()
=
generate-id(key('kNSbyURI',.)[1])
]">
<xsl:value-of select="concat(., '
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
正如@svick所评论的,上述解决方案仍然会偶尔产生重复的名称空间,例如使用以下XML文档:
<authors xmlns:user="mynamespace">
<?ttt This is a PI ?>
<author xmlns:user2="mynamespace2">
<name idd="VH">Victor Hugo</name>
<user2:name idd="VH">Victor Hugo</user2:name>
<nationality xmlns:user3="mynamespace3">French</nationality>
</author>
<t xmlns:user2="mynamespace2"/>
</authors>
当此转换应用于上述XML文档时,它只生成文档中所有唯一的名称空间:
<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::*)]">
<xsl:value-of select="concat(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<authors xmlns:user="mynamespace">
<?ttt This is a PI ?>
<author xmlns:user2="mynamespace2">
<name idd="VH">Victor Hugo</name>
<user2:name idd="VH">Victor Hugo</user2:name>
<nationality xmlns:user3="mynamespace3">French</nationality>
</author>
</authors>
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output method="text"/>
<xsl:key name="kNSbyURI" match="n" use="."/>
<xsl:template match="/">
<xsl:variable name="vrtfNS">
<xsl:for-each select=
"//namespace::*[not(. = ../../namespace::*)]">
<n><xsl:value-of select="."/></n>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="vNS" select="ext:node-set($vrtfNS)/*"/>
<xsl:for-each select=
"$vNS[generate-id()
=
generate-id(key('kNSbyURI',.)[1])
]">
<xsl:value-of select="concat(., '
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
第二部分:XSLT2.0解决方案
XSLT 2.0解决方案是一个简单的XPath 2.0单行程序:
distinct-values(//namespace::*)
另一个不带扩展功能:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:param name="pNamespaces" select="'
'"/>
<xsl:variable name="vNamespaces">
<xsl:variable name="vMyNamespaces">
<xsl:value-of select="$pNamespaces"/>
<xsl:for-each select="namespace::*
[not(contains(
$pNamespaces,
concat('
',.,'
')))]">
<xsl:value-of select="concat(.,'
')"/>
</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>
编辑:还有这个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(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<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(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl: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(),'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
编辑4:与两次转换的效率相同
此样式表:
<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(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<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(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl: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(),'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
EDIT5:在处理没有名称空间的XSLT处理器时(如TransforMiix),您只能提取与此样式表实际使用的名称空间:
<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(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<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(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl: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(),'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
好问题(+1)。请参阅我的答案,以获得简短而有效的解决方案。:)我刚刚在我的答案中添加了一个XSLT2.0解决方案——只是为了完整性。如果在两个位置定义了一个名称空间,那么这将无法正常工作:
@svick:很好!我已经解决了这个问题。谢谢你的回答!我正在寻找一个非EXSLT解决方案,因为我需要模板是跨引擎可执行的。但是,如果我将来某个时候在XSLT2.0中重写它,我将返回节点集解决方案。谢谢。我还没有意识到在XSLT2.0中会这么容易。@Alejandro:+1获得正确答案。我们都知道,不使用xxx:node-set()
会导致效率更低的解决方案:)+1用于避免扩展的解决方案。我正在开发一个使用字符串来累积名称空间URI的应用程序,但您的可能更好。感谢您的解决方案!我错过了名称空间::
轴,而且我总是忘记
(尤其是当它的使用真的很有价值的时候)。@Boldewyn:你很好!我很高兴它能帮到你。@Alejandro:很好的解决方案,但是用count检查集合成员资格仍然效率很低。如果有人需要最有效的解决方案(如果要找到唯一的名称空间,这是必须的,因为文档中有太多的名称空间节点),那么使用两个过程的解决方案就是答案。