gpt4 book ai didi

xml - 使用 xsl/xpath 查找 xmlns

转载 作者:数据小太阳 更新时间:2023-10-29 02:05:53 42 4
gpt4 key购买 nike

我正在尝试使用转换获取 xml 文档的根元素的信息列表。我无法弄清楚的部分是:

如何获取默认命名空间 url?

如何获取每个 xmlns 前缀及其相关 url 的列表?

这是我的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/example.xsl"?>
<foo:mytag xmlns="http://default.example.com/"
xmlns:foo="http://foo.example.com/"
xmlns:bar="http://bar.example.com">
</foo:mytag>

这是我的 xsl 文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
doctype-public="-//W3C//DTD XHTML 1.1//EN"
doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
encoding="UTF-8"
indent="yes"
method="xml"
omit-xml-declaration="no"
version="1.0" />

<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
default namespace - <b>???</b><br />
full tag name - <b><xsl:value-of select="name(*)"/></b><br />
xmlns for tag - <b><xsl:value-of select="namespace-uri(*)"/></b><br />
tag prefix - <b>???</b><br />
tag name - <b><xsl:value-of select="local-name(*)"/></b><br />
List of xmlns - <br />
????
</body>
</html>
</xsl:template>

这个转换是在浏览器中完成的。基本上是我所期望的结果:

default namespace - http://default.example.com/
full tag name - foo:mytag
xmlns for tag - http://foo.example.com/
tag prefix - foo
tag name - mytag
List of xmlns -
- http://default.example.com/
foo - http://foo.example.com/
bar - http://bar.example.com/

我是否遗漏了一些明显的东西来填补缺失的数据?

最佳答案

我认为这大致符合您的要求。 XSLT 1.0 - 在 XMLSpy 中测试

<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
default namespace - <b><xsl:value-of select="*/namespace::*[not (name())]"/></b><br />
full tag name - <b><xsl:value-of select="name(*)"/></b><br />
xmlns for tag - <b><xsl:value-of select="namespace-uri(*)"/></b><br />
tag prefix - <b><xsl:value-of select="substring-before(name(*),':')" /></b><br />
tag name - <b><xsl:value-of select="local-name(*)"/></b><br />
List of xmlns - <br />
<xsl:for-each select="*/namespace::*">
<xsl:if test="not (name() = 'xml')"><xsl:text>
</xsl:text>
<xsl:value-of select="name()" /><xsl:text> - </xsl:text><xsl:value-of select="." /><br />
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>

关于xml - 使用 xsl/xpath 查找 xmlns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1319138/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com