Nokogiri 在解析期间剥离属性名称空间

发布于 2024-12-24 22:23:36 字数 467 浏览 1 评论 0原文

我有这个:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

机场等船 2024-12-31 22:23:36

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文