2009-08-28 2 views
1

XML 파일을 편집하는 데 문제가 있습니다. 나는 현재 Nokogiri을 사용하려고하지만이 문제를 해결하기 위해 다른 Ruby 라이브러리에 열려 있습니다.Nokogiri를 사용하여 XML 파일에 네임 스페이스가있는 노드 추가

다른 노드 집합 안에 노드 집합을 추가하려고합니다. 두 가지 모두 흥미로운 네임 스페이스를 가지고 있습니다. 여기에 코드가 있습니다. 내가 바로 첫 번째 <p:sp>

require 'rubygems' 
require 'nokogiri' 

parent = <<EOF 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" mc:PreserveAttributes="mv:*"> 
    <p:spTree> 
    <p:sp> 
     <p:nvSpPr> 
     <p:cNvPr id="1" name="Title 1"/> 
     </p:nvSpPr> 
    </p:sp> 
    </p:spTree> 
</p:sld> 
EOF 

new_node = <<EOF 
<p:sp> 
    <p:cNvPr id="2" name="Title 2"/> 
    <a:off x="1524000" y="4572000"/> 
</p:sp> 
EOF 

@doc = Nokogiri::XML(parent) 
@doc.xpath('.//p:sp').after(new_node) 

@doc 후 부모에게 new_node를 추가하기 위해 노력하고있어 보이는 위의 코드 후 아래의 XML과 같이 실행됩니다 :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" mc:PreserveAttributes="mv:*"> 
    <p:spTree> 
    <p:sp> 
     <p:nvSpPr> 
     <p:cNvPr id="1" name="Title 1"/> 
     </p:nvSpPr> 
    </p:sp> 

    <p:p:sp> 
    <p:p:cNvPr name="Title 2" id="2"/> 
    <p:a:off x="1524000" y="4572000"/> 
    </p:p:sp> 

    </p:spTree> 
</p:sld> 

공지 사항이 페이지에서 모든 네임 스페이스 다시. 두 노드는 <p:sp>이어야하고 <a:off>이 아니야 <p:p:sp><p:a:off> 나는 new_node에서 p :를 제거 할 수 있지만 a : off는 여전히 p : 아래에 네임 스페이스가 있어야합니다. 나는 틀린 일을해야한다는 것을 안다. 내가 찾는 최종 결과는 다음과 같습니다.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" mc:PreserveAttributes="mv:*"> 
    <p:spTree> 
    <p:sp> 
     <p:nvSpPr> 
     <p:cNvPr id="1" name="Title 1"/> 
     </p:nvSpPr> 
    </p:sp> 
    <p:sp> 
     <p:cNvPr name="Title 2" id="2"/> 
     <a:off x="1524000" y="4572000"/> 
    </p:sp> 
    </p:spTree> 
</p:sld> 
+0

: "XML 폭력처럼 - 그것은 문제가 해결되지 않는 경우에, 당신이 그것을 충분히 사용하고 있지 않습니다. " –

답변

1

그래서 Nokogiri가 문제였습니다. 구조에 Hpricot! (RIP의 _why)

#!/usr/bin/ruby  
require 'rubygems' 
require 'hpricot' 

parent = <<EOF 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" mc:PreserveAttributes="mv:*"> 
    <p:spTree> 
    <p:sp> 
     <p:nvSpPr> 
     <p:cNvPr id="1" name="Title 1"/> 
     </p:nvSpPr> 
    </p:sp> 
    </p:spTree> 
</p:sld> 
EOF 

new_node = <<EOF 
    <p:sp> 
    <p:cNvPr id="2" name="Title 2"/> 
    <a:off x="1524000" y="4572000"/> 
    </p:sp> 
EOF 


doc = Hpricot(parent) 

doc.search('//p:sp').after(new_node) 

그리고 출력은 다음과 같습니다 노코 기리 홈페이지에서

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<p:sld mc:PreserveAttributes="mv:*" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" mc:Ignorable="mv" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> 
    <p:sptree> 
    <p:sp> 
     <p:nvsppr> 
     <p:cnvpr name="Title 1" id="1" /> 
     </p:nvsppr> 
    </p:sp> 

    <p:sp> 
    <p:cnvpr name="Title 2" id="2" /> 
    <a:off x="1524000" y="4572000" /> 
    </p:sp> 

    </p:sptree> 
</p:sld> 
관련 문제