根节点的xpath属性

时间:2012-02-18 17:14:20

标签: xml xpath nokogiri

我想在根元素中获取ID,LASTEDITED,EXPIRESS属性。我正在使用xpath,ruby和nokogiri。但它有效吗,任何想法?

xPath查询:

  doc.xpath('/educationProvider/@id').each do |id_node| 
    puts node.content
  end

  doc.xpath('/educationProvider/@lastEdited').each do |lastedited_node|
    puts lastedited_node.content
  end

  doc.xpath('/educationProvider/@expires').each do |expires_node|
    puts expires_node.content
  end

我的XML如何:

<?xml version="1.0" encoding="UTF-8"?>
<p:educationProvider xmlns:p="http://skolverket.se/education/provider/1.0" xmlns="http://skolverket.se/education/commontypes/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expires="2015-01-31" id="provider.uh.msb" lastEdited="2012-11-01T12:51:37" xsi:schemaLocation="http://skolverket.se/education/provider/1.0 educationProvider.xsd">
        <p:vCard>
            <VERSION/> 
            <FN/> 
            <N/> 
            <ADR>
                <LOCALITY>KARLSTAD</LOCALITY> 
                <PCODE>651 81</PCODE> 
            </ADR>
            <TEL>
                <NUMBER>0771-240240</NUMBER> 
            </TEL>
            <EMAIL>
                <USERID>utbildning@msbmyndigheten.se</USERID> 
            </EMAIL>
            <ORG>
                <ORGNAME>Myndigheten för samhällsskydd och beredskap</ORGNAME> 
            </ORG>
            <URL>http://www.msbmyndigheten.se</URL>
        </p:vCard>
    </p:educationProvider>

这是我的RUBY-SCRIPT:

require 'rubygems'
require 'nokogiri'
require 'open-uri'

# parse the HTML document with all the links to the XML files.
doc = Nokogiri::HTML(open('http://testnavet.skolverket.se/SusaNavExport/EmilExporter?GetEvent&EMILVersion=1.1&NotExpired&EIAcademicType=UoH&SelectEP'))
# URLS - array
@urls = Array.new 
#Get all XML-urls and save them in urls-array
doc.xpath('//a/@href').each do |links|
  @urls << links.content
end

@id = Array.new
@lastedited = Array.new
@expires = Array.new

# loop all the url of the XML files
@urls.each do |url|
  doc = Nokogiri::HTML(open(url))
  # grab the content I want
  doc.xpath('/educationProvider/@id').each do |id_node| 
    id_node.content
  end

  doc.xpath('/educationProvider/@lastEdited').each do |lastedited_node|
    @lastedited << lastedited_node.content
  end

  doc.xpath('/educationProvider/@expires').each do |expires_node|
    @expires << expires_node.content
  end
end

#print it out
(0..@id.length - 1).each do |index|
  puts "ID: #{@id[index]}"
  puts "Lastedited: #{@lastedited[index]}"
  puts "Expiress: #{@expires[index]}"
end

2 个答案:

答案 0 :(得分:8)

  

我想在根目录中获取ID,LASTEDITED,EXPIRESS属性   元件。

只需使用

/*/@id

这将选择XML文档顶部元素的id属性。

/*/@lastEdited

这将选择XML文档顶部元素的lastEdited属性。

/*/@expires

这将选择XML文档顶部元素的expires属性。

或者,可以使用单个XPath表达式选择所有这三个属性

/*/@*[contains('|id|lastEdited|expires|', 
               concat('|', name(), '|')
               )
     ]

基于XSLT的验证:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:for-each select=
  "/*/@*[contains('|id|lastEdited|expires|',
                  concat('|', name(), '|')
                  )
         ]">
   <xsl:value-of select=
   "concat('&#xA;',
           name(),
           ' = ',
           .
          )"/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用此XSLT转换时

<p:educationProvider xmlns:p="http://skolverket.se/education/provider/1.0" xmlns="http://skolverket.se/education/commontypes/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expires="2015-01-31" id="provider.uh.msb" lastEdited="2012-11-01T12:51:37" xsi:schemaLocation="http://skolverket.se/education/provider/1.0 educationProvider.xsd">
    <p:vCard>
        <VERSION/>
        <FN/>
        <N/>
        <ADR>
            <LOCALITY>KARLSTAD</LOCALITY>
            <PCODE>651 81</PCODE>
        </ADR>
        <TEL>
            <NUMBER>0771-240240</NUMBER>
        </TEL>
        <EMAIL>
            <USERID>utbildning@msbmyndigheten.se</USERID>
        </EMAIL>
        <ORG>
            <ORGNAME>Myndigheten för samhällsskydd och beredskap</ORGNAME>
        </ORG>
        <URL>http://www.msbmyndigheten.se</URL>
    </p:vCard>
</p:educationProvider>

评估Xpath表达式,并为每个选定的属性输出其名称和值

expires = 2015-01-31
id = provider.uh.msb
lastEdited = 2012-11-01T12:51:37

答案 1 :(得分:0)

如果您只想访问文档中的根节点,可以执行以下操作:

root = doc.root
root_id = root['id']
last_edited = root['lastEdited']

如果需要使用XPath找到它,则需要使用正确的命名空间。您的根节点的名称空间为“p”,因此您必须执行此操作:

doc.xpath('/p:educationProvider/@id').first.value

请注意节点名称前面的p: