用Nokogiri解析后URL被更改

时间:2019-03-25 21:21:37

标签: ruby xml parsing nokogiri

str = "<?xml version='1.0' encoding='utf-8'?><url>https://somehost?p1=v1&p2=v2</url>"
=> "<?xml version='1.0' encoding='utf-8'?><url>https://somehost?p1=v1&p2=v2</url>"

x = Nokogiri::XML(str)
=> #<Nokogiri::XML::Document:0x3fcaa893b900 name="document" children=[#<Nokogiri::XML::Element:0x3fcaa893b644 name="url" children=[#<Nokogiri::XML::Text:0x3fcaa893b48c "https://somehost?p1=v1=v2">]>]>

为什么删除“&p2”?解析后?

1 个答案:

答案 0 :(得分:3)

这是因为在XML / HTML上下文中,&具有特殊含义。您必须逃脱它:

<?xml version='1.0' encoding='utf-8'?><url>https://somehost?p1=v1&amp;p2=v2</url>

将其解析为实体&p2,因为它不是有效的实体,所以它被删除,留下p1=v1=p2