我使用Nokogiri解析XML,我试图获取节点及其值的属性,但我无法得到它。使用(Nokogiri::XML.parse(xml_record))
的解析XML看起来像:
#(Element:0x3fe544ad4544 {
name = "Article",
attributes = [ #(Attr:0x3fe54446c3c8 { name = "Id", value = "1980274" })],
children = [
#(Element:0x3fe54446b9b4 { name = "ArticleName", children = [ #(Text "100 Porsches and Me")] }),
#(Element:0x3fe544bf9744 { name = "Locked", attributes = [ #(Attr:0x3fe544bf96b8 { name = "Value", value = "0" })] }),
#(Element:0x3fe544bf8dbc { name = "OriginalTitle", attributes = [ #(Attr:0x3fe544bf8c54 { name = "N", value = "1" })] }),
#(Element:0x3fe544bf7dcc { name = "ProductionYear", attributes = [ #(Attr:0x3fe544bf7cb4 { name = "Value", value = "2006" })], children = [ #(Text "2006")] }),
#(Element:0x3fe544b84098 { name = "SalesStart", attributes = [ #(Attr:0x3fe544b83fd0 { name = "Value", value = "1391212800" })], children = [ #(Text "01.02.2014")] }),
#(Element:0x3fe544b834e0 { name = "SalesEnd", attributes = [ #(Attr:0x3fe544b83454 { name = "Value", value = "1548892800" })], children = [ #(Text "31.01.2019")] }),
#(Element:0x3fe545c52758 { name = "Price", attributes = [ #(Attr:0x3fe545c526b8 { name = "Value", value = "3.9900" })], children = [ #(Text "3,99")] }),
#(Element:0x3fe545c51c40 { name = "Runtime", attributes = [ #(Attr:0x3fe545c51b8c { name = "Value", value = "90" })], children = [ #(Text "90")] }),
#(Element:0x3fe545c51038 { name = "FeatureType", attributes = [ #(Attr:0x3fe545c50f98 { name = "Name", value = "FeatureFilm" })], children = [ #(Text "Film")] }),
#(Element:0x3fe54616158c { name = "OrderNumber", children = [ #(Text "LIG00000075")] }),
#(Element:0x3fe546160d80 { name = "ArticleCode", children = [ #(Text "LHE28409261")] }),
当我xml_rec.xpath('Article').attr(Id)
时,它返回nil。
我不想做" //文章"。
因为我将每个xml_rec
传递给一个方法,当我做的时候#34; //文章"我只得到第一个元素的ID。
有没有办法在Nokogiri中获取根元素的属性值?