Nokogiri 可以保留属性引用样式吗? [英] Can Nokogiri retain attribute quoting style?

查看:38
本文介绍了Nokogiri 可以保留属性引用样式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的文件的内容(注意嵌套的引号):

Here is the contents of my file (note the nested quotes):

<?xml version="1.0" encoding="utf-8"?>
        <property name="eventData" value='{"key":"value"}'/>

在 Ruby 中我有:

in Ruby I have:

file = File.read(settings.test_file)      
@xml = Nokogiri::XML( file)
puts "@xml  " + @xml.to_s

这是输出:

<property name="eventData" value="{&quot;key&quot;:&quot;value&quot;}"/>

有没有办法转换它以便输出可以准确地保留引号?即外单,内双?

Is there a way to convert it so the output would preserve the quotes exactly? i.e. single on the outside, double on the inside?

推荐答案

不,不能.Nokogiri::XML::Attr(也不是libxml2 中的底层数据结构)中没有存储关于用于(或应该)使用什么类型的引号来分隔属性的信息.因此,所有序列化(由 libxml2 完成)都使用相同的属性引用样式.

No, it cannot. There is no information stored in a Nokogiri::XML::Attr (nor the underlying data structure in libxml2) about what type of quotes were (or should be) used to delimit an attribute. As such, all serialization (done by libxml2) uses the same attribute quoting style.

事实上,这些信息甚至没有正确地保留在 XML 信息集中,因为 规范描述:

Indeed, this information is not even properly retained within the XML Information Set, as described by the specs:

以下信息未在当前版本的 XML 信息集中表示(此列表并非详尽无遗):

Appendix D: What is not in the Information Set

The following information is not represented in the current version of the XML Information Set (this list is not intended to be exhaustive):

[...]

17) 用于引用属性值的引号类型(单引号或双引号).

17) The kind of quotation marks (single or double) used to quote attribute values.

好消息是这两种 XML 序列化样式描述了完全相同的内容.坏消息是,除非您使用 规范 XML 序列化(Nokogiri 是还不能生产 最近才能够生产)有多种方式可以表示同一个文档,这些方式看起来像是对标准文本差异工具的许多虚假更改".

The good news is that the two XML serialization styles describe the exact same content. The bad news is that unless you're using a Canonical XML Serialization (which Nokogiri is not yet able to produce just recently able to produce) there are a large variety of ways to represent the same document that would look like many spurious 'changes' to a standard text-diffing tool.

也许如果您能描述一下您为何需要此功能(您尝试实现的最终目标是什么?),我们可以进一步帮助您.

Perhaps if you can describe why you wanted this functionality (what is the end goal you are trying to accomplish?) we could help you further.

您可能还对 感兴趣这个类似的问题.

这篇关于Nokogiri 可以保留属性引用样式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆