通过 XmlSlurper/XmlParser 从 xsd 文件获取命名空间 xmlns 的名称
Get the name of namespace xmlns from xsd file through XmlSlurper/XmlParser
我正在尝试通过 Groovy 获取 xml(xsd) 文件的命名空间。
XmlParsers 返回的节点和 XmlSlurper 的 GPathResult 似乎忽略了名称空间定义。
例如:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
试图通过
获取根节点的属性
rootNode.attributes()
只会检索:
targetNamespace:http://www.w3schools.com
elementFormDefault:qualified
并省略 xml:ns 和 xmlns 定义。相同的结果包含在 GPathResult 的 @ 属性中。
这似乎与节点 class 实现无关,并且依赖于所使用的 XmlParsers。
那么应该如何实现 XmlParser 以在节点中包含这些属性,以及将其留在 Groovy 之外的任何原因?
使用 XmlSlurper
constructor 的不同变体:
def xml = """<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
</xs:schema>
"""
assert new XmlSlurper(false, false, false).parseText(xml).attributes() == [
'xmlns:xs':'http://www.w3.org/2001/XMLSchema',
'targetNamespace':'http://www.w3schools.com',
'xmlns':'http://www.w3schools.com',
'elementFormDefault':'qualified'
]
我正在尝试通过 Groovy 获取 xml(xsd) 文件的命名空间。 XmlParsers 返回的节点和 XmlSlurper 的 GPathResult 似乎忽略了名称空间定义。
例如:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
试图通过
获取根节点的属性rootNode.attributes()
只会检索:
targetNamespace:http://www.w3schools.com
elementFormDefault:qualified
并省略 xml:ns 和 xmlns 定义。相同的结果包含在 GPathResult 的 @ 属性中。
这似乎与节点 class 实现无关,并且依赖于所使用的 XmlParsers。
那么应该如何实现 XmlParser 以在节点中包含这些属性,以及将其留在 Groovy 之外的任何原因?
使用 XmlSlurper
constructor 的不同变体:
def xml = """<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
</xs:schema>
"""
assert new XmlSlurper(false, false, false).parseText(xml).attributes() == [
'xmlns:xs':'http://www.w3.org/2001/XMLSchema',
'targetNamespace':'http://www.w3schools.com',
'xmlns':'http://www.w3schools.com',
'elementFormDefault':'qualified'
]