Bol*_*wyn 10 xml xslt namespaces list
我正在编写一个XSLT 1.0样式表来将多名称空间XML文档转换为HTML.在结果HTML的某个地方,我想列出文档中出现的所有命名空间.
这可能吗?
我想到了类似的东西
<xsl:for-each select="//*|//@*">
<xsl:value-of select="namespace-uri(.)" />
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
但当然我会得到重复的重复数据.所以我必须以某种方式过滤,我已经打印过.
递归调用模板会起作用,但我无法理解如何到达所有元素.
//@xmlns:*直接访问不起作用,因为无法通过XPath访问它(一个不允许将任何前缀绑定到xmlns:命名空间).
小智 7
另一个没有扩展功能:
<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>
Run Code Online (Sandbox Code Playgroud)
输出(使用Dimitre的输入样本):
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
Run Code Online (Sandbox Code Playgroud)
编辑:这个XPath表达式:
//*/namespace::*[not(. = ../../namespace::*|preceding::*/namespace::*)]
Run Code Online (Sandbox Code Playgroud)
作为证明,这个样式表:
<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>
Run Code Online (Sandbox Code Playgroud)
输出:
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
Run Code Online (Sandbox Code Playgroud)
编辑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(.,'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
输出:
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
Run Code Online (Sandbox Code Playgroud)
编辑 5:当您处理没有namespace轴实现的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(),'
')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
TransforMiix输出:
mynamespace2
Run Code Online (Sandbox Code Playgroud)
这种转变:
<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>
Run Code Online (Sandbox Code Playgroud)
应用于此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>
</authors>
Run Code Online (Sandbox Code Playgroud)
产生想要的,正确的结果:
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
Run Code Online (Sandbox Code Playgroud)
更新:
正如@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>
Run Code Online (Sandbox Code Playgroud)
命名空间"mynamespace2"将在输出中生成两次.
以下转换修复了此问题:
<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>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于上述XML文档时,它只生成文档中的所有唯一命名空间:
http://www.w3.org/XML/1998/namespace
mynamespace
mynamespace2
mynamespace3
Run Code Online (Sandbox Code Playgroud)
第二部分:XSLT 2.0解决方案.
XSLT 2.0解决方案是一个简单的XPath 2.0单行程序:
distinct-values(//namespace::*)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5754 次 |
| 最近记录: |