Nokogiri 在解析期间剥离属性名称空间
我有这个:
<style:style style:name="MYBOLD" style:family="text" style:parent-style-name="Standard">
<style:text-properties style:font-name="Arial" style:font-name-complex="Arial" fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
</style:style>
注意属性如何具有命名空间。
当我像这样解析它时:
Nokogiri::XML.fragment(xmltext)
它似乎丢失了标签和属性的名称空间!有什么办法可以保存这些吗?
I have this:
<style:style style:name="MYBOLD" style:family="text" style:parent-style-name="Standard">
<style:text-properties style:font-name="Arial" style:font-name-complex="Arial" fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
</style:style>
Note how the attributes have a namespace.
When I parse it like so:
Nokogiri::XML.fragment(xmltext)
It seems to have lost the namespaces for both the tags and the attributes! Is there any way I can preserve those?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Nokogiri 对命名空间一无所知,除了 (1) 根节点上的命名空间和 (2) 您自己注册的任何命名空间。由于根据定义,片段没有根节点,因此您必须自己提供名称空间信息。
我注意到 DocumentFragment 的构造函数有一个可选的 Context 节点,它可以从中收集名称空间信息。由于您在评论中指出您有一个将在其中插入片段的文档,因此只要在该文档中正确注册了命名空间,您就可以将其用于上下文信息。
Nokogiri is blind to namespaces except (1) those on the root node, and (2) any that you register yourself. Since a fragment by definition has no root node, you'll have to supply namespace information yourself.
I noticed that the constructor to DocumentFragment has an optional Context node, from which it can glean namespace information. Since you have indicated in your comments that you have a document in which you will insert the fragment, as long as the namespaces are properly registered in that document you can probably use it for the context information.