使用 nokogiri 构建器添加 dtd

发布于 2024-08-16 12:44:37 字数 439 浏览 1 评论 0原文

我正在使用 nokogiri 生成 svg 图片。我想添加正确的 xml 前导码和 svg DTD 声明以获得类似以下内容:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 
<svg>
...

使用 builder 我可以使用 instruct!declare! 但是我想坚持使用 nokogiri 因为我在项目中将它用于其他目的,并且我希望保持较低的要求。你有什么想法吗?

谢谢

I am using nokogiri to generate svg pictures. I would like to add the correct xml preamble and svg DTD declaration to get something like:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 
<svg>
...

With builder I could use instruct! and declare! but I want to stick with nokogiri because I use it for other purpose in my project and I want to stay low on requirements. Do you have some ideas ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

美人迟暮 2024-08-23 12:44:37

以下摘自 Nokogiri::XML::Builder 页面底部的注释 (也许是最近添加的),我认为这可以解决问题:

builder = Nokogiri::XML::Builder.new do |xml|
  xml.doc.create_internal_subset(
    'html',
    "-//W3C//DTD HTML 4.01 Transitional//EN",
    "http://www.w3.org/TR/html4/loose.dtd"
  )
  xml.root do
    xml.foo
  end
end

puts builder.to_xml

Following is from a note at the bottom of the Nokogiri::XML::Builder page (maybe added recently), which I think will do the trick:

builder = Nokogiri::XML::Builder.new do |xml|
  xml.doc.create_internal_subset(
    'html',
    "-//W3C//DTD HTML 4.01 Transitional//EN",
    "http://www.w3.org/TR/html4/loose.dtd"
  )
  xml.root do
    xml.foo
  end
end

puts builder.to_xml
是伱的 2024-08-23 12:44:37

您现在可以(不知道从哪个版本)使用 Node#create_internal_subset 来创建 DTD 节点。有关详细信息,请参阅:http://nokogiri.org/Nokogiri/XML/Builder.html

向下滚动到“文档类型”部分以获取示例。

You can now (don't know from which version) use Node#create_internal_subset to create the DTD node. For more info see: http://nokogiri.org/Nokogiri/XML/Builder.html

And scroll down to the "Document Types" section for an example.

十二 2024-08-23 12:44:37

这是一个可能的解决方案,尽管它看起来像是一个肮脏的把戏:

#1. I build the svg document
builder = Nokogiri::XML::Builder.new do |xml|
  xml.svg do
    # ...
  end
end

#2. I retrieve the svg root node
svg = builder.doc.xpath("/svg").first

#3. I define and parse an xml document with the required preamble and dtd
str =<<EOS
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1. /DTD/svg11.dtd">
EOS
doc = Nokogiri::XML::Document.parse(str)

#4. I add the svg node to the document above
doc.add_child(svg)

Here is a possible solution, though it looks like a dirty trick:

#1. I build the svg document
builder = Nokogiri::XML::Builder.new do |xml|
  xml.svg do
    # ...
  end
end

#2. I retrieve the svg root node
svg = builder.doc.xpath("/svg").first

#3. I define and parse an xml document with the required preamble and dtd
str =<<EOS
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1. /DTD/svg11.dtd">
EOS
doc = Nokogiri::XML::Document.parse(str)

#4. I add the svg node to the document above
doc.add_child(svg)
前事休说 2024-08-23 12:44:37

对于那些使用 HTML 的人来说,Eric Walkers 的示例不太有效,因为文档类型是自动添加的。您需要先删除它:

builder = Nokogiri::HTML::Builder.new do |html|
  html.doc.internal_subset.remove
  html.doc.create_internal_subset('html', nil, nil)
end

这将添加 HTML5 doctype 又名

For those working with HTML, Eric Walkers' example doesn't quite work since a doctype is automatically added. You need to remove it first:

builder = Nokogiri::HTML::Builder.new do |html|
  html.doc.internal_subset.remove
  html.doc.create_internal_subset('html', nil, nil)
end

This will add the HTML5 doctype a.k.a. <!DOCTYPE html>

愛上了 2024-08-23 12:44:37

似乎没有任何方法可以使用 Nokogiri::XML::Builder 添加文档类型。然而,向 XML 文档添加 doctype 声明的实用性是可疑的,除非您的工具需要它。阅读 DTD 在 Web 上不起作用,作者 Henri Sivonen,因为某些原因它不是一个使用 DTD 是个好主意,您应该确保您的文档格式良好,并根据外部模式(可能是 DTD,或者可能是更强大的东西,如 XSD 或 RELAX-NG)而不是 DTD 来验证它嵌入到文档中。

There does not appear to be any way to add a doctype using Nokogiri::XML::Builder. However, adding a doctype declaration to an XML document is of dubious utility, unless your tools require it. Read DTDs Don't Work on the Web by Henri Sivonen for some reasons why it's not a very good idea to use DTDs, and you should instead ensure your document is well-formed, and validate it against an external schema (which may be a DTD, or may be something more powerful like XSD or RELAX-NG) rather than a DTD embedded within the document.

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