2012-12-26 3 views
3

pptx 파일을 구문 분석 중이므로 문제가 발생했습니다. 나는 sldMasterId 태그의 r:id 속성 값을 얻을 필요가네임 스페이스를 사용하여 XML 특성 값 가져 오기

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<p:presentation xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> 
    <p:sldMasterIdLst> 
    <p:sldMasterId id="2147483648" r:id="rId2"/> 
    </p:sldMasterIdLst> 
    <p:sldIdLst> 
    <p:sldId id="256" r:id="rId3"/> 
    </p:sldIdLst> 
    <p:sldSz cx="10080625" cy="7559675"/> 
    <p:notesSz cx="7772400" cy="10058400"/> 
</p:presentation> 

:이 소스 XML의 샘플입니다.

doc = Nokogiri::XML(path_to_pptx) 
doc.xpath('p:presentation/p:sldMasterIdLst/p:sldMasterId').attr('id').value 

반환 2147483648하지만 나는 r:id 속성 값 인 rId2이 필요합니다.

은 내가 attribute_with_ns(name, namespace) 방법을 찾았지만,

doc.xpath('p:presentation/p:sldMasterIdLst/p:sldMasterId').attribute_with_ns('id', 'r') 

반환 전무.

답변

1

http://nokogiri.org/Nokogiri/XML/Node.html#method-i-attributes

서로 다른 네임 스페이스 대신 된 attribute_node를 사용하여, 같은 이름의 속성을 구분해야하는 경우.

doc.xpath('p:presentation/p:sldMasterIdLst/p:sldMasterId').each do |element| 
    element.attribute_nodes().select do |node| 
    puts node if node.namespace && node.namespace.prefix == "r" 
    end 
end 
당신은 요소의 네임 스페이스를 참조하는 같은 방법으로 당신의 XPath의 속성의 네임 스페이스를 참조 할 수 있습니다
1

: 당신이 attribute_with_ns를 사용하려면

doc.xpath('p:presentation/p:sldMasterIdLst/p:sldMasterId/@r:id') 

을, 당신은 실제 네임 스페이스를 사용하지 않을 필요 단지 접두사 :

doc.at_xpath('p:presentation/p:sldMasterIdLst/p:sldMasterId') 
    .attribute_with_ns('id', "http://schemas.openxmlformats.org/officeDocument/2006/relationships") 
관련 문제