Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Nokogiri可以搜索“吗?”;?“xml样式表”;标签?_Ruby_Xml_Nokogiri - Fatal编程技术网

Ruby Nokogiri可以搜索“吗?”;?“xml样式表”;标签?

Ruby Nokogiri可以搜索“吗?”;?“xml样式表”;标签?,ruby,xml,nokogiri,Ruby,Xml,Nokogiri,我需要解析XML样式表: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="/templates/xslt/inspections/disclaimer_en.xsl"?> 但我得到了一个错误: `on_error': unexpected '?' after '' (Nokogiri::CSS::SyntaxError) Nokogiri无法搜索作为XML处理指

我需要解析XML样式表:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/templates/xslt/inspections/disclaimer_en.xsl"?>
但我得到了一个错误:

`on_error': unexpected '?' after '' (Nokogiri::CSS::SyntaxError)

Nokogiri无法搜索作为XML处理指令的标记。您可以这样访问它们:

doc.children[0]

这不是一个XML元素;这是一个XML。这就是为什么您无法通过查询找到它。要找到您想要的:

#查找第一个xml样式表PI
xss=doc.at_xpath('//处理指令(“xml样式表”))
#查找每个xml样式表
xsss=doc.xpath('//处理指令(“xml样式表”))
在行动中看到:

需要“nokogiri”
xml=type=“text/xsl”href=“/templates/disclaimer\u en.xsl”
因为处理指令不是元素,所以它没有属性;例如,您不能要求
xss['type']
xss['href']
;如果愿意,您需要将内容解析为元素。一种方法是:

类Nokogiri::XML::ProcessingInstruction def至_元件 document.parse(“”) 结束 结束 p xss.to_元素['href']#=>“/templates/disclaimer_en.xsl”
请注意,Nokogiri或libxml2中存在这样的情况,即如果在
-1之前至少有一个字符(可以是空格),则会导致在文档中以a的形式出现。这在Nokogiri 1.5.5上对我不起作用:
p Nokogiri.XML(“”)。children[0]#=>##
两年前确实有效,但Nokogiri可以从内部进行更改。当然,在写这个答案的时候,我测试了我的代码。我真的相信你。我在内心挣扎着如何处理旧的答案,这些答案曾经明确正确,但现在不再有效。我目前的信念是,如果存在其他正确答案,那么这些答案应该被否决。我不想惩罚你(当然不是!),而是想表明答案(不再)有用。请注意,如果你知道每个PI都位于文档的根上,而不是文档深处另一个元素的子元素,你可以通过使用
xpath('/processing instruction(“xml样式表”)来提高搜索性能
doc.children[0]