Ruby XPath 和 XSLT
Ruby XPath 和 XSLT
Ruby XPath
Ruby XPath 是一种在 XML 文件中查找信息的语言。它是查看 XML 文件的替代方法。它用于浏览 XML 文档中的元素和属性,将该文档视为逻辑有序树。
Ruby XPath 对于从 XML 文件获取相关信息和属性非常有用。它需要基于树的解析。
示例:
#!/usr/bin/ruby-w require
'rexml/document' include REXML xmlfile = File.
new(
"trial.xml") xmldoc = Document.
new(xmlfile)
# Info for the first cloth found clothing = XPath.
first(xmldoc,
"//clothing") p clothing
# Print out all the cloth types XPath.
each(xmldoc,
"//type") { |e| puts e.text }
# Get an array of all of the cloth brands. names = XPath.
match(xmldoc,
"//brand").map {|x| x.text } p names
输出:
Ruby XSLT
Ruby XSLT 是一个基于 libxml 和 libxslt 的简单类。有两个 XSLT 解析器可用于 Ruby。
Ruby-Sablotron
XSLT4R
它可以根据 GNU 的条款自由分发。
Ruby-Sablotron
主要为Linux操作系统编写,由Masayoshi Takahashi编写。
它需要以下库:
黑斑
图标
外籍人士
XSLT4R
由 Michael Neumann 编写。它使用简单的命令行界面,也可以在第三方应用程序中使用来转换 XML 文档。
XSLT4R 需要 XMLScan 来操作,它包含在 XSLT4R 档案中。这些模块需要使用标准的 Ruby 安装方法进行安装。
语法:
ruby xslt.rb stylesheet.xsl document.xml [arguments]
要在应用程序中使用 XSLT4R,您需要包含 XSLT 并输入您需要的参数。