Nokogiri 无法从 svg 获取 g 标签
Nokogiri can't get g tags from svg
我有一个这样的 svg 文件:
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" height="700" width="1024" viewBox="0 0 1024 700" shape-rendering="geometricPrecision" text-rendering="geometricPrecision" image-rendering="optimizeQuality" fill-rule="evenodd" clip-rule="evenodd" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="L_691200_group">
<g id="L_691200_background">
<path stroke-width="5" fill="none" fill-rule="nonzero" stroke="black" d="..."/>
</g>
<g id="L_691200_path"><path d="..."/></g>
<g id="L_691200_icon"><circle cx="387.413" cy="485.451" r="7.99993" stroke-width="1" stroke="black"/><circle cx="478.876" cy="379.327" r="2.50001" stroke-width="1" stroke="black"/><circle cx="276.717" cy="593.047" r="2.50001" stroke-width="1" stroke="black"/></g>
<g id="L_691200_symbol">
<path fill="white" fill-rule="nonzero" d="..."/>
</g>
</g>
</svg>
我尝试使用 Nokogiri 在 Ruby 中解析它,但我无法获取 g 标签。
我尝试了各种方法:
groups = doc.xpath('//g')
groups = doc.xpath('//xmlns:g')
groups = doc.xpath('//g', 'g' => 'http://www.w3.org/2000/svg')
groups = doc.css('g')
但是群组总是空的。我让它工作的唯一方法是从 doc 中删除命名空间。但是我不能那样做,因为我需要在更改一些内容后再次保存文档。
如何在不删除命名空间的情况下获取 g 标签?
听起来你有一个诱导错误。为了代码格式,我将 post 作为答案。
▶ Nokogiri::VERSION
#⇒ "1.5.4"
▶ xml = Nokogiri.parse '<?xml version="1.0" encoding="UTF-8"?>...'
▶ xml.xpath('//xmlns:g').size
#⇒ 5
除此之外,xml.css 'g'
的工作方式相同。
我有一个这样的 svg 文件:
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" height="700" width="1024" viewBox="0 0 1024 700" shape-rendering="geometricPrecision" text-rendering="geometricPrecision" image-rendering="optimizeQuality" fill-rule="evenodd" clip-rule="evenodd" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="L_691200_group">
<g id="L_691200_background">
<path stroke-width="5" fill="none" fill-rule="nonzero" stroke="black" d="..."/>
</g>
<g id="L_691200_path"><path d="..."/></g>
<g id="L_691200_icon"><circle cx="387.413" cy="485.451" r="7.99993" stroke-width="1" stroke="black"/><circle cx="478.876" cy="379.327" r="2.50001" stroke-width="1" stroke="black"/><circle cx="276.717" cy="593.047" r="2.50001" stroke-width="1" stroke="black"/></g>
<g id="L_691200_symbol">
<path fill="white" fill-rule="nonzero" d="..."/>
</g>
</g>
</svg>
我尝试使用 Nokogiri 在 Ruby 中解析它,但我无法获取 g 标签。
我尝试了各种方法:
groups = doc.xpath('//g')
groups = doc.xpath('//xmlns:g')
groups = doc.xpath('//g', 'g' => 'http://www.w3.org/2000/svg')
groups = doc.css('g')
但是群组总是空的。我让它工作的唯一方法是从 doc 中删除命名空间。但是我不能那样做,因为我需要在更改一些内容后再次保存文档。
如何在不删除命名空间的情况下获取 g 标签?
听起来你有一个诱导错误。为了代码格式,我将 post 作为答案。
▶ Nokogiri::VERSION
#⇒ "1.5.4"
▶ xml = Nokogiri.parse '<?xml version="1.0" encoding="UTF-8"?>...'
▶ xml.xpath('//xmlns:g').size
#⇒ 5
除此之外,xml.css 'g'
的工作方式相同。