有人知道如何用 Python 处理这种 XML 注释,这是我第一次看到这个。
<?link id="752760" resource-uuid="UUID-9f0575a3-1847-1cde-fd35-f18014fdecf3" resource-id="570935" resource-type="fork" type="ResourceLink"?>
我需要查询这种“元素”来获取resource-uuid
值。
有人知道如何用 Python 处理这种 XML 注释,这是我第一次看到这个。
<?link id="752760" resource-uuid="UUID-9f0575a3-1847-1cde-fd35-f18014fdecf3" resource-id="570935" resource-type="fork" typ
有人知道如何用 Python 处理这种 XML 注释,这是我第一次看到这个。
<?link id="752760" resource-uuid="UUID-9f0575a3-1847-1cde-fd35-f18014fdecf3" resource-id="570935" resource-type="fork" type="ResourceLink"?>
我需要查询这种“元素”来获取resource-uuid
值。
我正在使用OpenFace训练我自己的分类器。它对我来说工作正常,但我不想从我的命令中传递泡菜文件和图像,就像我希望它指定泡菜文件路径和图像路径一样。我已经浏览了它使用argparse的代码。我无法理解如何在不在命令行中传递任何参数的情况下指定路径。我试图nargs=?
使它成为可选参数,但我无法做到,因为它仍然显示error: too few arguments
。
请帮助我如何实现这一目标。
感谢大家,我了解了处理指令,并对此进行了研究,以了解如何处理它,如果有人需要它,我会从头开始:
from lxml import etree
...
file = 'path/to/file.xml'
tree = etree.parse(file)
result = tree.xpath('//processing-instruction("link")')
for pi in result:
# Each pi is a processing instruction tagged as 'link'
if pi.get('type').__str__() == 'ResourceImport':
# PI with type = ResourceImport
print pi.text # Check the text of tis PI
使用lxml
库很容易获得使用 XPath 的处理指令。
我希望这个代码片段对因为这个问题而来到这里的人有所帮助。
您必须区分处理指令和xml 声明
两者的写法相同:<?SomeName SomeContent ?>
.
在第 2.6 节中查找详细信息!
虽然 xml 声明必须放在首位并以<?xml
其他处理指令开头,但可能(几乎)出现在 XML 中的任何位置。
处理指令必须有名称,而内容不像元素的内容那样受到形式上的限制。它是自由文本...
所以这是格式良好的 XML:
<root>
<a>test</a>
<?piName some test?>
</root>
我不使用 python,但这会在 SQL-Server 中返回你的 PI:
DECLARE @xml XML=
N'<root>
<a>test</a>
<?link id="752760" resource-uuid="UUID-9f0575a3-1847-1cde-fd35-f18014fdecf3" resource-id="570935" resource-type="fork" type="ResourceLink"?>
</root>';
SELECT @xml.query('/root/processing-instruction("link")');
即使您的内容看起来像属性:在 PI 中,内容是自由文本。所以你必须从内容中解析出你的信息......
这个答案可能会对你有所帮助。
如果您的处理器支持 XQuery 3.1,这是解决问题的一种方法:
declare function local:values($pi) {
map:merge(
for $pair in tokenize($pi)
let $key := substring-before($pair, '=')
let $value := replace(substring-after($pair, '='), '^"|"$', '')
return map:entry($key, $value)
)
};
let $xml := document {
<xml>
<?link id="752760" resource-uuid="UUID-9f0575a3-1847-1cde-fd35-f18014fdecf3"
resource-id="570935" resource-type="fork" type="ResourceLink"?>
</xml>
}
for $pi in $xml//processing-instruction('link')
let $values := local:values($pi)
return $values?resource-uuid
旧版本 XQuery 的另一种解决方案:
let $xml := document {
<xml>
<?link id="752760" resource-uuid="UUID-9f0575a3-1847-1cde-fd35-f18014fdecf3"
resource-id="570935" resource-type="fork" type="ResourceLink"?>
</xml>
}
for $pi in $xml//processing-instruction('link')
for $pair in tokenize($pi, '\s+')[substring-before(., '=') = 'resource-uuid']
return replace(substring-after($pair, '='), '^"|"$', '')
这两个代码片段都在假设您的处理指令中的值全部按照您的示例组成(键和值用等号分隔,值用双引号括起来)的假设下工作。
您所指的“注释”称为处理指令。
keyword="value"
在处理指令时使用类似于 XML 元素属性的语法是很常见的,但不幸的是,这只是一种约定,而不是 XML 固有的东西,因此您必须自己解析内容以提取属性。(撒克逊人对此有一个功能saxon:get-pseudo-attribute()
)。
如果您使用的是 Python,那么在 Python 代码而不是 XPath 代码中执行这个额外的解析阶段可能更简单——除非您实际上需要将该值作为某个更大的 XPath 表达式的一部分,在这种情况下,详细信息取决于您是否使用XPath 或 XQuery 以及哪个版本。