Can Nokogiri Search for "Xml-Stylesheet" Tags

Can nokogiri handle css selector with optional tags?

In CSS you just use a comma to select multiple nodes:

document.css 'greeting, gruss'

If you want to be more specific you need to repeat the entire selector:

document.css 'hello-world greeting, hello-world gruss'

There’s no way to shorten this currently (something like the any psuedo-class could work, but isn’t available in Nokogiri).

In XPath you could do something like

document.xpath '//hello-world//*[name() = "greeting" or name() = "gruss"]'

which isn’t any shorter but means you avoid repeating the first part of the query.

You could also perhaps look at creating a custom function if this is something you plan to do a lot, which could be used from CSS or XPath.

How to do a Nokogiri search based on an XML attribute

This gives you the correct XPath initially, so that you don't have to expend the effort looping through mismatched elements to get your results:

document.xpath("//host[.//elem/@key='state']").each do |host|
ip_address = host.at_xpath("address[@addrtype='ipv4']").at_xpath("@addr").value
result = host.at_xpath("//ports//elem[@key='state']").content
puts "#{ip_address} #{result}"
end

This chooses all of the host elements that have a sub-element with a key attribute with a 'state' value. No need to check for correct matches within the loop, because this will actually only return the elements that you want. It's a complete XPath solution to your question.

How to retrieve the nokogiri processing instruction attributes?

I solved this problem by following instruction in below answer.

Can Nokogiri search for "?xml-stylesheet" tags?

Added new to_element method to Nokogiri::XML::ProcessingInstruction class

 class Nokogiri::XML::ProcessingInstruction
def to_element
document.parse("<#{name} #{content}/>")
end
end

style = xml.xpath('//processing-instruction("xml-stylesheet")').first
element = style.to_element

To retrieve the href attribute value

 element.attribute('href').value

Nokogiri get xpath from Nokogiri::XML::Element

rc is not an element-it's an array of matching elements:

results = rc.map do |node|
Nokogiri::CSS.xpath_for node.css_path
end

p results

Or, if you know there is only one matching element:

xpath = Nokogiri::CSS.xpath_for rc[0].css_path

Note that xpath_for returns an array, so you will need to extract the first element of the array:

xpath.first

How to search for prop elements in TMX with Nokogiri

The easiest way to traverse a node tree like this is using XPath. You've already used XPath for getting your top-level tu element, but you can extend XPath queries much further to get specific elements like you're looking for.

Here on DevHints is a handy cheat-sheet for what you can do with XPath.

Relative to your x variable which points to the tu element, here are the XPaths you'll want to use:

  • prop[@type="Att::Attribute1"] for finding your prop for Attribute 1
  • //seg or tuv/seg for finding the seg elements

Here's a complete code example using those XPaths. The at_xpath method returns one result, whereas the xpath method returns all results.

require 'nokogiri'

doc = File.open("test_for_import.xml") { |f| Nokogiri::XML(f) }

doc.xpath('//tu').each do |x|
puts "Creation date: " + x.attributes["creationdate"]
puts "User: " + x.attributes["creationid"]

# Get Attribute 1
# There should only be one result for this, so using `at_xpath`
attr1 = x.at_xpath('prop[@type="Att::Attribute1"]')
puts "Attribute 1: " + attr1.text

# Get each seg
# There will be many results, so using `xpath`
segs = x.xpath('//seg')
segs.each do |seg|
puts "Seg: " + seg.text
end
end

This outputs:

Creation date: 20181001T113609Z
User: some_user
Attribute 1: Value1
Seg: Testing
Seg: Testiranje

Can nokogiri use single quotes for attributes on saving xml?

It looks like the answer is no; not as the library is currently written, and maybe not at all. Tracing the call path for a node's serialization:

  • Nokogiri::XML::Node#to_s calls to_xml
  • Nokogiri::XML::Node#to_xml calls serialize (sets a few default options)
  • Nokogiri::XML::Node#serialize calls write_to
  • Nokogiri::XML::Node#write_to calls native_write_to
  • Nokogiri::XML::Node#native_write_to calls native_write_to, which looks like this:

&bsp;

def native_write_to(io, encoding, indent_string, options)
set_xml_indent_tree_output 1
set_xml_tree_indent_string indent_string
savectx = LibXML.xmlSaveToIO(IoCallbacks.writer(io), nil, nil, encoding, options)
LibXML.xmlSaveTree(savectx, cstruct)
LibXML.xmlSaveClose(savectx)
io
end

So, you are at the mercy of libxml at this point. Googling for libxml serialize single quote attributes does not immediately turn up any smoking guns.

I think you should file a feature request and see what sort of tenderlovin' you can get. :)

Rendering XML with Stylesheet in Rails

Take a look at Nokogiri gem: http://nokogiri.org/Nokogiri/XSLT/Stylesheet.html

doc   = Nokogiri::XML(File.read('some_file.xml'))
xslt = Nokogiri::XSLT(File.read('some_transformer.xslt'))

puts xslt.transform(doc)


Related Topics



Leave a reply



Submit